image - PHP imagecopyresampled problem with ratio, resizing and cropping -
okey writing image upload want force images 795x440px. can rezised have keep aspect ratio can cropped also.
the new images comes out in right size, cropped image original file has wrong ratio, tried diffrent ways can't right.
the image testing right now, original file
http://image.bayimg.com/jaiflaada.jpg
the result cropping
http://image.bayimg.com/jaifmaada.jpg
how right, image gets best size , crops rest?
list($width, $height) = getimagesize($save_dir); $prop = (795 / $width); $height = floor($height * $prop); $new_image = imagecreatetruecolor(795, 440); $bgcolor = imagecolorallocate($new_image, 255,255,255) or die("couldn't allocate color"); imagefill($new_image , 0,0 , $bgcolor) or die("couldnt fill color"); imagecopyresampled($new_image,$source_image,0,0,0,0,795,440,795,$height); imagejpeg($new_image,$new_directory,100);
i that:
public function cropimage($nw, $nh, $source, $stype, $dest) { list($w, $h) = getimagesize($source); switch($stype) { case 'gif': $simg = imagecreatefromgif($source); break; case 'jpg': case 'jpeg': $simg = imagecreatefromjpeg($source); break; case 'png': $simg = imagecreatefrompng($source); break; } $dimg = imagecreatetruecolor($nw, $nh); $white = imagecolorallocate($dimg, 255, 255, 255); imagefill($dimg, 1, 1, $white); $wm = $w/$nw; $hm = $h/$nh; $h_height = $nh/2; $w_height = $nw/2; if($w > $h) { $adjusted_width = $w / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h); } elseif(($w < $h) || ($w == $h)) { $adjusted_height = $h / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } if(imagejpeg($dimg, $dest, 70)) return true; else die("cropimage: error."); }
Comments
Post a Comment