Yii Framework Forum: Resize Image Before Save To Database Using Blob - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Resize Image Before Save To Database Using Blob Rate Topic: ***-- 1 Votes

#1 User is offline   Ann 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 13-April 11
  • Location:malaysia

Posted 04 December 2012 - 02:59 AM

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.
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,595
  • Joined: 04-March 10
  • Location:UK

Posted 04 December 2012 - 04:41 AM

Try this extension.
0

#3 User is offline   Ann 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 13-April 11
  • Location:malaysia

Posted 04 December 2012 - 09:28 PM

View PostKeith, on 04 December 2012 - 04:41 AM, said:




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.
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,595
  • Joined: 04-March 10
  • Location:UK

Posted 05 December 2012 - 03:58 AM

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

#5 User is offline   Ann 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 13-April 11
  • Location:malaysia

Posted 06 December 2012 - 12:51 AM

View PostKeith, on 05 December 2012 - 03:58 AM, said:

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,
		));
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users