duanchan9354 2014-09-10 15:35
浏览 27

使用move_uploaded_file时imagerotate

Trying to counter the issues with uploaded images from IOS devices where the exif orientation is kept causing them to be rotated sometimes.

I found many snippets on using imagerotate to counter this problem but trying to implement them.

For the saving of the image I am using:

$moveUploadedFile = move_uploaded_file($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName);

(taken from bulletproof image upload class)

Im fine with making the switch statement to check the exif data, but cant make the move_uploaded_file work.

I have tried (for testing) e.g:

$image = $fileToUpload["tmp_name"];
$image = imagerotate($image, 90, 0);
$moveUploadedFile = move_uploaded_file($image, $this->uploadDir . "/" . $newFileName);

This is giving me the error of the move_uploaded_file requesting a string BUT receiving a resource instead.

Any help?

  • 写回答

2条回答 默认 最新

  • dongpo1846 2015-08-30 15:29
    关注

    Here you go sir

    public function moveUploadedFile($tmp_name, $destination)
        {   
            if(move_uploaded_file($tmp_name, $destination)){
                $this->image_rotation();
                return true;
            }
    }
        public function image_rotation(){
            /* Check if the image is rotated,
             * and if it's rotates. Fix it!
             */
            // $filename = __DIR__ . '/'.$this->location .'/'. $this->name . '.' . $this->mime;
            $filename = $this   ->fullPath;
    
            $exif = exif_read_data($filename);
            if (isset($exif['Orientation']))
            {
              switch ($exif['Orientation'])
              {
                case 3:
                 // Need to rotate 180 deg
                      $degrees = 180;
                      break ;
    
                case 6:
                  // Need to rotate 90 deg clockwise
                      $degrees = -90;
                      break ;
    
                case 8:
                  // Need to rotate 90 deg counter clockwise
                      $degrees = 90;
                      break ;
              }
    
                if (preg_match("/jpg|jpeg/", pathinfo($filename, PATHINFO_EXTENSION)))
                {
                    $image_source = imagecreatefromjpeg($filename);
                    $rotate = imagerotate($image_source, $degrees, 0);
                    imagejpeg($rotate, $filename, 100);
    
                }
                if (preg_match("/png/", pathinfo($filename, PATHINFO_EXTENSION)))
                {
                   $image_source = imagecreatefrompng($filename);
                   $rotate = imagerotate($image_source, $degrees, 0);
                   imagepng($rotate, $filename, 100);
                }
                if (preg_match("/gif/", pathinfo($filename, PATHINFO_EXTENSION)))
                {
                   $image_source = imagecreatefromgif($filename);
                   $rotate = imagerotate($image_source, $degrees, 0);
                   imagepng($rotate, $filename, 100);
                }
    
                imagedestroy($image_source); //free up the memory
                imagedestroy($rotate);  //free up the memory
    
            }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图