Resize Image Before Save To Database Using Blob

Hi,

i want to resize image before upload to database. 100x100 size.

i store the image to table files.

CREATE TABLE files (

id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,


name VARCHAR(60) NOT NULL,


file BLOB NOT NULL,


file_type VARCHAR(60) NOT NULL,


file_size INT UNSIGNED NOT NULL

)




public function actionCreate()

	{

		

                $model=new CalonPengajar;

                $file_model = new Files;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

                

		if(isset($_POST['CalonPengajar']))

		{

			$model->attributes=$_POST['CalonPengajar'];

                        $file=CUploadedFile::getInstance($model,'image');

                        

			if($model->save()){

                                                             

                                $file_model->name = $file->name;

                                $file_model->id_calon_pengajar = $model->id;

                                $file_model->file_type=$file->type;

                                $file_model->file_size=$file->size;


                                $file_model->file=file_get_contents($file->tempName);


                                <<<< I WANT RESIZE IMAGE BEFORE SAVE TO 100X100>>>


                                $file_model->save(); // DONE

				$this->redirect(array('view','id'=>$model->id));

                        }

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}




Thank you for the help.

Try this extension.

I think can’t use this because i use blob. it store the image to mysql in binary data. not store the image in folder.

if i want retrieve binary data from mysql. i use this code,




public function actionDisplaySavedImage($id)

        {

                if($id == null || trim($id)=='') {

                        echo "error in image, bad ID value [{$id}]";

                        exit();

                }

                $id_calon_pengajar = Files::model()->find('id_calon_pengajar='.$id)->id;

                $file=Files::model()->findByPk($id_calon_pengajar);

                header('Pragma: public');

                header('Expires: 0');

                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

                header('Content-Transfer-Encoding: binary');

                header("Content-Length: {$file['file_size']}\n");

                header("Content-Type: {$file['file_type']}\n");

                header("Content-Disposition: attachment; filename=\"{$file['name']}\"\n");


                echo $file['file'];

                

        }



The problem is how to resize the image in binary data format before save in mysql? Sorry for my broken english.

You may be able to capture the binary output of render in a manner similar to this.

Keith,

That link helped me a lot. Thank you so much. I share my coding here.


public function actionCreate($pageTitle='')

	{

		$this->pageTitle = 'INSTRUCTOR COURSE REGISTRATION';

                $model=new CalonPengajar;

                $file_model = new Files;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

                

		if(isset($_POST['CalonPengajar']))

		{

			$model->attributes=$_POST['CalonPengajar'];

                        $file=CUploadedFile::getInstance($model,'image');

                        

			if($model->save()){

                                                             

                                $file_model->name = $file->name;

                                $file_model->id_calon_pengajar = $model->id;

                                $file_model->file_type=$file->type;

                                $file_model->file_size=$file->size;

                                $file_model->file=file_get_contents($file->tempName);                               

                               

                                $src_img = imagecreatefromstring(file_get_contents($file->tempName));

                                $dst_img = imagecreatetruecolor(100, 100);

                                //resize image 100x100

                                imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img));

                                imagejpeg($dst_img);                                

                                ob_start();

                                imagejpeg($dst_img);

                                $image_string = ob_get_contents();

                                ob_end_flush();

                                //end resize image

                                

                                $file_model->file =$image_string;

                                $file_model->save(); // DONE

                                

                                

                                

				$this->redirect(array('view','id'=>$model->id,'image_string'=>$image_string));

                        }

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}