note 43413 added to function.imagecopyresampled

From: Date: Sun, 20 Jun 2004 20:21:10 +0000
Subject: note 43413 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-71410@lists.php.net to get a copy of this message
Here's a very easy to use and very versatile resizing-function that produces a good quality and is quite fast. function imageresize($img,$target="",$width=0,$height=0,$percent=0){ // create an image of the given filetype if (strpos($img,".jpg") !== false or strpos($img,".jpeg") !== false) { $image = ImageCreateFromJpeg($img); $extension = ".jpg"; } elseif (strpos($img,".png") !== false) { $image = ImageCreateFromPng($img); $extension = ".png"; } elseif (strpos($img,".gif") !== false) { $image = ImageCreateFromGif($img); $extension = ".gif"; } $size = getimagesize ($img); // calculate missing values if ($width and !$height) { $height = ($size[1] / $size[0]) * $width; } elseif (!$width and $height) { $width = ($size[0] / $size[1]) * $height; } elseif ($percent) { $width = $size[0] / 100 * $percent; $height = $size[1] / 100 * $percent; } elseif (!$width and !$height and !$percent) { $width = 100; // here you can enter a standard value for actions where no arguments are given $height = ($size[1] / $size[0]) * $width; } $thumb = imagecreatetruecolor ($width, $height); if (function_exists("imageCopyResampled")) { if (!@ImageCopyResampled($thumb, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1])) { ImageCopyResized($thumb, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } } else { ImageCopyResized($thumb, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } //ImageCopyResampleBicubic ($thumb, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); if (!$target) { $target = "temp".$extension; } $return = true; switch ($extension) { case ".jpg": { imagejpeg($thumb, $target, 70); break; } case ".gif": { imagegif($thumb, $target); break; } case ".png": { imagepng($thumb, $target); break; } default: { $return = false; } } // report the success (or fail) of the action return $return; } ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43413 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43413&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43413&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71410) next »