note 97030 added to function.imagecopyresampled

From: Date: Mon, 29 Mar 2010 14:46:28 +0000
Subject: note 97030 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-167489@lists.php.net to get a copy of this message
@ seifer@loveletslive.com function: man this is a really great one, it spared me a lot of time, but anyway there are few things that I've changed a little bit and rewriten it: 1. support for gif and png images (you can add tiffs and other if you need to, I ain't gonna play w/that) 2. one of the last steps when you use imagecopyresampled the second time - in my case it makes the images distorted so I used copyimage here 3. it works fine combined with markglibres@yahoo.com setTransparency function for png's and gif's - you have final thumbnail making tool then. To summarize for people that might want to use it - this one creates thumbnail of desired dimensions from image (whether it is bigger or smaller then desired thumbnail) - there's support for jpg, gif and png (add more if you need to). The function scales images, crops area of desired dimensions and saves the thumbnail. So here it goes with those changes, it can be used right after the big image is uploaded by user, user should be limited in dimension of uploaded image - let's say max. 800x600 for example: <?php ## markglibres@yahoo.com setTransparency function function setTransparency($new_image, $image_source) { $transparencyIndex = imagecolortransparent($image_source); $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); if ($transparencyIndex >= 0) { $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex); } $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); imagefill($new_image, 0, 0, $transparencyIndex); imagecolortransparent($new_image, $transparencyIndex); } ## createCroppedThumbs (modified seifer@loveletslive.com function) # $sourceImg - source image # $dirPath - directory in which the thumb will be placed # $thumbNamePart - if you uploaded the file with the name 'myimg.png' this is 'myimg' # $thumbWidth - the desired thumbnail width # $thumbHeight - the desired thumbnail height function createCroppedThumbs($sourceImg, $dirPath, $thumbNamePart, $thumbWidth, $thumbHeight) { list($sourceWidth, $sourceHeight, $sourceMimeType) = @getimagesize($sourceImg); //width, height and mime of image you need to scale $sourceRatio = $sourceWidth/$sourceHeight; //ratio of source image $thumbLocation = $dirPath.$thumbNamePart.'_thumb.jpg'; //final location and name of the thumb $thumbRatio = $thumbWidth/$thumbHeight; //ratio of thumbnail $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); //create blank image //depending on mime type choose function switch($sourceMimeType) { case '1': $source = imagecreatefromgif($sourceImg); //gif break; case '2': $source = imagecreatefromjpeg($sourceImg); //jpg(jpeg) break; case '3': $source = imagecreatefrompng($sourceImg); //png break; default: $source = imagecreatefromjpeg($sourceImg); break; }//end of choosing function //temp. dimensions if ($thumbRatio > $sourceRatio) {//too tall img including square $tempHeight = $thumbWidth/$sourceRatio; $tempWidth = $thumbWidth; } else {//too short img $tempHeight = $thumbHeight; $tempWidth = $thumbHeight*$sourceRatio; }//end of temp. dimensions //finds cropping points $cropX = ($tempWidth/2) - ($thumbWidth/2); $cropY = ($tempHeight/2) - ($thumbHeight/2); //create temp. thumb $tempThumb = imagecreatetruecolor(round($tempWidth), round($tempHeight)); //for GIF & PNG if ($sourceMimeType == '1' || $sourceMimeType == '3') { setTransparency($tempThumb, $source); } imagecopyresampled($tempThumb, $source, 0, 0, 0, 0, $tempWidth, $tempHeight, $sourceWidth, $sourceHeight); //create final thumb imagecopy($thumb, $tempThumb, 0, 0, $cropX, $cropY, $tempWidth, $tempHeight); imagejpeg($thumb, $thumbLocation, 85); //save image //free memory imagedestroy($tempThumb); imagedestroy($thumb); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 217.173.187.165 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- https://master.php.net/note/edit/97030 Del: integrated -- https://master.php.net/note/delete/97030/integrated Del: useless -- https://master.php.net/note/delete/97030/useless Del: bad code -- https://master.php.net/note/delete/97030/bad+code Del: spam -- https://master.php.net/note/delete/97030/spam Del: non-english -- https://master.php.net/note/delete/97030/non-english Del: in docs -- https://master.php.net/note/delete/97030/in+docs Del: other reasons-- https://master.php.net/note/delete/97030 Reject -- https://master.php.net/note/reject/97030 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167489) next »