0

我正在使用来自 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;
}

图像被旋转,但仍保持其原始纵横比并裁剪照片。知道我做错了什么吗?

4

2 回答 2

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);
于 2012-07-11T01:02:22.937 回答
0

请注意:如果您要使用这么多 @ 来忽略错误,您不妨将其放在error_reporting(0);代码的开头。

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

祝你好运!

于 2012-07-11T00:37:15.737 回答