Create Images Thumbnails And Cache Them In PHP

14th November 2008

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.

  1. class ImageResize
  2. {
  3. public $img;
  4.  
  5. public function ImageResize($imgfile)
  6. {
  7. //detect image format
  8. $this->img["format"] = ereg_replace(".*\.(.*)$","\\1",$imgfile);
  9. $this->img["format"] = strtoupper($this->img["format"]);
  10. if($this->img["format"] == "JPG" || $this->img["format"] == "JPEG"){
  11. //JPEG
  12. $this->img["format"] = "JPEG";
  13. $this->img["src"] = ImageCreateFromJPEG ($imgfile);
  14. }elseif($this->img["format"] == "PNG"){
  15. //PNG
  16. $this->img["format"] = "PNG";
  17. $this->img["src"] = ImageCreateFromPNG ($imgfile);
  18. }elseif($this->img["format"] == "GIF"){
  19. //GIF
  20. $this->img["format"] = "GIF";
  21. $this->img["src"] = ImageCreateFromGif($imgfile);
  22. } elseif ($this->img["format"] == "WBMP"){
  23. //WBMP
  24. $this->img["format"] = "WBMP";
  25. $this->img["src"] = ImageCreateFromWBMP ($imgfile);
  26. } else {
  27. //DEFAULT
  28. echo "Not Supported File";
  29. exit();
  30. };
  31. $this->img["lebar"] = imagesx($this->img["src"]);
  32. $this->img["tinggi"] = imagesy($this->img["src"]);
  33. //default quality jpeg
  34. $this->img["quality"] = 75;
  35. }
  36.  
  37. public function size_height($size = 100)
  38. {
  39. //height
  40. $this->img["tinggi_thumb"] = $size;
  41. $this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
  42. }
  43.  
  44. public function size_width($size = 100)
  45. {
  46. //width
  47. $this->img["lebar_thumb"] = $size;
  48. $this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  49. }
  50.  
  51. public function size_auto($size = 100)
  52. {
  53. //size
  54. if($this->img["lebar"]> = $this->img["tinggi"]){
  55. $this->img["lebar_thumb"] = $size;
  56. $this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  57. }else{
  58. $this->img["tinggi_thumb"] = $size;
  59. $this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
  60. };
  61. }
  62.  
  63. public function jpeg_quality($quality = 75)
  64. {
  65. //jpeg quality
  66. $this->img["quality"] = $quality;
  67. }
  68.  
  69. public function show()
  70. {
  71. //show thumb
  72. header("Content-Type: image/".$this->img["format"]);
  73.  
  74. /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  75. $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
  76. 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"]);
  77.  
  78. if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG"){
  79. //JPEG
  80. imageJPEG($this->img["des"],"",$this->img["quality"]);
  81. }elseif($this->img["format"] == "PNG"){
  82. //PNG
  83. imagePNG($this->img["des"]);
  84. }elseif($this->img["format"] == "GIF"){
  85. //GIF
  86. imageGIF($this->img["des"]);
  87. }elseif($this->img["format"] == "WBMP"){
  88. //WBMP
  89. imageWBMP($this->img["des"]);
  90. };
  91. }
  92.  
  93. public function save($save = "")
  94. {
  95. //save thumb
  96. if (empty($save)) {
  97. $save = strtolower("./thumb.".$this->img["format"]);
  98. }
  99. /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  100. $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
  101. 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"]);
  102.  
  103. if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG") {
  104. //JPEG
  105. imageJPEG($this->img["des"],"$save",$this->img["quality"]);
  106. } elseif ($this->img["format"] == "PNG") {
  107. //PNG
  108. imagePNG($this->img["des"],"$save");
  109. } elseif ($this->img["format"] == "GIF") {
  110. //GIF
  111. imageGIF($this->img["des"],"$save");
  112. } elseif ($this->img["format"] == "WBMP") {
  113. //WBMP
  114. imageWBMP($this->img["des"],"$save");
  115. };
  116. }
  117. }

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

  1. include('class.ImageResize.php');
  2. // create ImageResize object
  3. $originalImage = new ImageResize("anImage.jpg");
  4. // use the show function to print this image to screen
  5. $originalImage->show();
  6. // use the save function to save this image to another file - leave empty to save as thumb.anImage.jpg
  7. $originalImage->save("anotherFile.png");
  8. // use one of the size functions to resize the image
  9. $originalImage->size_width(120);
  10. // save it again...
  11. $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.

  1. $imageName = str_replace(dirname("./images/", "" , "http://www.example.com/images/an_image.jpg");
  2.  
  3. if (file_exists("./thumb_cache" . $imageName)) {
  4. // 2592000 = 30 days
  5. if ( time() - filemtime("./thumb_cache".$imageName) > 2592000 ) {
  6. unlink("./thumb_cache".$imageName);
  7. }
  8. }
  9. if (!file_exists("./thumb_cache" . $imageName)) {
  10. include('class.ImageResize.php');
  11. // if cache file does not exist then create it.
  12. $originalImage = new ImageResize("./images/" . $_result['image_path']);
  13. $originalImage->size_width(120);
  14. $originalImage->save("./thumb_cache".$imageName);
  15. }

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.