Create Images Thumbnails And Cache Them In PHP
Published by philipnorton42 on Fri, 11/14/2008 - 14:58Creating 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 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.
1 2 3 4 5 6 7 8 9 10 11 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $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