note 61864 deleted from function.imagecopyresized by mazzanet

From: Date: Tue, 14 Feb 2006 09:55:12 +0000
Subject: note 61864 deleted from function.imagecopyresized by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-103671@lists.php.net to get a copy of this message
Note Submitter: sinasalek at yahoo dot com ---- 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);

« previous php.notes (#103671) next »