note 32330 added to function.imagecopyresized

From: Date: Fri, 23 May 2003 17:41:52 +0000
Subject: note 32330 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-48913@lists.php.net to get a copy of this message
Here's a couple of bicubic image resizers that people may find useful. I've used resizers that look at each pixel, break it down into RGB values, average them and then paint a single dot on the destination image. While it gets the job done, it can be very slow. I've just knocked together two functions that still resample but uses internal GD functions for the maths instead. These functions are for those with a modern PHP but with only version 1.x of GD which they cannot upgrade, usually because someone else hosts the site. People with GD 2.x should use imagecopyresampled instead. The first function simply copies a new image half the size of the original, which is what I need to do. The second is expanded to resize to a certain percentage and feel free to develop it further to your needs. I'm resizing images to a quarter of their original size and halving it twice gives better results than a single step. function imagecopyhalfsize($source) { $dimensions = array('srcW'=>imagesx($source), 'srcH'=>imagesy($source), 'dstW'=>floor(imagesx($source)/2), 'dstH'=>floor(imagesy($source)/2)); $images = array(); $offset = array('x'=>array(0,1,0,1), 'y'=>array(0,0,1,1)); for ($n=0; $n<4; $n++) { array_push($images, imagecreate($dimensions['dstW'], $dimensions['dstH'])); imagecopyresized($images[$n], $source, 0, 0, $offset['x'][$n], $offset['y'][$n], $dimensions['dstW'], $dimensions['dstH'], $dimensions['srcW']-1, $dimensions['srcH']-1); } $dest = imagecreate($dimensions['dstW'], $dimensions['dstH']); for ($n=0; $n<4; $n++) { imagecopymerge($dest, $images[$n], 0, 0, 0, 0, $dimensions['dstW'], $dimensions['dstH'], 100/($n+1)); imagedestroy($images[$n]); } return $dest; } function imagecopypercentsize($source, $xpercent, $ypercent = 'same as x') { if ($ypercent == 'same as x') $ypercent = $xpercent; $dimensions = array('srcW'=>imagesx($source), 'srcH'=>imagesy($source), 'dstW'=>floor(imagesx($source)*$xpercent/100), 'dstH'=>floor(imagesy($source)*$ypercent/100), 'dstX'=>floor(100/$xpercent/2), 'dstY'=>floor(100/$ypercent/2)); $images = array(); $offset = array('x'=>array(0,$dimensions['dstX'],0,$dimensions['dstX']), 'y'=>array(0,0,$dimensions['dstY'],$dimensions['dstY'])); for ($n=0; $n<4; $n++) { array_push($images, imagecreate($dimensions['dstW'], $dimensions['dstH'])); imagecopyresized($images[$n], $source, 0, 0, $offset['x'][$n], $offset['y'][$n], $dimensions['dstW'], $dimensions['dstH'], $dimensions['srcW']-$dimensions['dstX'], $dimensions['srcH']-$dimensions['dstY']); } $dest = imagecreate($dimensions['dstW'], $dimensions['dstH']); for ($n=0; $n<4; $n++) { imagecopymerge($dest, $images[$n], 0, 0, 0, 0, $dimensions['dstW'], $dimensions['dstH'], 100/($n+1)); imagedestroy($images[$n]); } return $dest; } ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32330 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32330&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32330&report=yes

« previous php.notes (#48913) next »