Wednesday, March 12, 2014

Creating Thumbnails with PHP using the GD Library

<?php
function getBaseResizeImage($sourceImgPath,$destinatioImgPath,$resizePercentage,$extensiontoConvert=''){
/*
* PHP GD
* resize an image using GD library
*/

// File and new size
//the original image
$filename=file_get_contents($sourceImgPath);
$filename2 = $sourceImgPath;


if($extensiontoConvert==''):
$ext = pathinfo($filename2, PATHINFO_EXTENSION);
else:
$ext=$extensiontoConvert;
endif;

//the resize will be a percent of the original size
$percent = $resizePercentage;

// Content type
header('Content-Type: image/jpeg');
header('Content-Type: image/png');
 

// Get new sizes
list($width, $height) = getimagesize($filename2);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth,$newheight);
$source = imagecreatefromstring($filename);

// Resize
imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);

// Output and free memory
//the resized image will
if($ext=='jpg'):
$destPath=$destinatioImgPath;
imagejpeg($thumb,$destPath);
endif;
if($ext=='png'):
$destPath=$destinatioImgPath;
imagepng($thumb,$destPath);
endif;
//imagedestroy($thumb);
}
$sourceImgPath='2879.jpg';
$destinatioImgPath='ram.png';
$resizePercentage='0.5';
$extensiontoConvert='png';
getBaseResizeImage($sourceImgPath,$destinatioImgPath,$resizePercentage,$extensiontoConvert);
?>

No comments:

Post a Comment