Batch PHP Thumbnail Generator

The following script will scan all images in the same directory and create thumbnails in the ‘thumbs’ folder of each image.  You can run it as many times as you wish, it will only create thumbnails for images that don’t exist in the ‘thumbs’ folder.  For example, you can easily set it up as a cron job.  Feel free to tweak the settings or any other part of this script however you wish.

<?php
/*############################################
Batch PHP-based thumbail creator by Randy M. (rsm@fastserv.com)
Classes borrowed from hiege Iseng Resize Class (http://shiege.com/scripts/thumbnail/)
----------------------------------------------
Note :
- GD must Enabled
- Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
  but some server can't generate .gif / .wbmp file types
- If your GD not support 'ImageCreateTrueColor' function,
  change one line from 'ImageCreateTrueColor' to 'ImageCreate'
  (the position in 'show' and 'save' function)
############################################*/


$thumbs_folder="thumbs"; // folder where thumbs will go
$thumbs_width=""; // set width for thumbnail, or
$thumbs_height=""; // set height for thumbnail, or
$thumbs_autosize="220";  // set the biggest width or height for thumbnail
$thumbs_quality="75";  // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75

$d = opendir(".");
while (false !== ($file = readdir($d))) {
  if ( filetype($file) == 'file' ){
    $type = ereg_replace(".*\.(.*)$","\\1",$file);
    if ( preg_match("/^(png|gif|jpg|jpeg)$/i",$type) ) {
      if ( !file_exists  ($thumbs_folder."/".$file) ) {
        echo "Resizing: ".$file."<br>";
        $thumb=new thumbnail($file);// generate image_file, set filename to resize
        if ($thumbs_width) {$thumb->size_width($thumbs_width);}
        if ($thumbs_height) {$thumb->size_width($thumbs_height);}
        if ($thumbs_autosize) {$thumb->size_auto($thumbs_autosize);}
        if ($thumbs_quality) {$thumb->jpeg_quality($thumbs_quality);}
        $thumb->save($thumbs_folder."/".$file);
      }
    }
  }
}


class thumbnail
{
    var $img;

    function thumbnail($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";
            return;
        }
        @$this->img["lebar"] = imagesx($this->img["src"]);
        @$this->img["tinggi"] = imagesy($this->img["src"]);
        //default quality jpeg
        $this->img["quality"]=75;
    }

    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"];
    }

    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"];
    }

    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"];
         }
    }

    function jpeg_quality($quality=75)
    {
        //jpeg quality
        $this->img["quality"]=$quality;
    }

    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"]);
            @imagecopyresized ($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"]);
        }
    }

    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"]);
            @imagecopyresized ($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");
        }
    }
}
?> 

5 Responses to Batch PHP Thumbnail Generator

  1. Sara says:

    Hi Randy,

    Thanks for generously sharing this script with the world! It’s saved me a lot of time. 🙂 (I’ve yet to test it yet but so far, it works great.)

    Take care!
    -Sara

  2. ereg_replace() is now deprecated.

    I replaced
    $type = ereg_replace(".*\.(.*)$","\\1",$file);
    with
    $type = preg_replace("/.*\.(.*)$/","\\1",$file);

    and
    $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
    with
    $this->img["format"]=preg_replace("/.*\.(.*)$/","\\1",$imgfile);

    And it workedfor me on php 5.3.8

    More info here: http://www.devthought.com/2009/06/09/fix-ereg-is-deprecated-errors-in-php-53/

  3. Clicked “post comment” too fast, forgot to say thanks 😛
    That is very usefull code snippet to me so thanks alot! 🙂

  4. IsaacM says:

    You might also want to replace imagecopyresampled instead of imagecopyresized, this will produce better quality looking thumbnails

  5. Stuart says:

    This is a great post. Thanks for sharing.
    One question though. How do I execute this script from say my main page (index.php)? I’ve tried include() and require(), but they don’t work for some reason.