Web Solutions Point

This blog is all about sharing solutions

Free Advertisement

Saturday, March 10, 2018

How to generate thumbnail images in php


Generating thumbnail images is a basic requirement in every project. If your webpage has lots of images then you should definitely use thumbnail images in place of original image. It can improve your web page performance. Because when your web page has lots of images, it will consume a lot of bandwidth to download that images, due to this your web page will take lot of time to render into the browser. To solve this issue we will use thumbnail images in place of original images and as you know the thumbnail images are always smaller than original images. so, your web page will not take too much time to load and it will load web page very fast.

Here i will share php function, which will help you to generate thumbnail images.

function ab_thumbnail($source_image_path, $thumbnail_image_path, $reduceSize, $reduceSizePercentage, $thumbnailMaxWidth, $thumbnailMaxHeight, $maintainAspectRatio, $bgColor = 0, $quality = 100) {

            //The getimagesize() function will determine the size of any supported given image file
            //and return the dimensions along with the file type and a height/width
            list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);

            switch ($source_image_type) {
                case IMAGETYPE_GIF:
                    $source_gd_image = imagecreatefromgif($source_image_path);
                    break;
                case IMAGETYPE_JPEG:
                    // imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename
                    $source_gd_image = imagecreatefromjpeg($source_image_path);
                    break;
                case IMAGETYPE_PNG:
                    $source_gd_image = imagecreatefrompng($source_image_path);
                    break;
            }

            if ($source_gd_image === false) {
                // Return false if image type is unknown
                return false;
            }

            // First check reduce size parameter is true or false        
            if ($reduceSize == true) {
                $reducedWidth = $source_image_width * $reduceSizePercentage;
                $reducedHeight = $source_image_height * $reduceSizePercentage;

                // Create a new true color image
                // imagecreatetruecolor() returns an image identifier representing a black image of the specified size
                $thumbnail_gd_image = imagecreatetruecolor($reducedWidth, $reducedHeight);

                // imagecopyresampled() copies a rectangular portion of one image to another image,
                // smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity
                imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $reducedWidth, $reducedHeight, $source_image_width, $source_image_height);

                // imagejpeg() creates a JPEG file from the given image
                imagejpeg($thumbnail_gd_image, $thumbnail_image_path, $quality);
                imagedestroy($source_gd_image);
                imagedestroy($thumbnail_gd_image);
                return true;
            } else if ($maintainAspectRatio == true) { // Then check $maintainAspectRatio parameter is true or false        
                // Custome code ends here
                // Calculating aspect ratio of original image
                $source_aspect_ratio = $source_image_width / $source_image_height;

                // Calculating aspect ratio of thumbnail image
                $thumbnail_aspect_ratio = $thumbnailMaxWidth / $thumbnailMaxHeight;

                if ($source_image_width <= $thumbnailMaxWidth && $source_image_height <= $thumbnailMaxHeight) {
                    // If original image is smaller than thumbnail image
                    $thumbnail_image_width = $source_image_width;
                    $thumbnail_image_height = $source_image_height;
                } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
                    // thumbnail aspect ratio is greater than original image aspect ratio
                    $thumbnail_image_width = (int) ($thumbnailMaxHeight * $source_aspect_ratio);
                    $thumbnail_image_height = $thumbnailMaxHeight;
                } else {
                    // thumbnail aspect ratio is smaller than original image aspect ratio
                    $thumbnail_image_width = $thumbnailMaxWidth;
                    $thumbnail_image_height = (int) ($thumbnailMaxWidth / $source_aspect_ratio);
                }

                // Create a new true color image
                // imagecreatetruecolor() returns an image identifier representing a black image of the specified size
                $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);

                // imagecopyresampled() copies a rectangular portion of one image to another image,
                // smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity
                imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);

                // Create a new true color image
                $img_disp = imagecreatetruecolor($thumbnailMaxWidth, $thumbnailMaxWidth);
                // Allocate a bg color for an image
                $backcolor = imagecolorallocate($img_disp, $bgColor, $bgColor, $bgColor);
                // Performs a flood fill starting at the given coordinate (top left is 0, 0) with the given color in the image
                imagefill($img_disp, 0, 0, $backcolor);

                // Copy a part of src_im onto dst_im starting at the x,y coordinates src_x, src_y with a width of src_w and a height of src_h.
                // The portion defined will be copied onto the x,y coordinates, dst_x and dst_y.
                imagecopy($img_disp, $thumbnail_gd_image, (imagesx($img_disp) / 2) - (imagesx($thumbnail_gd_image) / 2), (imagesy($img_disp) / 2) - (imagesy($thumbnail_gd_image) / 2), 0, 0, imagesx($thumbnail_gd_image), imagesy($thumbnail_gd_image));

                // imagejpeg() creates a JPEG file from the given image
                imagejpeg($img_disp, $thumbnail_image_path, $quality);
                imagedestroy($source_gd_image);
                imagedestroy($thumbnail_gd_image);
                imagedestroy($img_disp);
                return true;
            } else {
                // Create a new true color image
                // imagecreatetruecolor() returns an image identifier representing a black image of the specified size
                $thumbnail_gd_image = imagecreatetruecolor($thumbnailMaxWidth, $thumbnailMaxHeight);

                // imagecopyresampled() copies a rectangular portion of one image to another image,
                // smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity
                imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnailMaxWidth, $thumbnailMaxHeight, $source_image_width, $source_image_height);

                // imagejpeg() creates a JPEG file from the given image
                imagejpeg($thumbnail_gd_image, $thumbnail_image_path, $quality);
                imagedestroy($source_gd_image);
                imagedestroy($thumbnail_gd_image);
                return true;
            }
        }


        $img_source1 = 'sample 1.jpg';
        $img_source2 = 'sample 2.jpg';

        $img_thumb_path1 = 'uploads/sample 1.jpg';
        $img_thumb_path2 = 'uploads/sample 2.jpg';


        $reduceSize = true;
        $reduceSizePercentage = 1;

        $thumbnailMaxWidth = 150;
        $thumbnailMaxHeight = 150;
        $maintainAspectRatio = true;
        $bgColor = 218;
        $quality = 10;


        // Call the function
        ab_thumbnail($img_source2, $img_thumb_path2, $reduceSize, $reduceSizePercentage, $thumbnailMaxWidth, $thumbnailMaxHeight, $maintainAspectRatio, $bgColor, $quality);

First of all, i will list down all the features of provided by this function.
  • you can reduce size of an image
  • you can generate thumbnail image by keeping aspect ratio
  • you can generate fixed size thumbnail image
  • you can set image quality of generated image
  • you can set background color of generated thumbnail image

So let's get started,
Here, i am using two sample images. 1st one is landscape image & 2nd one is portrait image.

How to generate thumbnail images in php
Sample image 1 (size - 173 KB)

How to generate thumbnail images in php
Sample image 2 (size - 53 KB)


After generating thumbnail images of sample 1: 
How to generate thumbnail images in php
Fixed size thumbnail (size - 37 KB)

How to generate thumbnail images in php
Low quality thumbnail (size - 21 KB)

How to generate thumbnail images in php
Thumbnail by keeping aspect ratio & custom background color (size - 15 KB)

How to generate thumbnail images in php
Reduced size thumbnail (size - 124 KB)

After generating thumbnail images of sample 2:
How to generate thumbnail images in php
Fixed size thumbnail (size - 28 KB)

How to generate thumbnail images in php
Low quality thumbnail (size - 9 KB)

How to generate thumbnail images in php
Thumbnail by keeping aspect ratio & custom background color (size - 12 KB)

How to generate thumbnail images in php
Reduced size thumbnail (size - 49 KB)

Note: 

This function will first check $reduceSize parameter. If this parameter is set to false then it will proceed for custom size thumbnail generation process


This function has two optional parameter
  • Background color parameter is optional and its default value is black
  • Quality parameter is also optional and its default value is 100

No comments:

Post a Comment