Following code crop images baased on lower dimention (Width / Height)
Following is the funtion with image url,
public function croppThis($target_url) {
$this->jpegImgCrop($target_url);
}
$target_url - is Name of image.
Crop image bases on less width / Height:
public function jpegImgCrop($target_url) {//support
$image = imagecreatefromjpeg($target_url);
$filename = $target_url;
$width = imagesx($image);
$height = imagesy($image);
$image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP
if($width==$height) {
$thumb_width = $width;
$thumb_height = $height;
} elseif($width<$height) {
$thumb_width = $width;
$thumb_height = $width;
} elseif($width>$height) {
$thumb_width = $height;
$thumb_height = $height;
} else {
$thumb_width = 150;
$thumb_height = 150;
}
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
No comments:
Post a Comment