note 81898 added to function.imagecopyresampled

From: Date: Tue, 18 Mar 2008 19:41:14 +0000
Subject: note 81898 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-137180@lists.php.net to get a copy of this message
I hated that enlarging an image resulted in giant pixels rather than a smoother look, so I wrote this function. It takes longer, but gives a much nicer look. <?php function imagecopyresampledSMOOTH(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $mult=1.25){ // don't use a $mult that's too close to an int or this function won't make much of a difference $tgt_w = round($src_w * $mult); $tgt_h = round($src_h * $mult); // using $mult <= 1 will make the current step w/h smaller (or the same), don't allow this, always resize by at least 1 pixel larger if($tgt_w <= $src_w){ $tgt_w += 1; } if($tgt_h <= $src_h){ $tgt_h += 1; } // if the current step w/h is larger than the final height, adjust it back to the final size // this check also makes it so that if we are doing a resize to smaller image, it happens in one step (since that's already smooth) if($tgt_w > $dst_w){ $tgt_w = $dst_w; } if($tgt_h > $dst_h){ $tgt_h = $dst_h; } $tmpImg = imagecreatetruecolor($tgt_w, $tgt_h); imagecopyresampled($tmpImg, $src_img, 0, 0, $src_x, $src_y, $tgt_w, $tgt_h, $src_w, $src_h); imagecopy($dst_img, $tmpImg, $dst_x, $dst_y, 0, 0, $tgt_w, $tgt_h); imagedestroy($tmpImg); // as long as the final w/h has not been reached, reep on resizing if($tgt_w < $dst_w OR $tgt_h < $dst_h){ imagecopyresampledSMOOTH($dst_img, $dst_img, $dst_x, $dst_y, $dst_x, $dst_y, $dst_w, $dst_h, $tgt_w, $tgt_h, $mult); } } ?> ---- Server IP: 66.207.199.35 Probable Submitter: 129.97.208.23 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- https://master.php.net/note/edit/81898 Del: integrated -- https://master.php.net/note/delete/81898/integrated Del: useless -- https://master.php.net/note/delete/81898/useless Del: bad code -- https://master.php.net/note/delete/81898/bad+code Del: spam -- https://master.php.net/note/delete/81898/spam Del: non-english -- https://master.php.net/note/delete/81898/non-english Del: in docs -- https://master.php.net/note/delete/81898/in+docs Del: other reasons-- https://master.php.net/note/delete/81898 Reject -- https://master.php.net/note/reject/81898 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137180) next »