In this post you will find how to create thumbnail images with PHP. Function uses GD library so it doesn't depend on installed utilities like ImageMagick. On the other hand, you will have to install php-gd module - "yum install php-gd". Function will resize JPEG, PNG and GIF images, while PNG and GIF without losing their transparency. Version with GD library is a little bit longer than a version with ImageMagick utilities.
If you are looking for ImageMagick version of PHP thumbnail function, please see Create thumbnail with PHP (1). Both versions will do the same "magic".
==============================================================================
/**
* function creates a thumbnail image in the same directory with the prefix 'tn'
* thumb should fit to the defined box (second parameter)
* only bigger images are processed, while smaller images are just copied
* function will resize PNG and GIF images, without losing their transparency
*
* @param string $image1_path - full path to the image
* @param integer $box - box dimension
*/
function create_thumb($image1_path, $box=200){
// get image size and type
list($width1, $height1, $image1_type) = getimagesize($image1_path);
// prepare thumb name in the same directory with prefix 'tn'
$image2_path = dirname($image1_path) . '/tn_' .basename($image1_path);
// make image smaller if doesn't fit to the box
if ($width1 > $box || $height1 > $box){
// set the largest dimension
$width2 = $height2 = $box;
// calculate smaller thumb dimension (proportional)
if ($width1 < $height1) $width2 = round(($box / $height1) * $width1);
else $height2 = round(($box / $width1) * $height1);
// set image type, blending and set functions for gif, jpeg and png
switch($image1_type){
case IMAGETYPE_PNG: $img = 'png'; $blending = false; break;
case IMAGETYPE_GIF: $img = 'gif'; $blending = true; break;
case IMAGETYPE_JPEG: $img = 'jpeg'; break;
}
$imagecreate = "imagecreatefrom$img";
$imagesave = "image$img";
// initialize image from the file
$image1 = $imagecreate($image1_path);
// create a new true color image with dimensions $width2 and $height2
$image2 = imagecreatetruecolor($width2, $height2);
// preserve transparency for PNG and GIF images
if ($img == 'png' || $img == 'gif'){
// allocate a color for thumbnail
$background = imagecolorallocate($image2, 0, 0, 0);
// define a color as transparent
imagecolortransparent($image2, $background);
// set the blending mode for thumbnail
imagealphablending($image2, $blending);
// set the flag to save alpha channel
imagesavealpha($image2, true);
}
// save thumbnail image to the file
imagecopyresampled($image2, $image1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);
$imagesave($image2, $image2_path);
}
// else just copy the image
else copy($image1_path, $image2_path);
====================================================================================
I also wrote Resize images with PHP where you can read about resizing all JPG images inside a current directory. After saving images from my camera to the computer, I needed a tool to prepare images for the Web upload. With small command line PHP script, images are converted to the lower resolution and saved to the separate directory.
If you are looking for ImageMagick version of PHP thumbnail function, please see Create thumbnail with PHP (1). Both versions will do the same "magic".
==============================================================================
/**
* function creates a thumbnail image in the same directory with the prefix 'tn'
* thumb should fit to the defined box (second parameter)
* only bigger images are processed, while smaller images are just copied
* function will resize PNG and GIF images, without losing their transparency
*
* @param string $image1_path - full path to the image
* @param integer $box - box dimension
*/
function create_thumb($image1_path, $box=200){
// get image size and type
list($width1, $height1, $image1_type) = getimagesize($image1_path);
// prepare thumb name in the same directory with prefix 'tn'
$image2_path = dirname($image1_path) . '/tn_' .basename($image1_path);
// make image smaller if doesn't fit to the box
if ($width1 > $box || $height1 > $box){
// set the largest dimension
$width2 = $height2 = $box;
// calculate smaller thumb dimension (proportional)
if ($width1 < $height1) $width2 = round(($box / $height1) * $width1);
else $height2 = round(($box / $width1) * $height1);
// set image type, blending and set functions for gif, jpeg and png
switch($image1_type){
case IMAGETYPE_PNG: $img = 'png'; $blending = false; break;
case IMAGETYPE_GIF: $img = 'gif'; $blending = true; break;
case IMAGETYPE_JPEG: $img = 'jpeg'; break;
}
$imagecreate = "imagecreatefrom$img";
$imagesave = "image$img";
// initialize image from the file
$image1 = $imagecreate($image1_path);
// create a new true color image with dimensions $width2 and $height2
$image2 = imagecreatetruecolor($width2, $height2);
// preserve transparency for PNG and GIF images
if ($img == 'png' || $img == 'gif'){
// allocate a color for thumbnail
$background = imagecolorallocate($image2, 0, 0, 0);
// define a color as transparent
imagecolortransparent($image2, $background);
// set the blending mode for thumbnail
imagealphablending($image2, $blending);
// set the flag to save alpha channel
imagesavealpha($image2, true);
}
// save thumbnail image to the file
imagecopyresampled($image2, $image1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);
$imagesave($image2, $image2_path);
}
// else just copy the image
else copy($image1_path, $image2_path);
====================================================================================
I also wrote Resize images with PHP where you can read about resizing all JPG images inside a current directory. After saving images from my camera to the computer, I needed a tool to prepare images for the Web upload. With small command line PHP script, images are converted to the lower resolution and saved to the separate directory.
No comments:
Post a Comment