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/Whole10 進数の比率 (つまり、100px/400px、スケール = .25) です。

幸運を!

于 2012-07-11T00:37:15.737 に答える