【问题标题】:Codeigniter image rotate when image resize图像调整大小时,Codeigniter 图像旋转
【发布时间】:2016-04-15 16:33:09
【问题描述】:

我有一个尺寸为 (W-3000 X H-4000) 的图像。当我上传它并调整大小时,它总是显示为横向模式,意味着新尺寸是 w-1067 X h-800。我想为横向或 600X800 的纵向创建这张图片。这是我的代码:

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|jpeg|gif|png';
    $config['max_size'] = '5000';
    $this->load->library('upload', $config);

    //check if a file is being uploaded
    if(strlen($_FILES["testimg"]["name"])>0){

        if ( !$this->upload->do_upload("testimg"))//Check if upload is unsuccessful
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($errors);
        }
        else
        {

            $config['image_library'] = 'gd2';
            $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
            $config['width'] = '1';
            $config['height'] = '800';
            $config['maintain_ratio'] = TRUE;
            $config['master_dim'] = 'height';
            $this->load->library('image_lib',$config); 

            if (!$this->image_lib->resize()){  
                echo "error";
            }else{
                echo "success";
            }
       }      
   }  

在我的代码中,高度始终为 800 像素。对于 3000X4000 尺寸的图像是可以的。但是当我使用 4000X3000 尺寸的图像时呢?有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

  • 使用 getimagesize()

标签: php codeigniter


【解决方案1】:
$filename = $_FILES['testing']['tmp_name'];
list($width, $height) = getimagesize($filename);

if ($width >= $height)
{
    $config['width'] = 800;
    $config['master_dim'] = 'width';
}
else
{
    $config['height'] = 800;
    $config['master_dim'] = 'height';
}

甚至更短,您可以使用 master_dim 的“auto”参数来确定哪个值更大 - 宽度或高度。

$filename = $_FILES['testing']['tmp_name'];
list($width, $height) = getimagesize($filename);

if ($width >= $height)
{
    $config['width'] = 800;
}
else
{
    $config['height'] = 800;
}

$config['master_dim'] = 'auto';

【讨论】:

  • 我已经尝试过使用 2448X3264 尺寸的图像。当我调整大小时,它显示 800X600 图像。但是调整大小的图像会旋转。我想我应该使用 exif。
【解决方案2】:

感谢@Rpojka 的回答。但我用 exif_read_data() 修复了它。这是我的代码:

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|jpeg|gif|png';
    $config['max_size'] = '6048';
    $this->load->library('upload', $config);

    //check if a file is being uploaded
    if(strlen($_FILES["testimg"]["name"])>0){

        if ( !$this->upload->do_upload("testimg"))//Check if upload is unsuccessful
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($errors);
        }
        else
        {

            $config['image_library'] = 'gd2';
            $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
            $filename = $_FILES['testimg']['tmp_name'];


            $imgdata=exif_read_data($this->upload->upload_path.$this->upload->file_name, 'IFD0');


            list($width, $height) = getimagesize($filename);
            if ($width >= $height){
                $config['width'] = 800;
            }
            else{
                $config['height'] = 800;
            }
            $config['master_dim'] = 'auto';


            $this->load->library('image_lib',$config); 

            if (!$this->image_lib->resize()){  
                echo "error";
            }else{

                $this->image_lib->clear();
                $config=array();

                $config['image_library'] = 'gd2';
                $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;


                switch($imgdata['Orientation']) {
                    case 3:
                        $config['rotation_angle']='180';
                        break;
                    case 6:
                        $config['rotation_angle']='270';
                        break;
                    case 8:
                        $config['rotation_angle']='90';
                        break;
                }

                $this->image_lib->initialize($config); 
                $this->image_lib->rotate();

            }
       }      
   }  

这是exif方向图

 1        2       3      4         5            6           7          8

888888  888888      88  88      8888888888  88                  88  8888888888
88          88      88  88      88  88      88  88          88  88      88  88
8888      8888    8888  8888    88          8888888888  8888888888          88
88          88      88  88
88          88  888888  888888

【讨论】:

  • 不错的解决方案。只是因为文件类型而对文件类型有所了解,最好用if (exif_imagetype('image.gif') == IMAGETYPE_JPEG OR exif_imagetype('image.gif') == IMAGETYPE_TIFF_II OR exif_imagetype('image.gif') == IMAGETYPE_TIFF_MM) { //rest of code } else {//handle exception}之类的东西来处理它
  • 感谢您的信息 :)
  • 没问题,我在PHP docs找到了。不过对你来说做得很好。
  • 我从移动设备上传时遇到了同样的问题。我用你的例子来解决我的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-07-20
  • 2019-07-11
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多