note 88263 added to function.imagecopyresampled
| From: | Warren at osu1 dot php dot net | Date: | Sun, 18 Jan 2009 07:15:06 +0000 |
| Subject: | note 88263 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149377@lists.php.net to get a copy of this message | ||
This is a function I wrote for making thumbnails - it will accept a source *image resource* and
destination *path*, plus the max dimension and whether the thumbnail should be square.
I chose to accept a resource as the source to make it a bit more efficient to create multiple sizes.
For example:
$src_im=@imagecreatefromjpeg($pathtofile);
$large = resize($src_im,$destination_large,1024);
@imagedestroy($src_im);
$medium = resize($large,$destination_medium,500);
@imagedestroy($large);
$small = resize($medium,$destination_small,125);
@imagedestroy($medium);
$square = resize($small,$destination_square,75);
@imagedestroy($small);
@imagedestroy($square);
function resize($src_im, $dpath, $maxd, $square=false) {
$src_width = imagesx($src_im);
$src_height = imagesy($src_im);
$src_w=$src_width;
$src_h=$src_height;
$src_x=0;
$src_y=0;
if($square){
$dst_w = $maxd;
$dst_h = $maxd;
if($src_width>$src_height){
$src_x = ceil(($src_width-$src_height)/2);
$src_w=$src_height;
$src_h=$src_height;
}else{
$src_y = ceil(($src_height-$src_width)/2);
$src_w=$src_width;
$src_h=$src_width;
}
}else{
if($src_width>$src_height){
$dst_w=$maxd;
$dst_h=floor($src_height*($dst_w/$src_width));
}else{
$dst_h=$maxd;
$dst_w=floor($src_width*($dst_h/$src_height));
}
}
$dst_im=@imagecreatetruecolor($dst_w,$dst_h);
@imagecopyresampled($dst_im, $src_im, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
@imagejpeg($dst_im,$dpath);
return $dst_im;
}
----
Server IP: 69.147.83.197
Probable Submitter: 72.223.114.207
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/88263
Del: integrated -- https://master.php.net/note/delete/88263/integrated
Del: useless -- https://master.php.net/note/delete/88263/useless
Del: bad code -- https://master.php.net/note/delete/88263/bad+code
Del: spam -- https://master.php.net/note/delete/88263/spam
Del: non-english -- https://master.php.net/note/delete/88263/non-english
Del: in docs -- https://master.php.net/note/delete/88263/in+docs
Del: other reasons-- https://master.php.net/note/delete/88263
Reject -- https://master.php.net/note/reject/88263
Search -- https://master.php.net/manage/user-notes.php