Create Images Thumbnails And Cache Them In PHP

14th November 2008 - 4 minutes read time

Creating image thumbnails is a pretty common practice, and there are a few scripts available that allow you to do this in PHP using the GD2 library. However, they are normally overkill for what should be a simple task, so after a bit of searching and testing I found the following ImageResize class, which is taken from http://shiege.com/scripts/thumbnail/. I have modified the code to be PHP5, but if you want the PHP4 version then you can get it from the site.

class ImageResize
{
  public $img;
 
  public function ImageResize($imgfile)
  {
    //detect image format
    $this->img["format"] = ereg_replace(".*\.(.*)$","\\1",$imgfile);
    $this->img["format"] = strtoupper($this->img["format"]);
    if($this->img["format"] == "JPG" || $this->img["format"] == "JPEG"){
      //JPEG
      $this->img["format"] = "JPEG";
      $this->img["src"]  =  ImageCreateFromJPEG ($imgfile);
    }elseif($this->img["format"] == "PNG"){
      //PNG
      $this->img["format"] = "PNG";
      $this->img["src"]  =  ImageCreateFromPNG ($imgfile);
    }elseif($this->img["format"] == "GIF"){
      //GIF
      $this->img["format"] = "GIF";
      $this->img["src"]  =  ImageCreateFromGif($imgfile);
    } elseif ($this->img["format"] == "WBMP"){
      //WBMP
      $this->img["format"] = "WBMP";
      $this->img["src"]  =  ImageCreateFromWBMP ($imgfile);
    } else {
      //DEFAULT
      echo "Not Supported File";
      exit();
    };
    $this->img["lebar"]  =  imagesx($this->img["src"]);
    $this->img["tinggi"]  =  imagesy($this->img["src"]);
    //default quality jpeg
    $this->img["quality"] = 75;
  }
 
  public function size_height($size = 100)
  {
    //height
    $this->img["tinggi_thumb"] = $size;
    $this->img["lebar_thumb"]  =  ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
  }
 
  public function size_width($size = 100)
  {
    //width
    $this->img["lebar_thumb"] = $size;
    $this->img["tinggi_thumb"]  =  ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  }
 
  public function size_auto($size = 100)
  {
    //size
    if($this->img["lebar"]> = $this->img["tinggi"]){
      $this->img["lebar_thumb"] = $size;
      $this->img["tinggi_thumb"]  =  ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
    }else{
      $this->img["tinggi_thumb"] = $size;
      $this->img["lebar_thumb"]  =  ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
    };
  }
 
  public function jpeg_quality($quality = 75)
  {
    //jpeg quality
    $this->img["quality"] = $quality;
  }
 
  public function show()
  {
    //show thumb
    header("Content-Type: image/".$this->img["format"]);
 
    /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
    $this->img["des"]  =  ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    imagecopyresampled($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
 
    if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG"){
      //JPEG
      imageJPEG($this->img["des"],"",$this->img["quality"]);
    }elseif($this->img["format"] == "PNG"){
      //PNG
      imagePNG($this->img["des"]);
    }elseif($this->img["format"] == "GIF"){
      //GIF
      imageGIF($this->img["des"]);
    }elseif($this->img["format"] == "WBMP"){
      //WBMP
      imageWBMP($this->img["des"]);
    };
  }
 
  public function save($save = "")
  {
    //save thumb
    if (empty($save)) {
      $save = strtolower("./thumb.".$this->img["format"]);
    }
    /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
    $this->img["des"]  =  ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    imagecopyresampled($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
 
    if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG") {
      //JPEG
      imageJPEG($this->img["des"],"$save",$this->img["quality"]);
    } elseif ($this->img["format"] == "PNG") {
      //PNG
      imagePNG($this->img["des"],"$save");
    } elseif ($this->img["format"] == "GIF") {
      //GIF
      imageGIF($this->img["des"],"$save");
    } elseif ($this->img["format"] == "WBMP") {
      //WBMP
      imageWBMP($this->img["des"],"$save");
    };
  }
}

Put this into a file called class.ImageResize.php and you can use it to resize and show any image you want.

include('class.ImageResize.php');
// create ImageResize object
$originalImage = new ImageResize("anImage.jpg");
// use the show function to print this image to screen
$originalImage->show();
// use the save function to save this image to another file - leave empty to save as thumb.anImage.jpg
$originalImage->save("anotherFile.png");
// use one of the size functions to resize the image
$originalImage->size_width(120);
// save it again...
$originalImage->save("thumb_anotherFile.png");

If you want to create a simple thumbnail caching function then you can use the following code. It checks to see if the image exists and if it is older than 30 days. If it is then the file is deleted and because the file no longer exists (or if it never existed) the next part of the code where the thumbnail is created is run.

$imageName = str_replace(dirname("./images/", "" , "http://www.example.com/images/an_image.jpg");
 
if (file_exists("./thumb_cache" . $imageName)) {
  // 2592000 = 30 days
  if ( time() - filemtime("./thumb_cache".$imageName) > 2592000 ) {
    unlink("./thumb_cache".$imageName);
  }
}
if (!file_exists("./thumb_cache" . $imageName)) {
  include('class.ImageResize.php');
  // if cache file does not exist then create it.
  $originalImage = new ImageResize("./images/" . $_result['image_path']);
  $originalImage->size_width(120);
  $originalImage->save("./thumb_cache".$imageName);
}

Add new comment

The content of this field is kept private and will not be shown publicly.