Last updated on January 17, 2018
This Post is about Creating Thumbnail Images Using PHP, Thumbnails are very useful for websites it will save the bandwidth and reduce the page loading time(performance boost). Nobody wants to create Thumbnails for each image on the website Manually(no time or expensive), so to create dynamic Thumbnails on the fly just go throw the below function.
following example uses the PHP GD2 library functionality. The only Drawback of Dynamic Thumbnails doesn’t look as good as thumbnails created in Photoshop.
The code below creates a function named createThumbs that will get three parameters. The first and the second is correspondingly the path to the Image and the path to the directory in which thumbnails will be placed. The third parameter is the width you want for the thumbnail images.
The PHP Thumbnail Script
function createThumbs($src, $dest, $desired_width) { /* load image and get image size */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); /* find the "desired height" of this thumbnail, relative to the desired width */ $desired_height = floor($height * ($desired_width / $width)); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width, $desired_height); /* copy source image at a resized size */ imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image, $dest); }