note 55740 added to function.imagecopyresampled

From: Date: Fri, 12 Aug 2005 00:19:55 +0000
Subject: note 55740 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-93472@lists.php.net to get a copy of this message
Here is my own custom utility which I call "Thumbnailer" (I know it is cheesy). It's rather short, and it will take JPEG (you can change that easily), and format it to a specified output width and height as a PNG (or whatever else you choose): <?php //Dimensions $outWidth=100; $outHeight=75; //Do it $imgSrc=imagecreatefromjpeg("[source]"); $srcWidth=imagesx($imgSrc); $srcHeight=imagesy($imgSrc); //Make thumbnails $imgOut=imagecreatetruecolor($outWidth,$outHeight); imagerectangle($imgOut,0,0,$outWidth,$outHeight, imagecolorallocate($imgOut,0,0,0)); //Copy them proportionatly $dx=0; $dy=0; $dw=$outWidth; $dh=$outHeight; if ($outWidth*$srcHeight!=$outHeight*$srcWidth) { //Non-proportional, cut-off //Which dimensions is larger if ($srcWidth>$srcHeight) { //Empty space on top and bottom $dw=$outWidth; $dh=($dw*$srcHeight)/$srcWidth; $dy=($outHeight-$dh)/2; } else { //Empty space on left and right $dh=$outHeight; $dw=($dh*$srcWidth)/$srcHeight; $dx=($outWidth-$dw)/2; } } imagecopyresampled($imgOut,$imgSrc,$dx,$dy,0,0, $dw,$dh,$srcWidth,$srcHeight); //imagecopyresized for lower quality //Create the thumbnail and destroy everything else imagepng($imgOut,"[out]"); echo "Image created successfully: $outWidth x $outHeight, ".round(filesize("[source]")/1024)." KB<br>"; imagedestroy($imgSrc); imagedestroy($imgOut); ?> I just use this as a loop to create thumbnails for a series of ordered images. ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55740 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55740&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55740&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93472) next »