Yii 1.1: simpleimage

Simple extension to resize image
15 followers

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/

Total 15 comments

#11920 report it
Cozumel at 2013/02/13 01:47am
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!

#10145 report it
yasen at 2012/10/07 11:31am
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);
#9579 report it
cass at 2012/08/24 12:41pm
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

#7567 report it
javad at 2012/03/30 10:48am
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");
#5861 report it
enfield at 2011/11/21 02:41am
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.

#5860 report it
David T at 2011/11/21 01:43am
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

#5857 report it
enfield at 2011/11/21 12:04am
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?

#5141 report it
suriyansuresh at 2011/09/18 07:35am
I fixed that issue

add <?php at the top line in simple_image.php
please update the extension

#5140 report it
suriyansuresh at 2011/09/18 07:25am
Please check my previous comment

I have tried that line also

#5138 report it
David T at 2011/09/18 06:29am
Fixed

I am sorry when I gave the wrong class name. You just replaced this line:

$img = Yii::app()->simple_image->load($file);

To this:

$img = Yii::app()->simpleImage->load($file);

This would make the extension run properly.

#5137 report it
suriyansuresh at 2011/09/18 05:55am
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"); 
}
#5136 report it
David T at 2011/09/18 05:36am
solution

hi suriyansuresh, I place file in \protected\extensions\CSimpleImage.php. please fix your path.

#5134 report it
suriyansuresh at 2011/09/18 02:36am
Fatal Error

Class 'simple_image' not found in C:\wamp\www\mysite\protected\extensions\simple_image\CSimpleImage.php on line 14

#5126 report it
David T at 2011/09/16 04:49am
Because it simple

Reason I add new extension: - It simple and very easy to use - Lightweight and your page will load faster

Most of time, I just need to resize image.

#5124 report it
redguy at 2011/09/16 04:08am
Why new extension?

Why new extension? There is already http://www.yiiframework.com/extension/image ehich is much more powerful than just resize...

Leave a comment

Please to leave your comment.

Create extension
Downloads
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: David T
  • Category: Others
  • Votes: +16
  • Downloaded: 2,611 times
  • Created on: Sep 16, 2011
  • Last updated: Sep 18, 2011
  • Tags: image