note 74789 added to function.imagejpeg

From: Date: Fri, 27 Apr 2007 14:53:16 +0000
Subject: note 74789 added to function.imagejpeg
Groups: php.notes 
Request: Send a blank email to php-notes+get-125918@lists.php.net to get a copy of this message
This is a function that I had developed for a CMS. The idea is that it runs from an upload form. The user uploads a hi-res image, which is copied to the server, a low res one is created (half the width/height of hi res) and a thumbnail is created. The image is resized matching the smallest side of the image to the thumbnail size (in this example it is 70px). The other side is then resized proportionally (which will end up being more than 70px) and cropped and centered so that it too is 70px. There is a mixture of imagecopyresized and imagecopyresampled, i have chosen the ones that work best in this situation but imagecopyresampled gives a much nicer image. Hope this helps someone. $dest_hires = "../images/artistsphotos/".$imageName."h.jpg"; $dest_lowres = "../images/artistsphotos/".$imageName."l.jpg"; $dest_thumbnail = "../images/artistsphotos/".$imageName."i.jpg"; /*upload full size image to site - if it is hi res we do not want to manipulate it want to leave all qualit in tact.*/ move_uploaded_file($_FILES["fileartimg"]["tmp_name"],"$dest_hires"); $ims = getimagesize($dest_hires); //now we have dimensions of original image... /******creating lower res image******/ $newwidth=ceil($ims[0]/2);//half the width of original file - use ceil() to avoid decimals. $newheight=ceil($ims[1]/2);//half the height $img = imagecreatetruecolor($newwidth,$newheight); //low res img - always use truecolor to prevent any 'wierd' colour effects. $org_img = imagecreatefromjpeg($dest_hires); //load in hi res imagecopyresized($img, $org_img, 0, 0, 0, 0, $newwidth, $newheight, $ims[0], $ims[1]); imagejpeg($img,$dest_lowres,80);//save to file low res img. imagedestroy($img); /******creating thumbnail******/ //see which is bigger, x or y axis, then resize smaller side to 70. //$resizewidth is the temporary width of the thumbnail, it is in fact the width when it has been resized //using the apect ration and before cropping, at this stage the thumb will not be 70px X 70px //unless it is a square. Same applies to height ($resizeheight). //$thumbx and $thumby are the positions of the cropping are for thumbnail, these are calculated //so that the cropped image is centered. if ($ims[0]>$ims[1]) {//then the width is bigger $aspectRatio = $ims[1]/70; $resizewidth=ceil($ims[0]/$aspectRatio); $resizeheight=70; $thumbx=ceil(($resizewidth - $thumbwidth)/2); $thumby=0; } else if ($ims[0]<$ims[1]) {//then the height is bigger $aspectRatio = $ims[0]/70; $resizewidth=70; $resizeheight=ceil($ims[1]/$aspectRatio); $thumbx=0; $thumby=ceil(($resizeheight - $thumbheight)/2); } else if ($ims[0]==$ims[1]) {//then we have a perfect square. $resizewidth=70; $resizeheight=70; $thumbx=0; $thumby=0; } $img = imagecreatetruecolor($resizewidth,$resizeheight); $org_img = imagecreatefromjpeg($dest_lowres); //this is the almost thumbnail sized image with everything resized to ratio imagecopyresampled($img, $org_img, 0, 0, 0, 0, $resizewidth, $resizeheight, $newwidth, $newheight); $img2 = imagecreatetruecolor($thumbwidth,$thumbheight); //this is the thumbnail image, where the above is cropped. imagecopyresized($img2, $img, 0, 0, $thumbx, $thumby, $resizewidth, $resizeheight, $resizewidth, $resizeheight); imagejpeg($img2,$dest_thumbnail,100); imagedestroy($img); imagedestroy($img2); ---- Server IP: 194.145.210.3 Probable Submitter: 86.129.150.36 ---- Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php Edit -- https://master.php.net/note/edit/74789 Del: integrated -- https://master.php.net/note/delete/74789/integrated Del: useless -- https://master.php.net/note/delete/74789/useless Del: bad code -- https://master.php.net/note/delete/74789/bad+code Del: spam -- https://master.php.net/note/delete/74789/spam Del: non-english -- https://master.php.net/note/delete/74789/non-english Del: in docs -- https://master.php.net/note/delete/74789/in+docs Del: other reasons-- https://master.php.net/note/delete/74789 Reject -- https://master.php.net/note/reject/74789 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125918) next »