SAImageDisplayer is a Yii Widget providing a better display of images in the views.
This extension resizes images and thumbnail only when they are called and if resized image or his thumbnail does not exist. This means that you can upload images without taking care of dimensions.
This extension is based on justintimeimageresizer extension adding some improvements :
To install this extension it's really easy, all you have to do is extract the SAImageDisplayer.php file in your ``extension` folder!
In this part we are going to configure the extension. This part is optional but I encourage you to do it because later you won't have to put all the variables defined in the config in each of your call to the widget!
There are some params that won't change from one call to the widget to another. This is why I encourage you to use the ability that yii offers to globally configure some widgets option!
To do it edit your configuration main file (in general config/main.php) and add the following code:
return array( ... 'components'=>array( 'widgetFactory'=>array( 'widgets'=>array( ... 'SAImageDisplayer'=>array( 'theOption'=>theValue, 'otherOption'=>otherValue, ), ), ), ), );
The options that I encourage you to put in this configuration are:
webroot. Default to images:'baseDir' => 'images',
originals:'originalFolderName' => 'originals',
groups):'sizes' =>array( 'nameOfTheSize' => array('width' => width, 'height' => height), ),
'groups' => array( 'nameOfTheGroup' => array( 'nameOfTheSize' => array('width' => width, 'height' => height), ), ),
sizesThe following example:
'sizes' =>array( 'tiny' => array('width' => 40, 'height' => 30), 'big' => array('width' => 640, 'height' => 480), 'thumb' => array('width' => 400, 'height' => 300), ),
will give the following structure (if images is defined as baseDir and originals as originalFolderName):
images/ big/ originals/ thumb/ tiny/
groupsThe following example:
'groups' => array( 'news' => array( 'tiny' => array('width' => 40, 'height' => 30), 'big' => array('width' => 640, 'height' => 480), ), 'reviews' => array( 'thumb' => array('width' => 400, 'height' => 300), ), ),
will give the following structure (if images is defined as baseDir and originals as originalFolderName):
images/
news/
big/
originals/
tiny/
reviews/
originals/
thumb/
Here is a complete configuration example that you could use:
return array( ... 'components'=>array( 'widgetFactory'=>array( 'widgets'=>array( ... 'SAImageDisplayer'=>array( 'baseDir' => 'images', 'originalFolderName'=> 'originals', 'sizes' =>array( 'tiny' => array('width' => 40, 'height' => 30), 'big' => array('width' => 640, 'height' => 480), 'thumb' => array('width' => 400, 'height' => 300), ), 'groups' => array( 'news' => array( 'tiny' => array('width' => 40, 'height' => 30), 'big' => array('width' => 640, 'height' => 480), ), 'reviews' => array( 'thumb' => array('width' => 400, 'height' => 300), ), ), ), ), ), ), );
For the usage, I'll assume that you have followed the configuration step above. If not, each params defined before need to be put in each widget call in your views.
All the originals images have to be in the folder defined by originalFolderName. If you are using the option group then the original image has to be in the folder groupName/originalFolderName
To simply display an image with a given size here is the syntax:
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', ));
If you want to display the original file with its original size (for a gallery for example) you can do it by not setting size or setting it to original:
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', ));
Now you can simply add a title to the image:
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', 'title' => 'My super title', ));
This way you can set:
titlealtclassidstyleNot sure your image really exists? No need to check it, the plugin do it for you and allow you to define a default image!
This image as to be in your original folder and will be resized to match the size you set.
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', 'defaultImage' => 'default.png', ));
Let's say you have some images representing the news on your site and some others the reviews. You don't want to mix them because after it's a mess to now what image belongs to what type. I agree with you, everything need to be clean!
This is why you can declare a group for each image you are displaying! The plugin will work in the folder corresponding to the group name.
So to display a new image I'll use:
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', 'defaultImage' => 'default.png', 'group' => 'news', ));
And for a review:
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', 'defaultImage' => 'default.png', 'group' => 'reviews', ));
And in your image folder you'll have something clean like:
images/
news/
big/
originals/
thumb/
reviews/
originals/
thumb/
Be careful, since you are using the option group, the original and the default images have to be in the folder groupName/originalFolderName!
Of course we can use group and size for some images and just specify size for others!
This way you'll endup with a structure looking like:
images/
big/
originals/
news/
big/
originals/
tiny/
reviews/
originals/
thumb/
thumb/
tiny/
Since v1.2 we can generate a new image size whitout displaying the image on the screen!
All we have to do is set the option displayImage to false in the widget. All the other operations will be performed except displaying the image tag.
$this->widget('ext.SAImageDisplayer', array( 'image' => 'yourImage.png', 'size' => 'thumb', 'defaultImage' => 'default.png', 'displayImage' => false, ));
image : Name of the image to display (with the extension)size : Name of the size to display. The size has to be declared in sizes or groups if the option group is useddefaultImage : Name of the default image to display if image doesn't existsizes : Array containing all the possible sizes than can be called when group is not defined. The array has to be with the following structure:'sizes' =>array( 'nameOfTheSize' => array('width' => width, 'height' => height), ),
groups : Array containing all the groups and the associated sizes that the widget may use. The array has to be with the following structure:'groups' => array( 'nameOfTheGroup' => array( 'nameOfTheSize' => array('width' => width, 'height' => height), ), ),
baseDir : The path to the dir that will contain originals and resized folders. The root of this param will be webroot. Default to imagesoriginalFolderName : The name of the folder holding the originals images. default to originalstitle : Title of the image tagalt : Alternative name of the imagestyle : Style of the image tagid : Id of the image tagclass : Class of the image taggroup : Name of the group the image belongs todisplayImage : Weither or not we should display the image on the screen. Default to true.Don't hesitate to ask for help or tell me any issue you could meet while using the plugin! If you see som typo errors in this extension page please tell me, English is not my mother tongue so there might have plenty of them.
Feel free to provide some ideas of improvment and if you provide some pull request on github I'll most definitely examinate your code and add it to the widget if I thin it adds something!
Total 20 comments
Actually I meant before uploading, resizing the original image to a more suitable size. I'll look into it but I'm also exploring the ImageMagick solution!
BTW, thank a lot!
The widget can't handle it on the client side since all the php is executed on the server side (only javascript can be executed int he client browser). If you don't want to change to imagemagick or give more memory to php your best bet is to limit the size of the uploaded image and force the users to give smaller images in the upload form!
Hi,
Thank you for your help, I will try it out. But I think it must be possible to find something to use client side memory to resize before uploading, no?
Nevertheless, I'll look up ImageMagick!
hi again darkheir :D
i wonder how to display image orderly. at the moment im thinking about using dynamicdrive css as the style. i have been trying it but somehow, i think im doing it the wrong way.
if you have spare time, please drop by to forum : How To Display Image Orderly. i already post question, code, and the displayed picture there.
thank you. :)
Okay, I tried with a big image and I have an error! The error is:
My memory limit was
32M. I tried to change it to512M(just for testing purpose) and then the image is resized and displayed! So the problem is that php needs a lot of memory to resize a big sized image.For me the solution would be one of the following:
php.inifile change thememory_limitvar to something more important.ImageMagickinstead ofGDto manipulate images.Imagemagickis not impacted by php memory limit, so you will be able to leave the same amount of memory for php. The drawback is thatImageMagickneeds to be installed on your server.Thank you for helping, Actually, this did not work, my image is uploaded but not resized... :-/
Hi,
Thank for your kind comment, I'm glad that you like this extension!
For your problem with big Images I have to admit that I never tried it. I will look into that, but chances are that php hasn't enough memory to handle big sized images or that it's a problem coming from the image extension.
To know if this is a problem coming from php or the image extension you could try the following code for one of the images causing problem:
Hi,
First, thank you for this work, it takes a huge place in my project and you are reponsible of its success :-)
I just had an issue with large images when trying to show the resized version of them, e.g. >5Mb for this exemple. I don't have any error code to show, my app just crashes. Do you have any suggestion ?
The version 1.3 fixes rhigel problem (see forum thread for more informations)
the image only show the title as picture. can you give me advice where i do wrong in my code.
_view.php
it show the name/title "21.png" as picture. it doesnot show the real picture. i wonder why is that happen. below is my main code.
main.php
i also put same question in yii forum. Image Does Not Show. if know the answer of my problem, you can either answer here or in the forum.
thank you.
This is actually a great idea!
I will update the extension and the documentation when I have some spare time!
Hi, I am using this extension quite often, works perfect. Thanks for this. I have made one addition to your script, which actually makes the usage of it even broader. My case is: Sometimes, I do want to display an image inside another widget. Or within a cell of the grid. To call a widget from within another widget can be very difficult or whith huge workarounds. I created a simple solution for it. I defined a new paramter called "precheck". So the widget calls this:
Within your extension, I define that this variable, set it default to be false and edited the run-Function so that it only returns the image-tag, if precheck is false. This way your whole script is running, checking if image exist,storing it correctly without showing it inside the view, as nothing is returned. I can then inside the view use the image in fancybox, knowing that your script has created everything needed on the page.
Hope it helps others, gb5256
OMG i forgot image extension! testing if it works now..
EDIT: Ok, it works now. Great support, thank you very much and sorry, im yii noob :P
Just a question: have you installed the "image" extension (http://www.yiiframework.com/extension/image)?
It's indicated in the requirements that this extension needs Image's extension to work.
If it's not it then I'm going to watch it at home and I'll get back to you! Keep me informed.
Yes.
My config:
And in my view:
And the error: Stack Trace
0
+ C:\xampp\htdocs\yii-1.1.13\framework\YiiBase.php(395): YiiBase::autoload()
1
unknown(0): YiiBase::autoload("Image")
2
+ C:\xampp\htdocs\WorkPro\protected\extensions\SAImageDisplayer.php(170): spl_autoload_call("Image")
3
+ C:\xampp\htdocs\WorkPro\protected\extensions\SAImageDisplayer.php(125): SAImageDisplayer->createImagesIfNotExists()
4
+ C:\xampp\htdocs\yii-1.1.13\framework\web\CBaseController.php(147): SAImageDisplayer->init()
5
+ C:\xampp\htdocs\yii-1.1.13\framework\web\CBaseController.php(172): CBaseController->createWidget("ext.SAImageDisplayer", array("image" => "a.jpg", "size" => "centro_trabajo_thumbnails"))
6
+ C:\xampp\htdocs\WorkPro\protected\modules\centrotrabajo\views\centrotrabajo_form.php(120): CBaseController->widget("ext.SAImageDisplayer", array("image" => "a.jpg", "size" => "centro_trabajo_thumbnails"))
7
+ C:\xampp\htdocs\yii-1.1.13\framework\web\CBaseController.php(126): require("C:\xampp\htdocs\WorkPro\protected\modules\centrotrabajo\views\ce...")
8
+ C:\xampp\htdocs\yii-1.1.13\framework\web\CBaseController.php(95): CBaseController->renderInternal("C:\xampp\htdocs\WorkPro\protected\modules\centrotrabajo\views\ce...", array("model" => Centrotrabajo), true)
9
+ C:\xampp\htdocs\yii-1.1.13\framework\web\CController.php(869): CBaseController->renderFile("C:\xampp\htdocs\WorkPro\protected\modules\centrotrabajo\views\ce...", array("model" => Centrotrabajo), true)
Thanks.
@Tikisman : Could you show me what did you put in your configuration file and how did you call the plugin?
bad installation instructions, plugin fails
@venuk : The extension only display an image so the demo wouldn't be showing anything much interesting.
Basically the extension displays an image resizing it to the desired size. So you don't need to resize it when it is uploaded. The image will be resized on the go when the extension is called whith a size the image doesn't exit in. The advantages of this approach are:
Then there are some other options that I think are usefull when dispaying images:
A demo would be a great helpful to understand
I'll try it on the next project
Leave a comment
Please login to leave your comment.