You don't need to create many types of thumbnails for images in your project.
You can create a thumbnail directly in the View
. Thumbnail will automatically cached. It's easy!
Features: ¶
- Easy to use
- Support
GD
andImagick
- Automaticly thumbnails caching
- Cache sorting to subdirectories
- Support
Retina
displays - Based on Kohana Image Library.
Requirements ¶
- Yii 1.1 or above
GD
orImagick
Resources ¶
Note: Latest release and documentation are available from extension github page.
Installing and configuring ¶
Extract the EasyImage
folder under protected/extensions
Add the following to your config file components
section:
'components'=>array(
//...
'easyImage' => array(
'class' => 'application.extensions.easyimage.EasyImage',
//'driver' => 'GD',
//'quality' => 100,
//'cachePath' => '/assets/easyimage/',
//'cacheTime' => 2592000,
//'retinaSupport' => false,
),
and the following to import
section:
'import' => array(
//...
'ext.easyimage.EasyImage'
),
Usage ¶
InstanceOf ¶
$image = new EasyImage('/path/to/image.jpg');
$image->resize(100, 100);
$image->save('/full/path/to/thumb.jpg');
Parameters ¶
- string
$file
required - Image file path - string
$driver
- Driver:GD
,Imagick
ThumbOf ¶
You can create a thumbnail directly in the View
:
// Create and autocache
Yii::app()->easyImage->thumbOf('/path/to/image.jpg', array('rotate' => 90));
// or
Yii::app()->easyImage->thumbOf('image.jpg', array('rotate' => 90), array('class' => 'image'));
// or
Yii::app()->easyImage->thumbOf('image.png',
array(
'resize' => array('width' => 100, 'height' => 100),
'rotate' => array('degrees' => 90),
'sharpen' => 50,
'background' => '#ffffff',
'type' => 'jpg',
'quality' => 60,
));
Note. This method return CHtml::image()
Parameters ¶
- string
$file
required - Image file path - array
$params
- Image manipulation methods. See Methods - array
$htmlOptions
- options for CHtml::image()
ThumbSrcOf ¶
Yii::app()->easyImage->thumbSrcOf('image.jpg', array('crop' => array('width' => 100, 'height' => 100)));
Note. This method return path to image cached.
Parameters ¶
- string
$file
required - Image file path - array
$params
- Image manipulation methods. See Methods
Suggestions
Not tested, but will be coming back to this extension ;-).
If not done:
Hint: I have my own YHtml class which extends CHtml. I would put a replacement
for 'image' in there.
Looks nice!
+1 for composer support ;)
Images with absolute path not supported?
Hi, excellent extension.
However, I got a "file name can't be empty" error if I use:
$thumbnail = new EasyImage($absoluteImagePath);
This error is due the EasyImage constructor uses this:
However, I am using EasyImage from a console command (images are generated asynch) and I need read the images from a different directory and save the thumbs on a different directory.
If I remove "dirname(Yii::app()->basePath)" from EasyImage above line, works like a charm.
Can you add more flexibility regarding paths please?
Thank you,
Leandro
Absolute path
Leandro, thanks for your comment! I'll do it at nearest time.
Thank you
Thank you very much Artur for include this change in a future version!
Can I ask just one more change please? :)
I'm using Yii with Yiiboilerplate. The extensions path alias is different from an standard Yii application.
The EasyImage class imports this:
Yii::import('ext.easyimage.drivers.<class>');
But with Yiiboilerplate, there are several extensions directories for the different applications (for frontend, backend, console and common).
I need change the above lines with:
Yii::import('common.extensions.easyimage.drivers.<class>');
Could the extension alias be configurable too please?
Thank you!
Leandro
@leandro: Aliases
Your change request is good, but you can also fix your problem with this workflow.
Some problems
Hi
<?php $img = Yii::app()->easyImage->thumbOf($path.$photo->id.'.jpg', array( 'resize' => array('width' => 1280, 'height' => null), 'type' => 'jpg', 'quality' => 100 )); $imgNew = str_replace(array('/frontend/www',"<","=","img",'/>','src"','"','alt',' ',"'"),'',$img); // CVarDumper::dump($imgNew); // echo CHtml::image(Yii::app()->createAbsoluteUrl($path.$photo->id.'.jpg')); echo CHtml::image(Yii::app()->createAbsoluteUrl($imgNew));
i wanna save some space on my server then i use your extension.
but i have some reasons about your methods. first thing is your thumbOf or your thumbOfSrc doesn't return the right Url. Next thing is i dont understand how to build a retina img without double my images(this step i really doesnt want ).
My Way:
My User upload big images like 3000px.... then i cache the size what i need with your extension. My Structure is backend and frontend in other folders. the i save my images in common and build symlink in my Frontend and Backend to my upload Folder then i use your extension to get the sizes what i need but only in Frontend to save Space.
Indeed easy
Was indeed very easy to use.
I use it to scan my image folder,and then create thumbnails of each image that does not have a thumbnail then display the thumbnail as a link to the full size image. Easy to combine with with any javascript gallery.
<?php $imageFolder = "images/portfolio/"; foreach(glob($imageFolder.'*') as $image){ //search our images/portfolio folder for images $filename = str_replace($imageFolder,"",$image); //extract the filename+extension only $thumbnail = $imageFolder."thumbnails/".$filename; // thumbnail path if(is_file($image)){ //make sure no folder or such is included if(!is_file($thumbnail)){ //if there is no thumbnail create one $crop = new EasyImage($imageFolder.$filename); //the image $crop->resize(75, 75); //new size $crop->save($imageFolder."thumbnails/".$filename); //save to thumbnail } //echo the thumbnail as a link to the full size image and set title to the filename only echo CHtml::link(CHtml::tag('img',array( 'src'=>$thumbnail, 'alt'=>$image, )),$image,array('title'=>str_replace(array($imageFolder,'.png','.jpg','.jpeg','.gif'),"",$image))); } }?>
How to set it title ?
How to set it title ?
Yii::app()->easyImage->thumbOf($modelNovedades[0]['imagen'],array('resize' => array('height' => 236),'type' => 'jpg','quality' => 60),array('title'=>'hola'))
re: How to set it title ?
playdog, your code is correct.
Result:
[html] <img title="hola" src="/assets/easyimage/c/c8dc27197f545c9cd85160e3d4764a8d.jpg" alt="">
Maybe you are using the cache for the page?
default image
Dear,
very nice extension I used to use Image extension but moving to this....I wanted to ask if it supports default image if the image was not found: example: when trying to resize user profile image if he user doesn't have an image the a default user.png will be returned....
Cheers
CGridView ?
Dear all,
Can we use this extension on CGridView? Any idea ?
Regards!
imagerotate available only with bunbled GD error
If you are running you app under Ubuntu or Debian, you may have seen this message. I'm not exactly sure which Ubuntu php5 package release corrects this but it is no longer valid. The current php553 install includes a GD version that has imagerotate so you'll need to comment out these lines 230-232 in Image_GD.php
// if (!Image_GD::$_bundled) {
// throw new CException('This method requires imagerotate, which is only available in the bundled version of GD');
// }
I'm now able to rotate images correctly. If you are running on php5.3 or earlier, I think the issue is real and imagerotate is only available where included in the PHP bundle. I ran across this as I develop on WAMP and deploy to LAMP on Ubuntu.
Jim,
I think you should install package "php-gd-bundled".
re: imagerotate available only with bunbled GD error
@Artur, "E: Unable to locate package php-gd-bundled" when I try apt-get. Is this package available from a different repo? It doesn't appear to be in standard repo's.
Jim,
Try this
exception Not an image or invalid image though image exists
Hi,
I have used this extension in my project and it gives exception Not an Image or Invalid Image though image exists. the extension throws the exception on linux environment and on windows environment it works normal for the same image.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.