note 71663 deleted from function.imagecopyresized by nlopess

From: Date: Sun, 10 Dec 2006 19:21:27 +0000
Subject: note 71663 deleted from function.imagecopyresized by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-120490@lists.php.net to get a copy of this message
Note Submitter: damasekm at centrum dot cz ---- resizeimage function let you resize image according to your max width and max height. This script is based on the one from kyle dot florence at gmail dot com (16-Sep-2006 03:38). Difference from the original: - supports JPG, PNG and GIF on input and will always create JPG - supports Quality setting - validates that source image exists & is readable - validates that destination directory is writable - no dirs - just whole input/output file path - no thumb generation included (for thumb run it twice with different parameter) <? function resizeimage($in_img, $out_img, $max_w, $max_h, $quality) { // check whether input file exists and out-dir is writable $out_img_path = pathinfo($out_img); if (!is_readable($in_img) || !is_writable($out_img_path['dirname'])) { return false; } //if-not readable // get information about original image list($or_w, $or_h, $or_t) = getimagesize($in_img); // make sure image is a gif, jpeg or png if ($or_t == 1 && preg_match("/.gif/i", strtolower($in_img))) { $or_image = imagecreatefromgif($in_img); } else if ($or_t == 2 && ( preg_match("/.jpg/i", strtolower($in_img)) || preg_match("/.jpeg/i", strtolower($in_img)) )) { $or_image = imagecreatefromjpeg($in_img); } else if ($or_t == 3 && preg_match("/.png/i", strtolower($in_img))) { $or_image = imagecreatefrompng($in_img); } else { return false; } //if-imagetype // obtain the image's ratio $ratio = ($or_h / $or_w); // resize image? if ($or_w > $max_w || $or_h > $max_h) { // resize by height, then width (height dominant) if ($max_h < $max_w) { $rs_h = $max_h; $rs_w = $rs_h / $ratio; } else { $rs_w = $max_w; $rs_h = $ratio * $rs_w; } //resize // copy old image to new image $rs_image = imagecreatetruecolor($rs_w, $rs_h); imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h); } else { $rs_w = $or_w; $rs_h = $or_h; $rs_image = $or_image; } //else-no resizing // generate resized image imagejpeg($rs_image, $out_img, $quality); return true; } //resizeimage ?> Example resizing image and also creating thumbnail: <? resizeimage("upl/pic.jpg", "upl/res-pic.jpg", 500, 500, 95); resizeimage("upl/pic.jpg", "upl/thumb-pic.jpg", 100, 100, 90); ?>

« previous php.notes (#120490) next »