缩略图图像不旋转

时间:2017-10-08 17:23:56

标签: php

我正在php中为上传的图片创建缩略图。缩略图已创建并正确保存,但在需要时不会旋转。我在许多图像上测试我的代码,但它没有用。这是我的代码,用于创建缩略图并根据其exif数据进行旋转。

//thumb image
$maxDim = 300; 
$filename = $_FILES['file']['name'];
list($width, $height, $type, $attr) = getimagesize($path.$filename); 
if ( $width > $maxDim || $height > $maxDim ) 
{
    $ext = pathinfo($filename, PATHINFO_EXTENSION); //var_dump($ext);
    $target_filename = $path.'thumb_'.$filename; 
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width  = $maxDim;
        $new_height = $maxDim/$ratio;
    } else {
        $new_width =  $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $contents = file_get_contents($path.$filename); 
    $src = imagecreatefromstring($contents); 
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );

    $exif = exif_read_data($path.$filename);
    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 3:
                $dst = imagerotate($dst, 180, 0);
                break;

            case 6:
                $dst = imagerotate($dst, 90, 0);
                break;

            case 8:
                $dst = imagerotate($dst, -90, 0);
                break;
        }
    }
    //check src image type
    if (exif_imagetype($path.$filename ) == IMAGETYPE_JPEG) {
        imagejpeg($dst, $target_filename );
    }
    else if(exif_imagetype($path.$filename ) == IMAGETYPE_PNG){
        imagepng($dst, $target_filename );
    }
    else if(exif_imagetype($path.$filename ) == IMAGETYPE_GIF){
        imagegif($dst, $target_filename );
    }

    imagedestroy( $dst );
}

0 个答案:

没有答案