note 58233 added to function.imagecopyresampled
| From: | info at lontronics dot nl | Date: | Fri, 28 Oct 2005 10:08:30 +0000 |
| Subject: | note 58233 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97733@lists.php.net to get a copy of this message | ||
For an internet photoalbum I was looking for a script which I could simply drop in a directory with
images and which would do the rest.
Therefor I am using the following resize script now. I have made the little thumb-part because I
want to have all the same size thumbnails, but with as less as possible distortion. Therefor, images
with other dimensions are resized, but also cut off a little.
Als $pictype = thumb, the extra dimension calculations are used
$width and $height are the new dimensions of $filename, the image
$dest is the directory where the new image must be copied in.
The script:
// Resize function
function resize($filename, $dest, $width, $height, $pictype)
{
$format = strtolower(substr(strrchr($filename,"."),1));
switch($format)
{
case 'gif' :
$type ="gif";
$img = imagecreatefromgif($filename);
break;
case 'png' :
$type ="png";
$img = imagecreatefrompng($filename);
break;
case 'jpg' :
$type ="jpg";
$img = imagecreatefromjpeg($filename);
break;
case 'jpeg' :
$type ="jpg";
$img = imagecreatefromjpeg($filename);
break;
default :
die ("ERROR; UNSUPPORTED IMAGE TYPE");
break;
}
list($org_width, $org_height) = getimagesize($filename);
$xoffset = 0;
$yoffset = 0;
if ($pictype == "thumb") // To minimize destortion
{
if ($org_width / $width > $org_height/ $height)
{
$xtmp = $org_width;
$xratio = 1-((($org_width/$org_height)-($width/$height))/2);
$org_width = $org_width * $xratio;
$xoffset = ($xtmp - $org_width)/2;
}
elseif ($org_height/ $height > $org_width / $width)
{
$ytmp = $org_height;
$yratio = 1-((($width/$height)-($org_width/$org_height))/2);
$org_height = $org_height * $yratio;
$yoffset = ($ytmp - $org_height)/2;
}
}
$img_n=imagecreatetruecolor ($width, $height);
imagecopyresampled($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $org_width,
$org_height);
if($type=="gif")
{
imagegif($img_n, $dest);
}
elseif($type=="jpg")
{
imagejpeg($img_n, $dest);
}
elseif($type=="png")
{
imagepng($img_n, $dest);
}
elseif($type=="bmp")
{
imagewbmp($img_n, $dest);
}
Return true;
}
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58233
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58233&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58233&report=yes
Search -- http://master.php.net/manage/user-notes.php