Using phpThumb() within CakePHP


I am doing a Web project using the CakePHP framework for my “day job”. I have said it before, and I’ll say it again, I love the user community and the user contributed code that CakePHP offers at the bakery. I am displaying some images on a particular section of the site and want these images to be uploaded by end users at 640×480 through a Web based interface. I then want these images to be displayed first on the site as thumbnails, however, I didn’t want the end users to have to learn how to resize and upload multiple images. Where did I turn? That’s right, kids! The CakePHP user community!

I found a really nice tutorial with code titled “Thumbnail Generation with phpThumb” at the bakery and dove in. The tutorial uses an open-source thumbnail generation PHP application called phpThumb() (imagine that!). I followed the tutorial line by line and had no issues creating the controllers and implementing user specified this “vendor” application into CakePHP and my web application. However, I did have some issues initially due to a lack of understanding of how phpThumb() works. Here’s what I mean.

The thumbnail generation script that you create with the afore mentioned tutorial and phpThumb() can accept three parameters: height, width, and quality (for JPEGs). When I was calling the script I was taking it back to a thumbnail generator I created a while back. I was only passing the width and quality or the height and quality. I figured the script would automagically determine the other parameter. Well, it will if you specify values for height or width that are less than the default or user defined maximum height and width. If you don’t, it will suppress your image to whatever the maximum height or width values are.

Here’s an example: my script, and probably every default download, has the maximum width and height set to 200px. So, if I were to specify a 600 pixel width for a photo that is 640×480, phpThumb() would try and resize my image to 600×440 (or something, heck I don’t know), but, because 440 is bigger than 200, which is the default for the height, it would make the image whatever it needed to to keep the height at 200 pixels high.

So, the smart thing to do, and probably what I should have been doing all along, is to specify the maximum height and width that you want the given image to be. phpThumb() will resize the image as close to that size as it can while keeping the aspect ratio the same. Pretty swift! I just didn’t know exactly how phpThumb() worked so I did meet some frustration along the way and thought the script was jacked up or something…

Maybe this will save someone out there some frustration. I know it will me in the future. I always need friendly little reminders from myself.

Until next time…

Leave a Reply


 

Categories