note 61864 added to function.imagecopyresized

From: Date: Tue, 14 Feb 2006 07:06:42 +0000
Subject: note 61864 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-103664@lists.php.net to get a copy of this message
I wrote a new image cropper, because when i wanted to crop an image to different aspect, it simply stretched , to fit with new aspect! here is the solution , with this function you can easily crop any image to any size, without changing aspect. function flexible_image_cropper($source_location, $desc_location=null,$crop_size_w=null,$crop_size_h=null) { list($src_w, $src_h) = getimagesize($source_location); $image_source = imagecreatefromjpeg($source_location); $image_desc = imagecreatetruecolor($crop_size_w,$crop_size_h); /* if ($crop_size_w>$crop_size_h) {$my_crop_size_w=null;} elseif ($crop_size_h>$crop_size_w) {$my_crop_size_h=null; if (is_null($my_crop_size_h) and !is_null($my_crop_size_w)) { $my_crop_size_h=$src_h*$my_crop_size_w/$src_w; } elseif (!is_null($my_crop_size_h)) { $my_crop_size_w=$src_w*$my_crop_size_h/$src_h; } */ if ($src_w<$src_h) {$my_crop_size_h=$src_h*$crop_size_w/$src_w;} else {$my_crop_size_h=$crop_size_h;} if ($src_h<$src_w) {$my_crop_size_w=$src_w*$crop_size_h/$src_h;} else {$my_crop_size_w=$crop_size_w;} // echo "($my_crop_size_w-$my_crop_size_h)"; if ($my_crop_size_w>$crop_size_w) {$additional_x=round(($my_crop_size_w-$crop_size_w)/2);} else {$additional_x=0;} if ($my_crop_size_h>$crop_size_h) {$additional_y=round(($my_crop_size_h-$crop_size_h)/2);} else {$additional_y=0;} $off_x=round($src_w/2)-round($my_crop_size_w/2); $off_y=round($src_h/2)-round($my_crop_size_h/2)+$additional_y; $off_w=(round($src_w/2)+round($my_crop_size_w/2))-$off_x; $off_h=(round($src_h/2)+round($my_crop_size_h/2))-$off_y; imagecopyresampled($image_desc, $image_source,0, 0, $off_x, $off_y, $my_crop_size_w, $my_crop_size_h, $off_w, $off_h); if (!is_null($desc_location)) imagejpeg($image_desc, $desc_location); else imagejpeg($image_desc); } SAMPLE : $source_file='gallery_files/IMG_9736.jpg'; $desc_file='gallery_files/test.jpg'; flexible_image_cropper($source_file,$desc_file,200,200); $source = imagecreatefromjpeg($desc_file); ---- Server IP: 64.71.164.2 Probable Submitter: 84.241.17.59 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61864 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61864&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61864&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103664) next »