Thumbnails
Resizing images is probably the most common image manipulation action on the web, and phMagick has great support for it, including run time or on the fly thumbnail generation
The first example is for offline thumbnailing
1 2 3 | include "phMagick.php"; $p = new phMagick('source.png','destination.png'); $p->resize(200,150); |

In this example source.png will be resized to 200px wide by 150px tall and saved as destination.png.
If only one measure is specified the image will be resized proportionally to that measure
As the aspect ratio of the image is maintained the new image might not be the exact size specified, for those occasions when the exact size is needed a handy function is provided
1 2 | $p = new phMagick('source.png', 'destination.png'); $p->resizeExactly(133,100); |

On the fly or run time thumbnails
If you are a web developer I’m certain you are familiar with the DIS (Designers Indecision Syndrome), where designers in the strive for the perfect design change specs a lot, a image that now is 250px by 150px tomorrow will be 130px 100px.
If your site has 500 or more images, creating thumbnails for all of them is quite a time consuming task. phMagick has the ability to generate thumbnails at run time and is smart enough to only generate the thumbnail if it doesn’t exist
1 2 3 | $p = new phMagick(); $imageUrl = $p->onTheFly('images/source.jpg', 150,100); echo '< img alt="" src="' . $imageUrl . '" />'; |
This example creates a thumbnail of source.jpg and returns the url in a format that can be used directly in html. The url will be “images/150_100_source.jpg” this way phMagic knows if the thumbnail already exists and won’t create a new one, it will simply return the url of the existing one.
This can be a very powerful technique as it frees you from manually or programmatically create each thumbnail that will be needed, it’s also very fast as it only creates the thumbnail once, and gives you the liberty experiment with the layout and thumbnail sizes without having any hassle
I need to query mysql database to pull out pdf’s and convert them to jpeg for a link thumbnail rollover.. Can you do this???
thanks
Hi Kathy
Sorry for the late response, but I’ve just arrived from my holidays.
Yes, I can do what you asked for, but it will be a paid task.
If you are still interested please let me know
King Regards
Hi Nuno, Nice job !
I’m interrested by resizing pictures without ratio like “Cut the Image to Fit” visible here http://www.imagemagick.org/Usage/thumbnails/#cut .
If I understand we can make it with resizeExactly() method, isn’t it ?
I hink it’s good to put this in this page !
Thanks !
Thanks for making that clear barat
Hi Nuno,
Great job!
I am getting errors executing the onTheFly function for generating thumbnails. I have downloaded the latest version of phmagick from the site (http://www.francodacosta.com/blog/phmagick/download).
Here is the code for generating a thumbnail image for an image.
debug=true;
$imageUrl = $p->onTheFly(“zebra_001.jpg”, 150,100);
echo ”;
echo ‘
‘;
?>
And this the error I am getting while executing the script.
Fatal error: Uncaught exception ‘Exception’ with message ‘Call to undefined method : onTheFly’ in /var/www/phmagick/phmagick.php:202 Stack trace: #0 [internal function]: phmagick->__call(‘onTheFly’, Array) #1 /var/www/phmagick/test.php(11): phmagick->onTheFly(‘zebra_001.jpg’, 150, 100) #2 {main} thrown in /var/www/phmagick/phmagick.php on line 202.
Can you please help me out.
Regards,
Sreedhar
Sreedhar,
Do you have the plugins folder? Sometimes when extracting the zip file this folder is no created
I ran into the same Bug as Sreedhar. It worked after I changed
$this->setSource($sourceFile);
$this->setDestination($thumbnailFile);
to
$p->setSource($sourceFile);
$p->setDestination($thumbnailFile);
and the call to resize from
$this->resize($width, $height, $exactDimentions);
to
$this->resize($p, $width, $height, $exactDimentions);
Hope that helps.
Ooops, forgot to mention file and function: This can be found in resize.php in the plugin-Folder and the function is onTheFly.
Thanks Karl
I will correct it.
I’m just wondering why the hell didn’t I put the onTheFly() function in my test script?
shame on me!
There is a new phMagick version, let me know if it solves your problems
Hi Nuno,
The new version works for me.
Cheers.
Hi Guys,
Facing a strange issue. previously i was using convert function to convert tiff images to jpg so that the GD functions can apply on it.
But now when i m trying create thumbnail: nothing happens, i mean no error nothing but thumbnail is not generated, tried png, jpg as both source and destination but nothing works.
Please suggest something here is my code.
convert();
$p->resize(250,200);
?>
not able to post the code correctly:
include “phmagick.php”;
$p = new phMagick(“napster.png”,”dapster.png”);
$p->resize(250,200);
@Neeraj,
Please see this article, it will show you how to debug phMagick and see error messages
http://www.francodacosta.com/blog/phmagick/examples/debugging-phmagick
If you need help, just mail me or ask here