note 48518 deleted from function.imagecopyresampled by tularis

From: Date: Tue, 28 Dec 2004 09:18:16 +0000
Subject: note 48518 deleted from function.imagecopyresampled by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-82392@lists.php.net to get a copy of this message
Note Submitter: ---- 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 ); ------- ---- Reason: useless note

« previous php.notes (#82392) next »