Resize a picture to a fixed size

This function creates a thumbnail that is exactly as big as the size you give it. The image is resized to best fit the size of the thumbnail. If it does not fit exactly in both directions, it’s centered in the thumnail.

The Function:

     function thumbnail_box($img, $box_w, $box_h) {
          //create the image, of the required size
          $new = imagecreatetruecolor($box_w, $box_h);
          if($new === false) {
               //creation failed -- probably not enough memory
               return null;
          }

          //Fill the image with a light grey color
          //(this will be visible in the padding around the image,
          //if the aspect ratios of the image and the thumbnail do not match)
          //Replace this with any color you want, or comment it out for black.
          //I used grey for testing =)
          $fill = imagecolorallocate($new, 200, 200, 205);
          imagefill($new, 0, 0, $fill);

          //compute resize ratio
          $hratio = $box_h / imagesy($img);
          $wratio = $box_w / imagesx($img);
          $ratio = min($hratio, $wratio);

          //if the source is smaller than the thumbnail size,
          //don't resize -- add a margin instead
          //(that is, dont magnify images)
          if($ratio > 1.0)
               $ratio = 1.0;

          //compute sizes
          $sy = floor(imagesy($img) * $ratio);
          $sx = floor(imagesx($img) * $ratio);

          //compute margins
          //Using these margins centers the image in the thumbnail.
          //If you always want the image to the top left,
          //set both of these to 0
          $m_y = floor(($box_h - $sy) / 2);
          $m_x = floor(($box_w - $sx) / 2);

          //Copy the image data, and resample
          //If you want a fast and ugly thumbnail,
          //replace imagecopyresampled with imagecopyresized
          if(!imagecopyresampled($new, $img, $m_x, $m_y, 0, 0, $sx, $sy, imagesx($img), imagesy($img))) {
               //copy failed
               imagedestroy($new);
               return null;
          }

          //copy successful
          return $new;
     }

Example Usage:

$i = imagecreatefromjpeg("img.jpg");
$thumb = thumbnail_box($i, 210, 150);
imagedestroy($i);

if(is_null($thumb)) {
    /* image creation or copying failed */
    header('HTTP/1.1 500 Internal Server Error');
    exit();
}

header('Content-Type: image/jpeg');
imagejpeg($thumb);

2 Responses to “Resize a picture to a fixed size”

Leave a Reply

Spam Protection by WP-SpamFree

Comment moderation is enabled. Your comment may take some time to appear.

Share your minds, soul, and idea is Digg proof thanks to caching by WP Super Cache