Getting the image dimensions in Smarty
Previous article Next articleUsing the height and width of the images and thumbnails in your Gallery templates should be pretty straightforward, not? Well I guess it wasn't... Until now!
How to use
1. How can we get the image sizes?
We use the getimagesize PHP function for this!
But in the latest Smarty releases due to security settings PHP functions aren't available by default...
If you do want to use PHP functions, you have to enable them by adding this line to your CMSMS config.php file:
This config variable loosens some of the security configuration for smarty templates. Particularly enabling this option allows the use of any PHP function as a Smarty plugin. You better not use this option if you are allowing content to be submitted for display on your website from untrusted sources! If the function works without this line, you better not add it!
We now can use the getimagesize function with the foreach loop!
{getimagesize($image->file)}
{/foreach}
As this outputs an array, we need to use the print_r modifier to see the output the PHP function generates.
Using the print_r modifier, we get to see the Array content:
[0] => 385
[1] => 100
[2] => 2
[3] => width="385" height="100"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
1
2. Storing the getimagesize output in an array
Seeing the output using print_r is all fine and dandy but we can't use the values yet in our template just yet!
We need to store the values generated by the getimagesize modifier in an array so we can use them later on.
To achieve this, we assign the output of the getimagesize to a variable (actually it's an array but PHP takes care of this for you automatically)
Note: You can change $image->file to $image->thumb if you are after the thumbnail sizes.
3. Using the values we need in our template
All the values are now stored in the $imagesize array.
To use the values in our template we can now simply call $imagesize[##THE VALUE YOU WANT TO USE##]
Height in pixels: {$imagesize[1]}
Image type: {$imagesize[2]}
width="xxx" height="yyy": {$imagesize[3]}
etc...
4. Lets use it!!
Now we can add $imagesize[#] in the Gallery template like:
The HTML output should be:
Working example
We take this image:
And do:
{$imagesize = getimagesize($image_test)}
<pre>{getimagesize($image_test)|print_r}</pre>
The output is:
Array ( [0] => 400 [1] => 320 [2] => 2 [3] => width="400" height="320" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) 1
Comment Form
ReviewManager
ReviewManager
0 Comments
This is a great solution to stop Pagespeed complaining about missing image width & height, but I get the Smarty warning below. Any ideas how to fix? I can't get registerPlugin() working.
PHP Deprecated: Using unregistered function "getimagesize" in a template is deprecated and will be removed in a future release. Use Smarty::registerPlugin to explicitly register a custom modifier. in /home/lorikeet/public_html/avaloncraftcottage.com.au/lib/smarty/sysplugins/smarty_internal_templatecompilerbase.php on line 651