note 81633 added to function.imagecopyresampled

From: Date: Fri, 07 Mar 2008 10:45:46 +0000
Subject: note 81633 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-136813@lists.php.net to get a copy of this message
Here's a simple function to resample one JPEG imagefile to another while keeping aspect ratio of the source within the destination's dimensions. You can also tune the allowable distortion if you end up making too many thumbnails with thin blank areas around them. Should work when enlarging images too. Function returns true if it worked, false if not. function resample_picfile($src, $dst, $w, $h) { // If distortion stretching is within the range below, // then let image be distorted. $lowend = 0.8; $highend = 1.25; $src_img = imagecreatefromjpeg($src); if($src_img) { $dst_img = ImageCreateTrueColor($w, $h); /* if you don't want aspect-preserved images to have a black bkgnd, fill $dst_img with the color of your choice here. */ if($dst_img) { $src_w = imageSX($src_img); $src_h = imageSY($src_img); $scaleX = (float)$w / $src_w; $scaleY = (float)$h / $src_h; $scale = min($scaleX, $scaleY); $dstW = $w; $dstH = $h; $dstX = $dstY = 0; $scaleR = $scaleX / $scaleY; if($scaleR < $lowend || $scaleR > $highend) { $dstW = (int)($scale * $src_w + 0.5); $dstH = (int)($scale * $src_h + 0.5); // Keep pic centered in frame. $dstX = (int)(0.5 * ($w - $dstW)); $dstY = (int)(0.5 * ($h - $dstH)); } imagecopyresampled( $dst_img, $src_img, $dstX, $dstY, 0, 0, $dstW, $dstH, $src_w, $src_h); imagejpeg($dst_img, $dst); imagedestroy($dst_img); } imagedestroy($src_img); return file_exists($dst); } return false; } ---- Server IP: 216.235.15.211 Probable Submitter: 70.68.70.186 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- https://master.php.net/note/edit/81633 Del: integrated -- https://master.php.net/note/delete/81633/integrated Del: useless -- https://master.php.net/note/delete/81633/useless Del: bad code -- https://master.php.net/note/delete/81633/bad+code Del: spam -- https://master.php.net/note/delete/81633/spam Del: non-english -- https://master.php.net/note/delete/81633/non-english Del: in docs -- https://master.php.net/note/delete/81633/in+docs Del: other reasons-- https://master.php.net/note/delete/81633 Reject -- https://master.php.net/note/reject/81633 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136813) next »