note 69042 added to function.imagecopyresized

From: Date: Mon, 21 Aug 2006 11:32:01 +0000
Subject: note 69042 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-116118@lists.php.net to get a copy of this message
Regarding the note and function of kyle.florence on August 3rd 2006: I tried to use his function resizejpeg() for resizing images in my gallery. As far as I can tell it contains a small bug. Resizing worked fine as long as I had the same maximum width and maximum height specified. Wanting all thumbnails to have the same height - so my images would appear in a straight line on my website both in portrait format and in landscape format - I soon encountered the problem that resizing with different values of maximum width and maximum height would not work proberly. If you are using the script change the following 2 lines where the resized width and the resized height are calculated: <?php $rs_h = $ratio * $max_h; ?> should be: <?php $rs_h = $ratio * $rs_w; ?> and: <?php $rs_w = $max_w / $ratio; ?> should be: <?php $rs_w = $rs_h / $ratio; ?> The following function is based on Kyle Florence's function. I left out the thumbnail-part and rather added the possibiliy of defining a new dir and new filename for the image. If you need to resize and then create a thumbnail just run the function twice. Here, the thumbnail will contain the full picture and not a cutout of the original image. The function supports JPG, GIF and PNG resizing. The quality in case of JPG is given to the function as the last parameter $Quality. The variable names should speak for their usage. <?php function Resize($Dir,$Image,$NewDir,$NewImage,$MaxWidth,$MaxHeight,$Quality) { list($ImageWidth,$ImageHeight,$TypeCode)=getimagesize($Dir.$Image); $ImageType=($TypeCode==1?"gif":($TypeCode==2?"jpeg": ($TypeCode==3?"png":FALSE))); $CreateFunction="imagecreatefrom".$ImageType; $OutputFunction="image".$ImageType; if ($ImageType) { $Ratio=($ImageHeight/$ImageWidth); $ImageSource=$CreateFunction($Dir.$Image); if ($ImageWidth > $MaxWidth || $ImageHeight > $MaxHeight) { if ($ImageWidth > $MaxWidth) { $ResizedWidth=$MaxWidth; $ResizedHeight=$ResizedWidth*$Ratio; } else { $ResizedWidth=$ImageWidth; $ResizedHeight=$ImageHeight; } if ($ResizedHeight > $MaxHeight) { $ResizedHeight=$MaxHeight; $ResizedWidth=$ResizedHeight/$Ratio; } $ResizedImage=imagecreatetruecolor($ResizedWidth,$ResizedHeight); imagecopyresampled($ResizedImage,$ImageSource,0,0,0,0,$ResizedWidth, $ResizedHeight,$ImageWidth,$ImageHeight); } else { $ResizedWidth=$ImageWidth; $ResizedHeight=$ImageHeight; $ResizedImage=$ImageSource; } $OutputFunction($ResizedImage,$NewDir.$NewImage,$Quality); return true; } else return false; } ?> Before calling the function support for JPG, PNG or GIF should be checked. ---- Server IP: 66.163.161.117 Probable Submitter: 84.185.240.41 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- https://master.php.net/note/edit/69042 Del: integrated -- https://master.php.net/note/delete/69042/integrated Del: useless -- https://master.php.net/note/delete/69042/useless Del: bad code -- https://master.php.net/note/delete/69042/bad+code Del: spam -- https://master.php.net/note/delete/69042/spam Del: non-english -- https://master.php.net/note/delete/69042/non-english Del: in docs -- https://master.php.net/note/delete/69042/in+docs Del: other reasons-- https://master.php.net/note/delete/69042 Reject -- https://master.php.net/note/reject/69042 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116118) next »