You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PHP竖屏图片压缩后自动左转90度问题排查求助

解决相机竖屏图片上传压缩后旋转90度的问题

这个问题的核心原因是相机拍摄的竖屏照片会在EXIF元数据中存储方向信息(Orientation标签),但你使用的GD库(imagecreatefromjpeg这类函数)默认不会自动识别并应用这个方向信息。压缩时GD只会按照图片原始像素的排列来处理,而浏览器/图片查看器会读取EXIF来正确显示,导致压缩后的图片丢失了EXIF方向信息,或者GD没按方向调整像素,最终显示时出现旋转偏差。

具体解决方案:修改image_resize函数,添加EXIF方向校正逻辑

你需要在创建图片资源后,读取照片的EXIF方向信息,然后根据不同的方向值旋转/翻转图片,同时更新宽高参数保证比例计算正确。

修改后的函数代码如下(关键修改部分已标注):

function image_resize($file, $string = null, $width = 0, $height = 0, $proportional = true, $output = 'file', $delete_original = true, $quality = 100, $grayscale = false) {
    if ($height <= 0 && $width <= 0) return false;
    if ($file === null && $string === null) return false;
    $info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
    $image = '';
    $final_width = 0;
    $final_height = 0;
    list($width_old, $height_old) = $info;
    $cropHeight = $cropWidth = 0;

    // --- 新增:读取EXIF方向信息 ---
    $orientation = 1; // 默认正常方向
    if ($file !== null && $info[2] == IMAGETYPE_JPEG) {
        $exif = @exif_read_data($file);
        if (isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
        }
    }

    if ($proportional) {
        if ($width == 0) $factor = $height/$height_old;
        elseif ($height == 0) $factor = $width/$width_old;
        else $factor = min($width / $width_old, $height / $height_old);

        $final_width = round($width_old * $factor);
        $final_height = round($height_old * $factor);
    } else {
        $final_width = ($width <= 0 ? $width_old : $width);
        $final_height = ($height <= 0 ? $height_old : $height);
        $widthX = $width_old / $width;
        $heightX = $height_old / $height;
        $x = min($widthX, $heightX);
        $cropWidth = ($width_old - $width * $x) / 2;
        $cropHeight = ($height_old - $height * $x) / 2;
    }

    // if the image is smaller than max. width/height, just keep it as is
    if ($width_old < $width && $height_old < $height) {
        $cropHeight = $cropWidth = 0;
        $final_width = $width_old;
        $final_height = $height_old;
    }

    switch($info[2]) {
        case IMAGETYPE_JPEG:
            $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string);
            break;
        case IMAGETYPE_GIF:
            $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string);
            break;
        case IMAGETYPE_PNG:
            $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string);
            break;
        default:
            return false;
            break;
    }

    // --- 新增:根据EXIF方向校正图片 ---
    if ($orientation != 1) {
        switch($orientation) {
            case 2: // 水平翻转
                imageflip($image, IMG_FLIP_HORIZONTAL);
                break;
            case 3: // 旋转180度
                $image = imagerotate($image, 180, 0);
                list($width_old, $height_old) = [$height_old, $width_old];
                break;
            case 4: // 垂直翻转
                imageflip($image, IMG_FLIP_VERTICAL);
                break;
            case 5: // 水平翻转后逆时针旋转270度
                imageflip($image, IMG_FLIP_HORIZONTAL);
                $image = imagerotate($image, 270, 0);
                list($width_old, $height_old) = [$height_old, $width_old];
                break;
            case 6: // 逆时针旋转270度(对应顺时针90度,解决竖屏旋转问题)
                $image = imagerotate($image, 270, 0);
                list($width_old, $height_old) = [$height_old, $width_old];
                break;
            case 7: // 水平翻转后逆时针旋转90度
                imageflip($image, IMG_FLIP_HORIZONTAL);
                $image = imagerotate($image, 90, 0);
                list($width_old, $height_old) = [$height_old, $width_old];
                break;
            case 8: // 逆时针旋转90度
                $image = imagerotate($image, 90, 0);
                list($width_old, $height_old) = [$height_old, $width_old];
                break;
        }
    }

    // grayscale image
    if ($grayscale) {
        imagefilter($image, IMG_FILTER_GRAYSCALE);
    }

    $image_resized = imagecreatetruecolor($final_width, $final_height);
    if (($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG)) {
        $transparency = imagecolortransparent($image);
        $palletsize = imagecolorstotal($image);

        if ($transparency >= 0 && $transparency < $palletsize) {
            $transparent_color = imagecolorsforindex($image, $transparency);
            $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
            imagefill($image_resized, 0, 0, $transparency);
            imagecolortransparent($image_resized, $transparency);
        } elseif ($info[2] == IMAGETYPE_PNG) {
            imagealphablending($image_resized, false);
            $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
            imagefill($image_resized, 0, 0, $color);
            imagesavealpha($image_resized, true);
        }
    }

    imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);

    // delete original image, if set
    if ($delete_original) {
        @unlink($file);
    }

    switch(strtolower($output)) {
        case 'browser':
            $mime = image_type_to_mime_type($info[2]);
            header("Content-type: " . $mime);
            $output = NULL;
            break;
        case 'file':
            $output = $file;
            break;
        case 'return':
            return $image_resized;
            break;
        default:
            break;
    }

    // saving new image
    switch($info[2]) {
        case IMAGETYPE_GIF:
            imagegif($image_resized, $output);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($image_resized, $output, $quality);
            break;
        case IMAGETYPE_PNG:
            $quality = 9 - (int)((0.9 * $quality) / 10.0);
            imagepng($image_resized, $output, $quality);
            break;
        default:
            return false;
            break;
    }
    return true;
}

关键说明:

  1. EXIF读取:只针对JPEG格式(相机照片大多是JPEG),用@exif_read_data避免没有EXIF时的警告。
  2. 方向校正:针对常见的8种Orientation值,调用GD的imagerotateimageflip函数调整像素排列。
  3. 宽高更新:旋转后图片的宽高会交换,必须更新$width_old$height_old,否则后续的比例计算和裁剪会出错。
  4. 扩展依赖:确保你的PHP环境开启了exif扩展(在php.ini中开启extension=exif,部分环境可能还需要开启mbstring扩展)。

这样修改后,竖屏拍摄的照片就能正确显示,不会再出现旋转问题了。

内容的提问来源于stack exchange,提问作者sylar32

火山引擎 最新活动