为什么旋转的图像具有相同的宽高比?

时间:2012-07-11 00:28:51

标签: php gd

我正在使用jquery文件上传的一些PHP代码,我试图在保存之前旋转图像。以下是我的函数调用:

public function CreateThumb($file_name, $options){

    $file_path = $options['src_dir'].$file_name;
    $new_file_path = $options['dst_dir'].$file_name;

    list($img_width, $img_height) = @getimagesize($file_path);

    if (!$img_width || !$img_height) {
        return false;
    }
    $scale = min(
            $options['max_width'] / $img_width,
            $options['max_height'] / $img_height
    );

    $new_width = $img_width * $scale;
    $new_height = $img_height * $scale;
    $new_img = @imagecreatetruecolor($new_width, $new_height);

    switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'jpg':
        case 'jpeg':
            $src_img = @imagecreatefromjpeg($file_path);
            $write_image = 'imagejpeg';
            $image_quality = isset($options['jpeg_quality']) ?
            $options['jpeg_quality'] : 95;
            break;
        case 'gif':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            $src_img = @imagecreatefromgif($file_path);
            $write_image = 'imagegif';
            $image_quality = null;
            break;
        case 'png':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            @imagealphablending($new_img, false);
            @imagesavealpha($new_img, true);
            $src_img = @imagecreatefrompng($file_path);
            $write_image = 'imagepng';
            $image_quality = isset($options['png_quality']) ?
            $options['png_quality'] : 9;
            break;
        default:
            $src_img = null;
    }

    $src_img = imagerotate($src_img, 90, 0) ;

    $success = $src_img && @imagecopyresampled(
            $new_img,
            $src_img,
            0, 0, 0, 0,
            $new_width,
            $new_height,
            $img_width,
            $img_height
    ) && $write_image($new_img, $new_file_path, $image_quality);

    // Free up memory (imagedestroy does not delete files):
    @imagedestroy($src_img);
    @imagedestroy($new_img);

    return $success;
}

图像会旋转但仍保持原始宽高比并正在裁剪照片。知道我做错了吗?

2 个答案:

答案 0 :(得分:1)

您的问题在于如何设置新值:

$new_width = $img_width * $scale;
$new_height = $img_height * $scale;

应该是,如果旋转90度:

$new_width = $img_height * $scale;    // reverse height and width
$new_height = $img_width * $scale;    // reverse height and width

编辑:在旋转原始图像时,必须颠倒旧的宽度和高度:

$success = $src_img && @imagecopyresampled(
        $new_img,
        $src_img,
        0, 0, 0, 0,
        $new_width,
        $new_height,
        $img_height,    // reverse width and height
        $img_width      // reverse width and height
) && $write_image($new_img, $new_file_path, $image_quality);

答案 1 :(得分:0)

请注意:如果您打算使用那么多@来忽略错误,您可以将error_reporting(0);放在代码的开头。

其次,我看到像素比率没有变化的原因是因为你的$scale是倒退的。除非最大尺寸小于图像尺寸,否则你会说scale = Whole/Part几乎总会给你一个大于1的值(增加像素比)。你希望它是scale = Part/Whole所以它的小数比(即100px / 400px,scale = .25)。

祝你好运!