Most of time we just need resize function in a image library. This extension uses simple_image to resize image.
Requirements ¶
Yii 1.1 or above
Usage ¶
extract download file to protected/extensions
Edit main.php
'components'=>array(
'simpleImage'=>array(
'class' => 'application.extensions.CSimpleImage',
),
Usage
$file="images/logo.jpg";
$img = Yii::app()->simpleImage->load($file);
$img->resizeToWidth(128);
$img->save("newLogo.jpg");
Resources ¶
For more information: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
User Contributed Notes 16
Why new extension?
Why new extension? There is already http://www.yiiframework.com/extension/image
ehich is much more powerful than just resize...
Because it simple
Reason I add new extension:
Most of time, I just need to resize image.
Fatal Error
Class 'simple_image' not found in C:\wamp\www\mysite\protected\extensions\simple_image\CSimpleImage.php on line 14
solution
hi suriyansuresh, I place file in \protected\extensions\CSimpleImage.php. please fix your path.
Error persists
Can you fix this issue
Property "CWebApplication.simple_image" is not defined.
I copied CSimpleImage.php and simple_image.php to extensions folder
Here is my configuration
'components' => array(
'file' => array( 'class' => 'application.extensions.file.CFile', ), 'simpleImage' => array( 'class' => 'application.extensions.CSimpleImage', ), .... public function actionTest() { $file = "images/logo.jpg"; $img = Yii::app()->simple_image->load($file); // $img = Yii::app()->simpleImage->load($file); i have tried this line also $img->resizeToWidth(128); $img->save("newLogo.jpg"); }Fixed
I am sorry when I gave the wrong class name.
You just replaced this line:
To this:
This would make the extension run properly.
Please check my previous comment
I have tried that line also
I fixed that issue
add <?php at the top line in simple_image.php
please update the extension
Property "CWebApplication.simpleImage" is not defined.
I changed every simple_image to simpleImage also in trying to overcome this.
view:
$file=$this->createUrl('displaySavedImage',array('id'=>$model->primaryKey)); $img = Yii::app()->simpleImage->load($file); $img->resizeToWidth(128); $img->output();Is this because of my folder structure?
$homePath = dirname(__FILE__) . '/../..'; $protectedPath = _joinpath($homePath, 'protected'); $webrootPath = _joinpath($homePath, 'webroot'); $runtimePath = _joinpath($homePath, 'runtime');if so what do I need to change in the extension to make it work for me?
simple image
The name simpleImage is defined in protected/config/main.php
'simpleImage'=>array( 'class' => 'application.extensions.CSimpleImage', ),if you can the file in simple_image.php to simpleImage.php. You need to change CSimpleImage.php to reflex that change
Still not working.
Hi DavidTran, thanks for the quick response. I have changed every instance of the name to simpleImage and it does not work. This is after I tried your code as provided.
This is is consistent no matter what I try to change to get things to work.
Property "CWebApplication.simpleImage" is not defined.
I appreciate you putting this together and it is something I could really use so it would be nice to understand how to get it working.
solve problem
protected/extensions/resizeImage/CSimpleImage.php
protected/extensions/resizeImage/simple_image.php
'components'=>array( 'simpleImage'=>array( 'class' => 'ext.resizeImage.CSimpleImage' )$file = "images/logo.jpg"; $img = Yii::app()->simpleImage->load($file); $img->resizeToWidth(128); $img->save("newLogo.jpg");Cropping
I appreciate this may go beyond of what the extension originally wanted to achieve, but I had to crop a few images and instead of adding yet another extension, I added the following method to this one:
function crop($startX, $startY, $width , $height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height); $this->image = $new_image; }Might come in handy for someone, I presume.
Cheers,
Cass
Save Method Improvement
I've changed simple_image::save() as follows:
function save($filename, $image_type=false, $compression=75, $permissions=null) { $image_type = $image_type ? $image_type : $this->image_type;This way saving a non-jpg image will not require adding image type parameter when calling save().
Edit: Adding at the end of the methods:
return $this;one may do calls like
Yii::app()->simpleImage->load($imgPath)->crop($conf['w'],$conf['h'])->save($imgPath);Property "CWebApplication.simpleImage" is not defined
If you get this error make sure that you've placed
'simpleImage'=>array( 'class' => 'application.extensions.CSimpleImage', ),In the correct place and not in the middle of another array ( ),
Great extension, thanks!
Why new extension?
@Maciej:
I already tried 'image' extension which exported from Kohana, but it have problem with multiple file upload (CUploadedFile). I always get this error if I upload 2 images or more
Fatal error: Cannot redeclare class Image_GD_Driver in /home/user/public_html/protected/extensions/image/drivers/GD.php on line 380
I tried to unset every variable for each loop, but still got these error.
But this extension (simpleimage) work perfectly for CUploadedFile.
Leave a comment
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.