note 71663 added to function.imagecopyresized

From: Date: Sun, 10 Dec 2006 19:04:33 +0000
Subject: note 71663 added to function.imagecopyresized
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-120488@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 195.136.184.34 Probable Submitter: 82.210.180.149 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- https://master.php.net/note/edit/71663 Del: integrated -- https://master.php.net/note/delete/71663/integrated Del: useless -- https://master.php.net/note/delete/71663/useless Del: bad code -- https://master.php.net/note/delete/71663/bad+code Del: spam -- https://master.php.net/note/delete/71663/spam Del: non-english -- https://master.php.net/note/delete/71663/non-english Del: in docs -- https://master.php.net/note/delete/71663/in+docs Del: other reasons-- https://master.php.net/note/delete/71663 Reject -- https://master.php.net/note/reject/71663 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120488) next »