note 98413 deleted from function.imagecopyresized by danbrown
| From: | danbrown@php.net | Date: | Mon, 14 Jun 2010 16:29:25 +0000 |
| Subject: | note 98413 deleted from function.imagecopyresized by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170118@lists.php.net to get a copy of this message | ||
Note Submitter: Stefano AI
----
To preserve the ALPHA channel in images
<?
function thumb($image, $sizethumb, $filename) {
switch($image['type']) {
case "image/jpeg":
case "image/jpg":
case "image/JPG":
case "image/pjepg":
$img = imagecreatefromjpeg($image['tmp_name']);
break;
case 'image/gif':
case 'image/GIF':
$img = imagecreatefromgif($image['tmp_name']);
break;
case 'image/bmp':
case 'image/BMP':
case 'image/x-ms-bmp':
$img = imagecreatefromwbmp($image['tmp_name']);
break;
case 'image/PNG':
case 'image/png':
$img = imagecreatefrompng($image['tmp_name']);
break;
default:
return false;
}
$src_w=imagesx($img);
$src_h=imagesy($img);
$ratio=$src_w/$src_h;
if($ratio>1) {
$dst_w=$sizethumb;
$dst_h=$sizethumb/$ratio;
}else {
$dst_h=$sizethumb;
$dst_w=$sizethumb*$ratio;
}
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($dst_image, false);
imagesavealpha($dst_image, true);
if(imagecopyresampled($dst_image, $img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h)==false)
return false;
imagedestroy($img);
switch($image['type']) {
case "image/jpeg":
case "image/jpg":
case "image/JPG":
case "image/pjepg":
if(imagejpeg($dst_image, $filename)==false)
return false;
break;
case 'image/gif':
case 'image/GIF':
if(imagegif($dst_image, $filename)==false)
return false;
break;
case 'image/bmp':
case 'image/BMP':
case 'image/x-ms-bmp':
if(imagewbmp($dst_image, $filename)==false)
return false;
break;
case 'image/PNG':
case 'image/png':
if(imagepng($dst_image, $filename)==false)
return false;
break;
}
imagedestroy($dst_image);
return true;
}
?>
http://www.stefanoai.com