FREE PHP script to create thumbnails from JPG's

Permalink
Hi you all,

Just thought I would offer up a script that I use that saves me a lot of time.

It's a simple script based mostly on one I found on the internet (I forget where I found it) that I modified slightly.

Here's the code...(assumes there is a JPG called "pebbles.jpg" that I want to create a thumbnail for)...

<?php
// The file you want to create a thumbnail for
$file = 'pebbles.jpg';
$save = 'pebbles-thumb.jpg';
// Sets the size of the thumbnail relative to the size of the original
$size = 0.25;
// We want to create a JPG
header('Content-type: image/jpeg');
// Set the resizing parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;
// Create and resize the thumbnail
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);


Simply drop the above script into whatever directory your images are in (or use explicit paths in the $file variable and the $save variable), change the name to the image you want to create a thumbnail for, and run it at the command line like so...

$ php whatever_name.php


May not work under the infamous Windows operating system. I run entirely on Linux.

For what it's worth.

The script could be improved upon greatly by processing an array containing file names or reading from the command line, etc. but I don't have time to modify it like that and it's quick enough for me to use it to create one thumbnail at a time.

Released into the public domain if there is any question (at least in so far as I modified it).

Oh...I almost forgot...if you use this script you agree to use it entirely at your own risk. I or Concrete5 cannot be held liable for it completely destroying your hard disk, computer, and causing you to fall over while in your chair, knocking over the lit candle, and burning down your house. LOL

Carlos

 
boomgraphics replied on at Permalink Reply
boomgraphics
concrete5 has the built in capability for that:
$ih = Loader::helper('image');
$picture = $c->getAttribute('image_attribute'); //could be image url I think
$thumbnail = (isset($picture)) ? $ih->getThumbnail($picture, 200, 100, array('crop' => true, 'quality' => 100)) : '';
echo $thumbnail;
carlos123 replied on at Permalink Reply
Oh...cool.

I didn't realize it could do that boomgraphics though I think I prefer to do things through a direct GD using script like the one I posted rather than pulling in Concrete5 and it's overhead to do it for me.

Unless I do it dynamically within a Concrete5 created page in which case using Concrete5 to do it makes sense.

Thanks for letting me know.

Carlos
KJLJon replied on at Permalink Reply
KJLJon
Do you know if it caches the image so it doesn't have to recreate it on the fly every time, as long as the settings are all the same?
carlos123 replied on at Permalink Reply
I have no clue as to whether it does that or not KJLJon.

Carlos
KJLJon replied on at Permalink Reply
KJLJon
In case anyone is wondering, it does cache it :)