note 55692 added to function.imagecopyresized
| From: | FearINC at gmail dot com | Date: | Wed, 10 Aug 2005 17:35:06 +0000 |
| Subject: | note 55692 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93388@lists.php.net to get a copy of this message | ||
I wrote a function not long ago that creates a thumbnail out of a large picture. Unlike other notes
on this page, the code is a function so it can be used many times on the same script. The function
allows the programer to set max height and width and resizes the picture proportionally.
<?php
function saveThumbnail($saveToDir, $imagePath, $imageName, $max_x, $max_y) {
preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageName, $ext);
switch (strtolower($ext[2])) {
case 'jpg' :
case 'jpeg': $im = imagecreatefromjpeg ($imagePath);
break;
case 'gif' : $im = imagecreatefromgif ($imagePath);
break;
case 'png' : $im = imagecreatefrompng ($imagePath);
break;
default : $stop = true;
break;
}
if (!isset($stop)) {
$x = imagesx($im);
$y = imagesy($im);
if (($max_x/$max_y) < ($x/$y)) {
$save = imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x));
}
else {
$save = imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y));
}
imagecopyresized($save, $im, 0, 0, 0, 0, imagesx($save), imagesy($save), $x, $y);
imagegif($save, "{$saveToDir}{$ext[1]}.gif");
imagedestroy($im);
imagedestroy($save);
}
}
?>
the function for now takes only jpg,gif and png files, but that can easily be changed.
It's an easy to use easy to understand function and I hope it will come useful to someone.
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55692
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55692&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55692&report=yes
Search -- http://master.php.net/manage/user-notes.php