note 48518 added to function.imagecopyresampled

From: Date: Tue, 28 Dec 2004 03:34:18 +0000
Subject: note 48518 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-82382@lists.php.net to get a copy of this message
carlos [at] wfmh [dot] org [dot] pl If you want to keep the aspect ratio while scaling the image (i.e. to ensure your thumbnails won't suck) here's the solution. $DestWidth and $DestHeigth are target dimension while $ImgWidth and $ImgHeight are current (source) ones (source image is already loaded into $img resource). Afterall $target will be our newly rescaled image which you can either save (with i.e. imagepng() or keep processing further). --------- $x = $y = 0; $NewWidth = $DestWidth; $NewHeight = $DestHeight; $DestAspectRatio = $DestWidth/$DestHeight; $ImgAspectRatio = $ImgWidth / $ImgHeight; if( $ImgAspectRatio > $DestAspectRatio) { $NewHeight = $DestWidth / $ImgWidth * $ImgHeight; $y = ($DestHeight - $NewHeight) / 2; } else { if( $ImgAspectRatio < $DestAspectRatio ) { $NewWidth = $DestHeight / $ImgHeight * $ImgWidth; $x = ( $DestWidth - $NewWidth ) / 2; } } $target = imagecreatetruecolor( $DestWidth, $DestHeight ); $bg = imagecolorallocate($target, 0, 0, 0 ); imagefilledrectangle( $target, 0, 0, $DestWidth, $DestHeight, $bg ); imagecopyresampled( $target, $img, $x, $y, 0, 0, $NewWidth, $NewHeight, $ImgWidth, $ImgHeight ); ------- ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48518 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48518&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48518&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82382) next »