Create square image thumbnails with PHP
Creating image thumbnails in PHP is nothing new, but I have rarely seen a function that will create a square thumbnail without distorting the image. The function below will take an image and regardless of whether its a portrait or landscape size image it will generate a square thumbnail that is not distorted.
Example
PHP Source
PHP:
-
function createThumb($source,$dest) {
-
-
$thumb_size = 150;
-
-
$width = $size[0];
-
$height = $size[1];
-
-
if($width> $height) {
-
$width = $height;
-
} elseif($height> $width) {
-
$height = $width;
-
}
-
-
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
-
$im = imagecreatefromjpeg($source);
-
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
-
imagejpeg($new_im,$dest,100);
-
-
}
Download Source
- squarethumb.zip (1KB)

My name is Noah Everett. I live in Tulsa, OK. I started 
Wow--that's pretty cool, and a tiny code footprint to boot--nice.
Just a quick one for the javascript inept: how do you call the function? can you outline a quick example of using it for us? With mahy thanks... AB
[...] Create square image thumbnails with PHP - Find Motive (tags: php tutorials) [...]
Any idea how to recall it? I have a WHILE statement that pull out images from a db, but I cannot elaborate and show them... help!
Good script, I was looking for this for a long time
Dont suppose anyone has a handy asp.net 2 version?
Awesome, yet so simple. Thanks for this!
For those who need help using it.. I found it easier to remove the function bit (lines 1 & 22) and save the entire thing as a file, say sqaurethumbs.php in the directory where your images are. Then I'd simply do a http img src and call the new page, eg
mydomain.com/gallery/squarethumb.php?source=sample.jpg
Hope this helps...
Very useful, and yes very simple! Good Work! If anyone is interested... you can modify the function to allow you to reuse the code a little better, by adding an additional parameter to the funtion for size. See modified below...
function createThumb($source,$dest,$size) {
$thumb_size = $size;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$dest,100);
}
Hope that helps someone!
[...] // updated to make same width and height // from http://www.findmotive.com/2006/08/29/create-square-image-thumbnails-with-php/ [...]
This is what I love about PHP, I've seen dozens of overly complex scripts trying to do this and you blow them away with 15 lines of code!
Thanks GJ!
i got this to work on a server with php 4 installed but i can not get it to work with php 5, is it compatible with php 5?
got it worked out... register_globals was off... turned it on..
how would i get this to work with gif images as well?
dont wanna sound stupid or anything, but can sumone please give me an example of the $source and $dest... getting errors allover the place lol
You should also put above the if statements:
$y = NULL;
$x = NULL;
Otherwise you might end up with errors saying "undefined x" or "undefined y" (depending on which length is longer). Unless you go the route of using the image src based method like stated above here in the comments...I believe at that point the errors are suppressed.
Also, if you are getting errors with the paths you are specifying for $source and $dest remember that you need to use the paths within the file system and not the http:// protocol. On php.net look up: __FILE__ and work yourself back from there.
This does exactly what I need and makes use of some nice functions. I will probably add gif and png support to it using the other functions specific to those types that this script just turned me on to (PHP is highly underrated for working with images and graphics).
Thanks for sharing this nice piece of code!
Thanks for the code.
I'll adapt it for our minifrontpage FREE Joomla! module. This module have been adapted another method of getting the thumbnails, preserving aspect ratio. So with your code, we'll have another option to forced the thumbnails into the same height and width. thx.
How do you call this php function ??? am newbie in php
hi,
why don't you give a complete working example. like how to call the function, where to place it etc.? This is a nice function but doesn't help all the way.
Dude,
thank you so much! I have been trying to tackle this all day. Aaaalmost got it, but looking at your code I made it way too difficult. Glad I found yours!
You rock.
Man, your code is what i want to do from 2 days now...
I wrote mine that look like your one, but...when the thumbnail is created, there's a black part on the left side of the image and the image look squeezed.
Do you know what could be the problem ?
Here's my code :
function creationThumb($categorie, $image, $numero, $lettres, $etape){
$nomPhoto = $image;
$sourceFile = "atraiter/".$categorie."/".$image;
$resultat = getimagesize($sourceFile);
$largeurSource = $resultat[0];
$hauteurSource = $resultat[1];
$largeurResult = $hauteurResult = 124;
$posX = $_POST[posx];
$posY = $_POST[posy];
$image_p = imagecreatetruecolor($largeurResult, $hauteurResult);
$image = imagecreatefromjpeg($sourceFile);
imagecopyresampled($image_p, $image, 0, 0, $posX, $posY, $largeurResult, $hauteurResult, $largeurSource, $hauteurSource);
$source = $image_p;
$newThumb = "atraiter/".$_GET[cat]."/".$lettres.$numero.".jpg";
imagejpeg($source, $newThumb);
}
Thank you very much, it's exactly what I was looking for
Great work. This will come in handy
Beats the bloated thumbnails scripts I was finding...
[...] http://www.randomsequence.com/articles/making-square-thumbnails-with-imagemagick/ http://www.findmotive.com/2006/08/29/create-square-image-thumbnails-with-php/ [...]
Oh thank god I found you! $width = $height. That's all it took to make everything connect. I just didin't think of that.
5th - 7th line could be written in 1 line:
list($width, $height) = getimagesize($source);