note 91817 added to function.imagecopyresampled
| From: | Anonymous at osu1 dot php dot net | Date: | Fri, 26 Jun 2009 14:48:04 +0000 |
| Subject: | note 91817 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157545@lists.php.net to get a copy of this message | ||
This is z3n666's code, only with a couple fixes...
@z3n666 - Your img_resizer code is great, there is a bug though... you specify "peg"
instead of "jpe".
---SNIP---
function img_resizer($src,$quality,$w,$h,$saveas) {
/* v2.5 with auto crop */
$r=1;
$e=strtolower(substr($src,strrpos($src,".")+1,3));
if (($e == "jpg") || ($e == "jpe")) {
---SNIP---
Also, I added the following so that it will not BLOW up images that already fit in the dimesions.
---SNIP---
if ($r) {
list($width,$height)=getimagesize($src);
if ($width <= $w && $height <= $h)
{
copy($src, $saveas);
return 1;
}
---SNIP---
Here is the full function, I changed the version also:
<?php
function _ckdir($fn) {
if (strpos($fn,"/") !== false) {
$p=substr($fn,0,strrpos($fn,"/"));
if (!is_dir($p)) {
_o("Mkdir: ".$p);
mkdir($p,777,true);
}
}
}
function img_resizer($src,$quality,$w,$h,$saveas) {
/* v2.6 with auto crop */
$r=1;
$e=strtolower(substr($src,strrpos($src,".")+1,3));
if (($e == "jpg") || ($e == "peg")) {
$OldImage=ImageCreateFromJpeg($src) or $r=0;
} elseif ($e == "gif") {
$OldImage=ImageCreateFromGif($src) or $r=0;
} elseif ($e == "bmp") {
$OldImage=ImageCreateFromwbmp($src) or $r=0;
} elseif ($e == "png") {
$OldImage=ImageCreateFromPng($src) or $r=0;
} else {
_o("Not a Valid Image! (".$e.") -- ".$src);$r=0;
}
if ($r) {
list($width,$height)=getimagesize($src);
// check if ratios match
$_ratio=array($width/$height,$w/$h);
if ($_ratio[0] != $_ratio[1]) { // crop image
// find the right scale to use
$_scale=min((float)($width/$w),(float)($height/$h));
// coords to crop
$cropX=(float)($width-($_scale*$w));
$cropY=(float)($height-($_scale*$h));
// cropped image size
$cropW=(float)($width-$cropX);
$cropH=(float)($height-$cropY);
$crop=ImageCreateTrueColor($cropW,$cropH);
// crop the middle part of the image to fit proportions
ImageCopy(
$crop,
$OldImage,
0,
0,
(int)($cropX/2),
(int)($cropY/2),
$cropW,
$cropH
);
}
// do the thumbnail
$NewThumb=ImageCreateTrueColor($w,$h);
if (isset($crop)) { // been cropped
ImageCopyResampled(
$NewThumb,
$crop,
0,
0,
0,
0,
$w,
$h,
$cropW,
$cropH
);
ImageDestroy($crop);
} else { // ratio match, regular resize
ImageCopyResampled(
$NewThumb,
$OldImage,
0,
0,
0,
0,
$w,
$h,
$width,
$height
);
}
_ckdir($saveas);
ImageJpeg($NewThumb,$saveas,$quality);
ImageDestroy($NewThumb);
ImageDestroy($OldImage);
}
return $r;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 99.247.237.46
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/91817
Del: integrated -- https://master.php.net/note/delete/91817/integrated
Del: useless -- https://master.php.net/note/delete/91817/useless
Del: bad code -- https://master.php.net/note/delete/91817/bad+code
Del: spam -- https://master.php.net/note/delete/91817/spam
Del: non-english -- https://master.php.net/note/delete/91817/non-english
Del: in docs -- https://master.php.net/note/delete/91817/in+docs
Del: other reasons-- https://master.php.net/note/delete/91817
Reject -- https://master.php.net/note/reject/91817
Search -- https://master.php.net/manage/user-notes.php