Php 使用move\u上传的\u文件时进行图像旋转

Php 使用move\u上传的\u文件时进行图像旋转,php,upload,exif,image-rotation,Php,Upload,Exif,Image Rotation,试图解决从IOS设备上传的图像存在的问题,其中exif方向保持不变,有时会导致图像旋转 我发现了许多关于使用imagerotate解决此问题的代码片段,但我正在尝试实现它们 为了保存我正在使用的图像: $moveUploadedFile = move_uploaded_file($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName); (摘自防弹图像上传类) 我可以用switch语句检查exif数据,但不能让mo

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

我发现了许多关于使用imagerotate解决此问题的代码片段,但我正在尝试实现它们

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

$moveUploadedFile = move_uploaded_file($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName);
(摘自防弹图像上传类)

我可以用switch语句检查exif数据,但不能让move\u上传的文件工作

我尝试过(测试),例如:

这给了我一个错误,move\u上传的\u文件请求一个字符串,但接收到一个资源

有什么帮助吗?

给您,先生

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

        }
}

$image是一种资源。但是,move_uploaded_file会获取上载文件的文件名以及目标()

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

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

希望这有帮助

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

        }
}