PHP GD调整大小并裁剪?

时间:2016-02-16 20:39:05

标签: php image-processing gd image-resizing

这是我的代码的一部分,我希望图像为900 x 600.目前它只保持一个尺寸正确,另一个尺寸更小,如749x600。

我需要它来缩放图像然后裁剪到大的一面,我该如何实现呢?

define('THUMBNAIL_IMAGE_MAX_WIDTH', 900);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 600);

 $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
    if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
        $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
        $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
    } else {
        $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
        $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
    }
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);

if($end === '.jpg'){

 imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 100);

}else if ($end === '.png') {

  imagepng($thumbnail_gd_image, $thumbnail_image_path, 100);

}else{

     imagegif($thumbnail_gd_image, $thumbnail_image_path, 100);
}

0 个答案:

没有答案