Discover & Rate New Music Check out ChartVote. Promote the music you like.


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:
  1. function createThumb($source,$dest) {
  2.  
  3.     $thumb_size = 150;
  4.  
  5.     $size = getimagesize($source);
  6.     $width = $size[0];
  7.     $height = $size[1];
  8.  
  9.     if($width> $height) {
  10.         $x = ceil(($width - $height) / 2 );
  11.         $width = $height;
  12.     } elseif($height> $width) {
  13.         $y = ceil(($height - $width) / 2);
  14.         $height = $width;
  15.     }
  16.  
  17.     $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
  18.     $im = imagecreatefromjpeg($source);
  19.     imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  20.     imagejpeg($new_im,$dest,100);
  21.  
  22. }

Download Source
- squarethumb.zip (1KB)


24 Comments

  1. JNunn on August 29th, 2006

    Wow--that's pretty cool, and a tiny code footprint to boot--nice.

  2. AB on August 31st, 2006

    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

  3. links for 2006-09-01 » D.C Life on September 1st, 2006

    [...] Create square image thumbnails with PHP - Find Motive (tags: php tutorials) [...]

  4. Valerio on October 9th, 2006

    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!

  5. Carsten on November 22nd, 2006

    Good script, I was looking for this for a long time :)

  6. jordon on November 30th, 2006

    Dont suppose anyone has a handy asp.net 2 version?

  7. Brian on February 5th, 2007

    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... :)

  8. thebrainman on March 6th, 2007

    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!

  9. Scott » Blog Archive » PHP function - create thumbnails with same width and height on March 9th, 2007

    [...]     // updated to make same width and height    // from http://www.findmotive.com/2006/08/29/create-square-image-thumbnails-with-php/ [...]

  10. Keo on May 27th, 2007

    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!

  11. Sam on June 18th, 2007

    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?

  12. Sam on June 18th, 2007

    got it worked out... register_globals was off... turned it on..

    how would i get this to work with gif images as well?

  13. luke snowden on July 22nd, 2007

    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

  14. Tom on August 7th, 2007

    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!

  15. rwin_schro on August 19th, 2007

    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.

  16. Mike on September 6th, 2007

    How do you call this php function ??? am newbie in php

  17. gaurav on September 18th, 2007

    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.

  18. Codex on September 18th, 2007

    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.

  19. Francky on October 3rd, 2007

    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);
    }

  20. Antonio on December 8th, 2007

    Thank you very much, it's exactly what I was looking for ;-)

  21. group-policy-prevent-install on December 11th, 2007

    Great work. This will come in handy :)
    Beats the bloated thumbnails scripts I was finding...

  22. Ugas piezīmes » Blog Archive » Bildes thubnail četrstūris on March 3rd, 2008

    [...] http://www.randomsequence.com/articles/making-square-thumbnails-with-imagemagick/ http://www.findmotive.com/2006/08/29/create-square-image-thumbnails-with-php/ [...]

  23. Joe on June 10th, 2008

    Oh thank god I found you! $width = $height. That's all it took to make everything connect. I just didin't think of that.

  24. Bene on June 24th, 2008

    5th - 7th line could be written in 1 line:

    list($width, $height) = getimagesize($source);

Leave a Reply

RSS Feed



Recommended Sites