note 104028 added to function.imagecopyresampled

From: Date: Wed, 18 May 2011 12:26:34 +0000
Subject: note 104028 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-179845@lists.php.net to get a copy of this message
Here is my ultimate image resizer that preserves transparency for gif's and png's and has an option to crop images to fixed dimensions (preserves image proportions by default) <?php function image_resize($src, $dst, $width, $height, $crop=0){ if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type!"; $type = strtolower(substr(strrchr($src,"."),1)); if($type == 'jpeg') $type = 'jpg'; switch($type){ case 'bmp': $img = imagecreatefromwbmp($src); break; case 'gif': $img = imagecreatefromgif($src); break; case 'jpg': $img = imagecreatefromjpeg($src); break; case 'png': $img = imagecreatefrompng($src); break; default : return "Unsupported picture type!"; } // resize if($crop){ if($w < $width or $h < $height) return "Picture is too small!"; $ratio = max($width/$w, $height/$h); $h = $height / $ratio; $x = ($w - $width / $ratio) / 2; $w = $width / $ratio; } else{ if($w < $width and $h < $height) return "Picture is too small!"; $ratio = min($width/$w, $height/$h); $width = $w * $ratio; $height = $h * $ratio; $x = 0; } $new = imagecreatetruecolor($width, $height); // preserve transparency if($type == "gif" or $type == "png"){ imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); imagealphablending($new, false); imagesavealpha($new, true); } imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h); switch($type){ case 'bmp': imagewbmp($new, $dst); break; case 'gif': imagegif($new, $dst); break; case 'jpg': imagejpeg($new, $dst); break; case 'png': imagepng($new, $dst); break; } return true; } ?> Example that I use when uploading new images to the server. This saves the original picture in the form: original.type and creates a new thumbnail: 100x100.type <?php $pic_type = strtolower(strrchr($picture['name'],".")); $pic_name = "original$pic_type"; move_uploaded_file($picture['tmp_name'], $pic_name); if (true !== ($pic_error = @image_resize($pic_name, "100x100$pic_type", 100, 100, 1))) { echo $pic_error; unlink($pic_name); } else echo "OK!"; ?> Cheers! ---- Server IP: 87.124.126.135 Probable Submitter: 138.251.95.1 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- https://master.php.net/note/edit/104028 Del: integrated -- https://master.php.net/note/delete/104028/integrated Del: useless -- https://master.php.net/note/delete/104028/useless Del: bad code -- https://master.php.net/note/delete/104028/bad+code Del: spam -- https://master.php.net/note/delete/104028/spam Del: non-english -- https://master.php.net/note/delete/104028/non-english Del: in docs -- https://master.php.net/note/delete/104028/in+docs Del: other reasons-- https://master.php.net/note/delete/104028 Reject -- https://master.php.net/note/reject/104028 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179845) next »