note 33613 added to function.imagecopyresampled

From: Date: Tue, 01 Jul 2003 22:09:24 +0000
Subject: note 33613 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-51321@lists.php.net to get a copy of this message
imagecopyresampled will shrink an image smoothly, but if you try to use it to enlarge an image, the result will be blocky, like in imagecopyresized. The following function will enlarge an image more smoothly using bilinear resampling. function imagecopyenlarged ( $dst_im , $src_im , $dstX , $dstY , $srcX , $srcY , $dstW , $dstH , $srcW , $srcH ) { for ( $y = $dstY ; $y < $dstH ; $y ++ ) { $tempy = ( $y - $dstY ) * $srcH / $dstH + $srcY; $tempyd = $tempy - floor ( $tempy ); $tempyu = 1 - $tempyd; $tempy = floor ( $tempy ); for ( $x = $dstX ; $x < $dstW ; $x ++ ) { $tempx = ( $x - $dstX ) * $srcW / $dstW + $srcX; $tempxr = $tempx - floor ( $tempx ); $tempxl = 1 - $tempxr; $tempx = floor ( $tempx ); $tempq = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx , $tempy ) ); $tempw = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx + 1 , $tempy ) ); $tempa = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx , $tempy + 1 ) ); $temps = imagecolorsforindex ( $src_im , imagecolorat ( $src_im , $tempx + 1 , $tempy + 1 ) ); $tempq['red'] = $tempq['red'] * $tempxl + $tempw['red'] * $tempxr; $tempq['green'] = $tempq['green'] * $tempxl + $tempw['green'] * $tempxr; $tempq['blue'] = $tempq['blue'] * $tempxl + $tempw['blue'] * $tempxr; $tempa['red'] = $tempa['red'] * $tempxl + $temps['red'] * $tempxr; $tempa['green'] = $tempa['green'] * $tempxl + $temps['green'] * $tempxr; $tempa['blue'] = $tempa['blue'] * $tempxl + $temps['blue'] * $tempxr; $tempq['red'] = $tempq['red'] * $tempyu + $tempa['red'] * $tempyd; $tempq['green'] = $tempq['green'] * $tempyu + $tempa['green'] * $tempyd; $tempq['blue'] = $tempq['blue'] * $tempyu + $tempa['blue'] * $tempyd; imagesetpixel ( $dst_im , $x , $y , imagecolorallocate ( $dst_im , $tempq['red'] , $tempq['green'] , $tempq['blue'] ) ); } } } Note that this function is only for enlarging an image. Depending on the application, you may need to have your script detect whether it will be enlarging or shrinking the image, and use the respective function. ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33613 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33613&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33613&report=yes

« previous php.notes (#51321) next »