You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter移动端图片上传EXIF方向修正问题求助

解决CodeIgniter移动端上传图片旋转问题

我来帮你搞定这个移动端图片上传后不自动修正方向的问题,你的代码里有几个关键细节没处理对,咱们一步步调整:

问题分析

  1. EXIF读取路径错误:你用$config['source_image'] = $files;,但$files是整个$_FILES数组,不是上传后的本地文件路径,导致exif_read_data根本读不到正确的图片元信息。
  2. 旋转配置的目标路径错误:$config['new_image'] = $files;同样是错误的,没法正确保存旋转后的图片。
  3. 重复加载库与配置冲突:多次重复加载image_lib,而且后续生成缩略图时没确保用的是已经旋转修正后的原图。

修正后的代码

public function changeimage() {
    $this->load->library('upload');
    $sid = $this->session->userdata['user']['id'];
    $files = $_FILES;

    if (!empty($_FILES['image']['name'])) {
        // 生成随机文件名
        $format = explode('.', $files['image']['name']);
        $format = end($format);
        $cimagename = md5(uniqid(mt_rand())) . ".$format";
        
        // 重置上传文件信息
        $_FILES['image']['name'] = $cimagename;
        $_FILES['image']['type'] = $files['image']['type'];
        $_FILES['image']['tmp_name'] = $files['image']['tmp_name'];
        $_FILES['image']['error'] = $files['image']['error'];
        $_FILES['image']['size'] = $files['image']['size'];

        $date1 = date('Y/m/d ', strtotime($this->input->post('createdon')));
        $this->upload->initialize($this->set_upload_options($sid, $date1));

        if (!$this->upload->do_upload("image")) {
            $data['error'] = true;
            $data['errormsg'] = "Something went wrong";
            echo $data['errormsg'];
        } else {
            $image_info = $this->upload->data();
            $this->load->library('image_lib');
            
            // 1. 处理图片旋转:使用上传后的完整本地路径
            $config = [];
            $config['image_library'] = 'gd2';
            $config['source_image'] = $image_info['full_path']; // 正确的原图路径
            $config['new_image'] = $image_info['full_path']; // 覆盖原文件,确保后续缩略图用修正后的图
            $config['create_thumb'] = false;

            // 读取EXIF方向信息
            $exif = exif_read_data($config['source_image']);
            if ($exif && isset($exif['Orientation'])) {
                $ort = $exif['Orientation'];
                switch($ort) {
                    case 3:
                    case 4:
                        $config['rotation_angle'] = '180';
                        break;
                    case 6:
                    case 5:
                        $config['rotation_angle'] = '270';
                        break;
                    case 8:
                    case 7:
                        $config['rotation_angle'] = '90';
                        break;
                    default:
                        $config['rotation_angle'] = '';
                }
            }

            // 执行旋转(只有需要旋转时才操作)
            if (!empty($config['rotation_angle'])) {
                $this->image_lib->initialize($config);
                if (!$this->image_lib->rotate()) {
                    echo $this->image_lib->display_errors();
                }
                $this->image_lib->clear();
            }

            // 2. 生成缩略图
            $config = [];
            $config['image_library'] = "gd2";
            $config['source_image'] = $image_info['full_path']; // 基于旋转后的原图生成缩略图
            $config['maintain_ratio'] = FALSE;

            $sizes = array(array("name" => "thumb", "width" => "200", "height" => "227"));
            foreach ($sizes as $size) {
                $config['new_image'] = "assets/userimages/" . $size['name'] . "_" . $image_info['file_name'];
                $config['width'] = $size['width'];
                $config['height'] = $size['height'];
                
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
            }
        }
    }
}

关键调整点

  • 正确获取原图路径:用$image_info['full_path'](CodeIgniter上传类返回的完整本地路径)来读取EXIF和处理旋转,而不是用$_FILES数组。
  • 覆盖原文件:把旋转后的图片保存回原路径,这样后续生成缩略图时用的就是已经修正方向的图片。
  • 简化EXIF判断:用switch语句让方向判断逻辑更清晰,同时覆盖所有可能的Orientation值。
  • 避免重复加载库:只加载一次image_lib,每次处理前重置配置并清空之前的状态,避免配置冲突。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:28:30