Do it simpler : Image thumbnail and resize in PHP

 
 
 
Printer-friendly version

One of the common complex task among webdevelopers is image resizing. It is a big headach when using multiple size images in multiple pages of a single version of image. For long I was using a an script that would create one thumnail version of the image when the image is uploaded. It was not a suitable solution. Many time we will need different size of thumbnails to be used in different location and pages.

I came across PHPThumb. It was a very much better one for me where it can deliver following types of resizing mechanism.

  • adaptiveResize ($width, $height)
  • crop ($startX, $startY, $cropWidth, $cropHeight)
  • cropFromCenter ($cropWidth, $cropHeight = null)
  • resize ($maxWidth, $maxHeight)
  • resizePercent ($percent)
  • rotateImage ($direction = ‘CW’)
  • rotateImageNDegrees ($degrees)
  • save ($fileName)
  • show ()

You can learn more about this script here

Well,I have written a simple script that can work with phpThumb. What it would do is, when I request for a resized version of an original image, it will check for a same resized version. If the one resized version exist, it will return the path of that. Otherwise, a new resized version will be created and saved before returning the path. This will help to use any size of thumbnail image at any time without wasting match resources.

The script

/**
 * @version 1.0 
 * @copyright (c) 2009 encodez solutions <a href="http://www.encodez.com">http://www.encodez.com</a> 
 */ 
function getResized($image, $width, $height, $resizeStyle = "ADAPTIVE", $percent = 100, $starX = 0, $starY = 0, $quality = 80) {
  /* setting file name */
  $fileName = basename($image);
 
  /* creating the path for the resized image */
  $path = substr($image, 0, -(strlen($fileName)));
  if ($resizeStyle != "RESIZEPERCENT") 
  {
    $path .= $width . "x" . $height . "-" . $resizeStyle; 
  } 
  else {
    $path .= $percent . "-" . $resizeStyle; 
  } 
  /*
   * checking whether image exists with the same criteria 
   * If exist return the generated path, 
   * If not, create the thumnail 
   */ 
  if (file_exists($path . "/" . $fileName) && filemtime($path . "/" . $fileName) > filemtime($image)) 
  { 
    return $path . "/" . $fileName; 
  } 
  else if (file_exists($image)) 
  { 
    /* making directory for the image if it is not exists */ 
    if (!file_exists($path)) mkdir($path, 0777); 
    $options = array('jpegQuality' => $quality); 
 
    try 
    { 
      /* creating the PhpThumbFactory object */ 
      $thumb = PhpThumbFactory::create($image, $options); 
    } 
    catch (Exception $ex) 
    { 
      echo "\n"; 
    } 
 
    switch ($resizeStyle) 
    { 
      case "ADAPTIVE":        
        // adaptiveResize 
        $thumb->adaptiveResize($width, $height)->save($path . "/" . $fileName); 
      break; 
 
      case "CROP":        
        // crop 
        $thumb->crop($startX, $startY, $width, $height)->save($path . "/" . $fileName); 
      break; 
 
      case "CROPCENTER": 
        // cropFromCenter 
        $thumb->cropFromCenter($width, $height = null)->save($path . "/" . $fileName); 
      break; 
 
      case "RESIZEPERCENT": 
        // resizePercent 
        $thumb->resizePercent($percent)->save($path . "/" . $fileName); 
      break; 
 
      case "RESIZE": 
        // resize 
        $thumb->resize($width, $height)->save($path . "/" . $fileName); 
      break; 
 
      default: 
        // resize 
        $thumb->resize($width, $height)->save($path . "/" . $fileName); 
    } 
    return $path . "/" . $fileName; 
  } 
  else 
  { 
    /* return 0 if the src image is not exists */ 
    return 0; 
  } 
}// end function getResized()

How to use this function?

scenario : Need a adaptive resize verion for /img/catalogue/sample1.jpg at 120px x 100px

solution :

<img src="<?php echo getResized("/img/catalogue/sample1.jpg", 120, 100, "ADAPTIVE") ?>" />

This will generate a “adaptive” resized image at the size of 120 px width and 100 px height.

This iamge will have the path

/img/catalogue/120×100-ADAPTIVE/sample1.jpg

This script might helpful you too. Please do not forget to put a comment if you like it Or any suggestion if you have.

Add new comment