【问题标题】:imagerotate when using move_uploaded_file使用 move_uploaded_file 时的图像旋转
【发布时间】:2014-09-10 15:35:59
【问题描述】:

试图解决从 IOS 设备上传图像的问题,其中 exif 方向保持不变,导致它们有时会旋转。

我发现了很多关于使用 imagerotate 来解决这个问题的 sn-ps,但我试图实现它们。

为了保存我正在使用的图像:

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

(取自防弹图片上传类)

我可以通过 switch 语句检查 exif 数据,但无法使 move_uploaded_file 工作。

我已经尝试过(用于测试)例如:

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

这给了我 move_uploaded_file 请求字符串但接收资源的错误。

有什么帮助吗?

【问题讨论】:

    标签: php upload exif image-rotation


    【解决方案1】:

    先生,你来了

    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
    
            }
    }
    

    【讨论】:

      【解决方案2】:

      $image 是一种资源。但是,move_uploaded_file 采用上传文件的文件名以及目标 (http://php.net/manual/en/function.move-uploaded-file.php)

      您有一个要保存的资源,而不是特定路径上的上传文件。

      使用 imagejpeg($image, $destination) 或 imagepng($image, $destination),具体取决于您要将其存储为什么。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-04
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2016-04-08
        • 2011-04-26
        • 2010-12-04
        相关资源
        最近更新 更多