Yii 1.1: image

Convert Kohana Image Library to Yii
93 followers

Provides methods for the dynamic manipulation of images. Various image formats such as JPEG, PNG, and GIF can be resized, cropped, rotated and sharpened.

All image manipulations are applied to a temporary image. Only the save() method is permanent, the temporary image being written to a specified image file.

Image manipulation methods can be chained efficiently. Recommended order: resize, crop, sharpen, quality and rotate or flip

此库提供方法动态的处理图片。支持对 JPEG,PNG 和 GIF 格式的图片进行调整大小,剪裁,旋转和锐化。

所有对图片的处理都会应用到一个临时图像上面,但只有 save() 方法是永久的,它会把临时图像写入指定的图像文件中。

图像处理方法也可以用“串连(chained)”方式。推荐顺序为:大小,剪裁,锐化,质量和旋转或翻转。

Resources

Documentation

Requirements

  • Yii 1.0 or above

Installation

  • Extract image folder under protected/extensions
  • Extract helpers folder under protected

Usage

The following code is the component registration in the config file:

'import'=>array(
    ...
    'application.helpers.*',
    ...
),
 
 
'components'=>array(
'image'=>array(
          'class'=>'application.extensions.image.CImageComponent',
            // GD or ImageMagick
            'driver'=>'GD',
            // ImageMagick setup path
            'params'=>array('directory'=>'/opt/local/bin'),
        ),...
)

See the following code example:

$image = Yii::app()->image->load('images/test.jpg');
$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
$image->save(); // or $image->save('images/small.jpg');

or

Yii::import('application.extensions.image.Image');
$image = new Image('images/test.jpg');
$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
$image->render();

Documentation

Change Log

February 1, 2009

  • The use of Yii::import instead of require to load image driver
  • Change image driver filename

January 22, 2009

  • Initial release.

Total 20 comments

#13752 report it
yashu at 2013/06/22 12:54pm
For those using ImageWick under Windows

Downloading Scripts (.exe) -

1.download your windows versions at http://imagemagick.org/script/binary-releases.php#windows and install them

Setting up extension in PHP-

1.download http://valokuva.org/~mikko/imagick-php54-php53.tgz

2.Extract php_*.dll to your ext folder

Config

Do something like this

'params'=>array('directory'=>'C:/Program Files/ImageMagick-6.8.6-Q16/'),

Tweak in Extension to make it work

Can this be called a bug?

1.Move to extensions/image/drivers/Image_ImageMagic_Driver.php

2.Replace

escapeshellcmd($this->dir.'convert'.$this->ext)

with

'"'.$this->dir.'convert'.$this->ext.'"'

Now everything works great!

Thanks for the extension!

Namasthe :)

#13234 report it
Artur Zhdanov at 2013/05/16 01:54pm
Kohana Image

schmunk, this extension based on latest Kohana Image library http://www.yiiframework.com/extension/easyimage/

#12984 report it
Bjorn at 2013/04/25 04:54am
Watermark

It works great, but does anyone has an example for a watermark function? Would be very nice.

On the forum i read that it has been added? http://www.yiiframework.com/forum/index.php/topic/36242-new-version-of-image-extension-supported-watermark/page__p__174486__hl__watermark#entry174486

#12348 report it
schmunk at 2013/03/15 07:00am
Kohana Image

Haven't tried it yet, but the underlying library is available on github in version 3.2.1.

#12346 report it
globaleyeglasses at 2013/03/15 12:31am
It will save time for other people using this extension

I agree with Parcouss too. Please, replace line 80 in Image_GD_Driver.php

Took a while to figure out the error and fix this $quality = CArray::remove('quality', $actions);

to

if (array_key_exists('quality', $actions)) { $quality = $actions['quality']; unset($actions['quality']);
} else { $quality = null; }

Thank You

#12151 report it
Jose H. Milán at 2013/03/01 12:44pm
#4199 Thanks

4199

Thank you, Nayjest

#11997 report it
Amr Bedair at 2013/02/19 07:20pm
Re: "Transparent GIF bug (black background)"

I added these two lines at the end of imagecreatetransparent() function to solve the problem ... just before the return

$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefilledrectangle($img, 0, 0, $width, $height, $transparent);
#11935 report it
playdog at 2013/02/13 02:49pm
NUEvo

Para las personas que usen esta extension en las nuevas versiones de yii framework. Les va a salir error. para esto deben hacer lo siguiente.

Gracias al usuario del post: #4199

$quality = CArray::remove('quality', $actions);

por

if (array_key_exists('quality', $actions)) {
    $quality = $actions['quality'];
    unset($actions['quality']);                 
} else {
    $quality = null;
}
#11602 report it
schmunk at 2013/01/22 12:32pm
500 Internal Server Error

Please have a look at your log file, usually something like /var/log/apache2/error.log

#11601 report it
Jai Sundar at 2013/01/22 12:12pm
Not working for 7000X4000 image

NetworkError: 500 Internal Server Error.

#11600 report it
schmunk at 2013/01/22 11:58am
@Jai Sundar

Please show us your PHP error - maybe a memory limit issue?

#11599 report it
Jai Sundar at 2013/01/22 11:52am
Not working for 7000X4000 image

Its working perfectly for lower dimension images.Not working for high dimension mages.Please help me to fix this issue.

#11171 report it
v_bogdan at 2012/12/22 07:36am
addition the image class

ram0973, thank you for the addition

#10124 report it
sirin k at 2012/10/05 06:00am
How to change Image background color?

hi, first of all thanks alot for the very usefull extension.

i got a problem with my resized image.ie the image im trying to resize have a rounded corners on the top.so its corner background was white before i resize it but after resizing with this extension it truned into black. on the top corners.

i think there will be some way to change this image background color in this extension.please help me.

-Sirin

#9476 report it
ram0973 at 2012/08/14 01:02pm
smart_resize

Just wrote a smart_resize function: It's auto resize and crop image dimensions EXACTLY you want.

public function smart_resize($width, $height)
    {
        if ( ! $this->valid_size('width', $width))
            throw new CException('image invalid width');
 
        if ( ! $this->valid_size('height', $height))
            throw new CException('image invalid height');
 
        if (empty($width) AND empty($height))
            throw new CException('image invalid dimensions');
 
        if ($this->image["width"]/$this->image["height"] > $width/$height) {
            $this->actions['resize'] = array
            (
                'width'  => $width,
                'height' => $height,
                'master' => Image::HEIGHT,
            );
        }
        else {
            $this->actions['resize'] = array
            (
                'width'  => $width,
                'height' => $height,
                'master' => Image::WIDTH,
            );
        }
 
        $this->actions['crop'] = array
        (
            'width'  => $width,
            'height' => $height,
            'top'    => 'center',
            'left'   => 'center',
        );
 
        return $this;
    }
#9190 report it
upadeshng at 2012/07/27 12:51am
Different scale of width and height not working in resizing image

If i give different scale in width and height, it shows scale of same dimension

#6951 report it
yiimann at 2012/02/15 12:05pm
yeah

good work, thanks.

#6570 report it
Nafania at 2012/01/19 04:44am
Re: "Image file not found"

dave3011: you must use path, not url, i.e. /some/image/path.jpg, not http://someImage.path

#6569 report it
dave3011 at 2012/01/19 04:21am
"Image file not found"

After installation of this extenstion as described (both ways, image->load() and new Image() ), I get the error: **image file not found ** (in myapppath\protected\extensions\image\Image.php(78))

This is weird since I tried to resize an image with the full path: $image = new Image('http://SomeRandomPicFromTheWeb.jpg');

Any help is appreciated!

#6124 report it
este at 2011/12/14 02:51pm
Transparent GIF bug (black background)

Is it possible to repair this bug for transparent GIF management in GD? This extension will always convert the background to black.

Guess you should use imagecolorallocatealpha()

Thank you!

Leave a comment

Please to leave your comment.

Create extension