PHP SCript Resize Gambar

Sekedar catatan script php untuk resize gambar

function resize($file,$savePath,$new_width=”100″,$new_height=”auto”,$imageQuality=”100″){
if(isset($file[‘tmp_file’]))
{
$tmpFile = fopen($file[“tmp_name”], “r”); // temporary file
$binnaryFile = fread($tmpFile,filesize($file[“tmp_name”])); // binnnary file
$sourceImage = imagecreatefromstring($binnaryFile);
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
if($new_width==”auto”){
$new_width = $new_height/$width*$height;
}
if($new_height==”auto”){
$new_height = $new_width/$width*$height;
}
$destinationImage = imagecreatetruecolor($new_width,$new_height);
imagecopyresized($destinationImage,$sourceImage,0,0,0,0,$new_width,$new_height,$width,$height);

switch($file[‘type’])
{
case ‘image/png’:
if (imagetypes() & IMG_PNG)
{
imagepng($destinationImage, $savePath, $imageQuality);
}
break;
case ‘image/gif’:
if (imagetypes() & IMG_GIF)
{
imagegif($destinationImage, $savePath, $imageQuality);
}
break;
case ‘image/jpg’:
case ‘image/jpeg’:
if (imagetypes() & IMG_JPG)
{
imagejpeg($destinationImage, $savePath, $imageQuality);
}
break;
}
}
else
{
return false;
}
return true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *



*