0

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?

Lovelock
  • 7,689
  • 19
  • 86
  • 186

2 Answers2

0

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

        }
}
Adam
  • 1,231
  • 1
  • 13
  • 37
0

$image is a resource. However, move_uploaded_file takes the filenames of the uploaded file as well as the destination (http://php.net/manual/en/function.move-uploaded-file.php)

You have a resource that you want to save, instead of an uploaded file at a specific path.

Use imagejpeg($image, $destination) or imagepng($image, $destination), depending on what you want to store it as.

Hope this helps!