note 50604 added to function.imagecopyresampled

From: Date: Fri, 04 Mar 2005 14:21:34 +0000
Subject: note 50604 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-85920@lists.php.net to get a copy of this message
Here's a function i made that will force a given set of constraints on an image (usrfull if you want to make thumbnails that are all the same size) while keeping aspect ratio. It resizes, then crops the extra off the top or bottom. function forceConstraints($srcFile, $srcType, $dstType, $dstWidth, $dstHeight, $dstPath) { if ($srcType == "image/jpeg") $handle = @imagecreatefromjpeg($srcFile); else if ($srcType == "image/png") $handle = @imagecreatefrompng($srcFile); else if ($srcType == "image/gif") $handle = @imagecreatefromgif($srcFile); else return false; if (!$handle) return false; $srcWidth = @imagesx($handle); $srcHeight = @imagesy($handle); if ($srcWidth >= $dstWidth && $srcHeight >= $dstHeight) { $newHandle = @imagecreatetruecolor($dstWidth, $dstHeight); if (!$newHandle) return false; if($srcHeight < $srcWidth) { $ratio = (double)($srcHeight / $dstHeight); $cpyWidth = round($dstWidth * $ratio); if ($cpyWidth > $srcWidth) { $ratio = (double)($srcWidth / $dstWidth); $cpyWidth = $srcWidth; $cpyHeight = round($dstHeight * $ratio); $xOffset = 0; $yOffset = round(($srcHeight - $cpyHeight) / 2); } else { $cpyHeight = $srcHeight; $xOffset = round(($srcWidth - $cpyWidth) / 2); $yOffset = 0; } } else { $ratio = (double)($srcWidth / $dstWidth); $cpyHeight = round($dstHeight * $ratio); if ($cpyHeight > $srcHeight) { $ratio = (double)($srcHeight / $dstHeight); $cpyHeight = $srcHeight; $cpyWidth = round($dstWidth * $ratio); $xOffset = round(($srcWidth - $cpyWidth) / 2); $yOffset = 0; } else { $cpyWidth = $srcWidth; $xOffset = 0; $yOffset = round(($srcHeight - $cpyHeight) / 2); } } if (!@imagecopyresampled($newHandle, $handle, 0, 0, $xOffset, $yOffset, $dstWidth, $dstHeight, $cpyWidth, $cpyHeight)) return false; @imagedestroy($handle); if ($dstType == "png") @imagepng($newHandle, $dstPath.".png"); else if ($dstType == "jpg") @imagejpeg($newHandle, $dstPath.".jpg", 90); else if ($dstType == "gif") @imagegif($newHandle, $dstPath.".gif"); else return false; @imagedestroy($newHandle); return true; } else { return "Sorry, that image is too small. The image must be at least ".$dstWidth."x".$dstHeight." pixels in size."; } } ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50604 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50604&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50604&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50604&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50604&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50604&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50604&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85920) next »