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.
format
<?php /** * Image helper functions * * @author Chris */ class ImageHelper { /** * Create a thumbnail of an image and returns relative path in webroot * * @param int $width * @param int $height * @param string $img * @param int $quality * @return string $path */ public static function thumb($width, $height, $img, $quality = 75) { $pathinfo = pathinfo($img); $thumb_name = "thumb_".$pathinfo['filename'].'_'.$width.'_'.$height.'.'.$pathinfo['extension']; $thumb_path = $pathinfo['dirname'].'/.tmb/'; if(!file_exists($thumb_path)){ mkdir($thumb_path); } if(!file_exists($thumb_path.$thumb_name) || filemtime($thumb_path.$thumb_name) > filemtime($img)){ $image = Yii::app()->image->load($img); $image->resize($width, $height)->quality($quality); $image->save($thumb_path.$thumb_name); } $relative_path = str_replace(YiiBase::getPathOfAlias('webroot'), '', $thumb_path.$thumb_name); return $relative_path; } } ?>
helper
My image helper for caching of thumbnails ;)
<?php
/**
*
@author Chris
*/
class ImageHelper {
/**
*
@return string $path
*/
public static function thumb($width, $height, $img, $quality = 75)
{
$pathinfo = pathinfo($img);
$thumbname = "thumb".$pathinfo['filename'].''.$width.''.$height.'.'.$pathinfo['extension'];
$thumb_path = $pathinfo['dirname'].'/.tmb/';
if(!file_exists($thumb_path)){
mkdir($thumb_path);
}
}
$relative_path = str_replace(YiiBase::getPathOfAlias('webroot'), '', $thumb_path.$thumb_name);
return $relative_path;
}
}
?>
Excellent work!
Easy to use, perfect job. Thanks!
Great Extension!
This is a great extension! It is very easy to install and to use.
perfect
I've used this with uploadify and works perfect
thank you!
modified class
I modified the class to crop rectangular images in square images
http://www.yiiframework.com/forum/index.php?/topic/639-extension-image/page__gopid__20222
Excellent Job
Easy to install
Easy to work wit
Very useful
Great Job
Great job on this extension... love it!
R
Great job
Thanks for this extension. I'll use it :-)
@kazio, your ext4ext also great
grayscale
here is my grayscale extension for extension :)
http://wklej.to/zIBN
cheers
Thanks
Good job!
That's cool :)
Thanks a lot!
thanks
thanks :)
Excellent
Using this extension for a gallery component i will be releasing, it couldn't have been easier!
Thanks!
very nice.. (image resize function from me)... thanks for wonderful extension
I need to create images of my size preference, on the fly so I write down following function and put it into a class(component directory)
public static function image($path='',$length='',$lengthiswidth=true)
{ Yii::import('application.extensions.image.Image'); $saveas = str_replace('.',($lengthiswidth ? '_w' : '_h').'_'.$length.'.',$path); if(!file_exists($saveas)) { $image = new Image($path); $width = 0; $height = 0; if($lengthiswidth) $height = $image->height * $length / $image->width; else $width = $image->width * $length / $image->height; $image->resize($width,$height)->quality(75); $image->save($saveas); } echo Chtml::image($saveas); }
use it as
echo common::image($product->image,150,true);
// here common is the class name and "$product->image" is image path
hope this will help some one...
+1 for this extension..
excellent !
It's excellent ! Really good.
However, I don't like the need for the CArray.php file, and so for the helper folder.
I used your extension in another one, where I have removed this need (fileimagearbehavior). CArray is only needed for 1 function of 3 lines, used only twice (one call in each Image_*_Driver.php file).
You can see these two files or copy them if you don't want CArray.
Thanx for the work and this extension !
new stuff
Hi,
I used this extension (as I said in previous post) and changed some things:
You can take the code and use it or do whatever you want with. It's in my extension.
+
I agree with Parcouss.
Please, replace _line 80 in Image_GDDriver.php
$quality = CArray::remove('quality', $actions);
to:
if (array_key_exists('quality', $actions)) { $quality = $actions['quality']; unset($actions['quality']); } else { $quality = null; }
awesome
That is what i need like wideimage library.
thanks
Great share
I just installed this extension for project I am working on for gallery module, and it is working perfect. I had my own small image library that I am using for simple image manipulation tasks(croping and resizing only), but for advanced functions, this is very nice to use, especially in Yii projects!
How to use it in a loop?
When used in a loop it's an Fatal error: Cannot redeclare class Image_GD_Driver
update:
Solved:
Changing 'include' in line 123 in GD.php to 'include_once' helps.
Don't know why the author used include.
Using Image in a Loop
When used in a loop it's an Fatal error: Cannot redeclare class Image_GD_Driver update: Solved: Changing 'include' in line 123 in GD.php to 'include_once' helps. Don't know why the author used include.
That seems to have been fixed in the GD.php, but I did encounter the same issue on line 123 of Image.php where the author used require. Changing this to:
require_once("drivers/{$this->config['driver']}.php");
does the trick.
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!
"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!
Re: "Image file not found"
dave3011: you must use path, not url, i.e. /some/image/path.jpg, not http://someImage.path
yeah
good work, thanks.
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
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; }
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
addition the image class
ram0973, thank you for the addition
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.
@Jai Sundar
Please show us your PHP error - maybe a memory limit issue?
Not working for 7000X4000 image
NetworkError: 500 Internal Server Error.
500 Internal Server Error
Please have a look at your log file, usually something like /var/log/apache2/error.log
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; }
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);
4199 Thanks
4199
Thank you, Nayjest
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
Kohana Image
Haven't tried it yet, but the underlying library is available on github in version 3.2.1.
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/pagep174486hlwatermark#entry174486
Kohana Image
schmunk, this extension based on latest Kohana Image library http://www.yiiframework.com/extension/easyimage/
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 :)
Where's the documentation?
Documentation is no longer available, can you help me?
Docs from archive.org
https://web.archive.org/web/20131123133040/http://docs.kohanaphp.com/libraries/image
Issue
Replace 'require' to 'require_once', in line 123 (Image.php)
This will allow you to use the class multiple times. (rediclare class error)
Useful for multiUpload
Sorry for my English
Function is_file(), inside extension does'nt work
I have tried many times, with this:
$picture = Yii::app()->image->load( 'http://www.neologys.com/recurso/logos/Koala.jpg');
but always trhow the follow error in
ImageClass.php File:
// Check to make sure the image exists
//if (!is_file($image)) //throw new CException('image file not found' );
I reviewed the path file, all is right, i tried with absolute routes also, the access
at parent folder, the access at file...
But it doesn't work. I would be very grateful , some help
image contains errors.
i use following action to display image
public function actionImageResize() { Yii::import('application.extensions.image.Image'); $image = new Image('images/banner4.jpg'); $image->resize(400, 100); $image->render(); $this->render('ImageResize'); }
but it gives me
The image "http://localhost:83/mysite/site/ImageResize" cannot be displayed because it contains errors
i applied all configuration as per document.
Anything Wrong?
why not working for high dimesnion.Is it a bug?
Not working for high dimension why why. 7000*4000
resize three dimension
Hi guys
current I just re size one image upload in filesystem, Can re size three dimension when upload one file?
Regards
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.