Home / Generate thumbnails with PHP

Generate thumbnails with PHP

There are many examples and tutorials online about how to generate thumbnail images with PHP but I decided to add another one myself in order to follow up an earlier post titled "extract images from a web page with PHP and the Simple HTML DOM Parser". This post features the class to generate the thumbnail images and the next post will show how to use the Simple HTML DOM Parser to grab all the images from a page and create thumbnails from them.

thumbnailGenerator Class

The thumbnail generator class is below. I’ve made it a class so the allowable image format types can be set without having to pass them as a function and to split out the creating an image from a file. I’ll probably add another function to it in the future to generate thumbnails for all images in a directory, and/or from an array of image filenames.

class thumbnailGenerator {

    var $allowableTypes = array(

    public function imageCreateFromFile($filename, $imageType) {

        switch($imageType) {
            case IMAGETYPE_GIF  : return imagecreatefromgif($filename);
            case IMAGETYPE_JPEG : return imagecreatefromjpeg($filename);
            case IMAGETYPE_PNG  : return imagecreatefrompng($filename);
            default             : return false;


     * Generates a thumbnail image using the file at $sourceFilename and either writing it
     * out to a new file or directly to the browser.
     * @param string $sourceFilename Filename for the image to have thumbnail made from
     * @param integer $maxWidth The maxium width for the resulting thumbnail
     * @param integer $maxHeight The maxium height for the resulting thumbnail
     * @param string $targetFormatOrFilename Either a filename extension (gif|jpg|png) or the
     *   filename the resulting file should be written to. This is optional and if not specified
     *   will send a jpg to the browser.
     * @return boolean true if the image could be created, false if not
    public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg') {

        $size = getimagesize($sourceFilename); // 0 = width, 1 = height, 2 = type

        // check to make sure source image is in allowable format
        if(!in_array($size[2], $this->allowableTypes)) {
            return false;

        // work out the extension, what target filename should be and output function to call
        $pathinfo = pathinfo($targetFormatOrFilename);
        if($pathinfo['basename'] == $pathinfo['filename']) {
            $extension = strtolower($targetFormatOrFilename);
            // set target to null so writes out to browser
            $targetFormatOrFilename = null;
        else {
            $extension = strtolower($pathinfo['extension']);

        switch($extension) {
            case 'gif' : $function = 'imagegif'; break;
            case 'png' : $function = 'imagepng'; break;
            default    : $function = 'imagejpeg'; break;

        // load the image and return false if didn't work
        $source = $this->imageCreateFromFile($sourceFilename, $size[2]);
        if(!$source) {
            return false;

        // write out the appropriate HTTP headers if going to browser
        if($targetFormatOrFilename == null) {
            if($extension == 'jpg') {
                header("Content-Type: image/jpeg");
            else {
                header("Content-Type: image/$extension");

        // if the source fits within the maximum then no need to resize
        if($size[0] <= $maxWidth && $size[1] <= $maxHeight) {
            $function($source, $targetFormatOrFilename);
        else {

            $ratioWidth = $maxWidth / $size[0];
            $ratioHeight = $maxHeight / $size[1];

            // use smallest ratio
            if($ratioWidth < $ratioHeight) {
                $newWidth = $maxWidth;
                $newHeight = round($size[1] * $ratioWidth);
            else {
                $newWidth = round($size[0] * $ratioHeight);
                $newHeight = $maxHeight;

            $target = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
            $function($target, $targetFormatOrFilename);


        return true;



Usage Examples

Create a thumbnail from /tmp/big.jpg and send it to the browser with maximum dimensions of 100×100 – this will default to jpg format and will send the necessary image type header:

$tg = new thumbnailGenerator;
$tg->generate('/tmp/big.jpg', 100, 100);

Do the same as above, but make the resulting file a png:

$tg = new thumbnailGenerator;
$tg->generate('/tmp/big.jpg', 100, 100, 'png');

Create a thumbnail from /tmp/big.jpg and save it to /tmp/small.jpg with maximum dimensions of 100×100:

$tg = new thumbnailGenerator;
$tg->generate('/tmp/big.jpg', 100, 100, '/tmp/small.jpg');

Same as the above but this time create a thumbnail in png format

$tg = new thumbnailGenerator;
$tg->generate('/tmp/big.jpg', 100, 100, '/tmp/small.jpg');

Follow up posts

The next post will show how to use this class in conjunction with the Simple HTML DOM Parser to generate thumbnail images for all images in a webpage, and I will also create an additional function for the class to create thumbnails for all images in a directory.