allpnay
allpnay

Reputation: 539

php imagerotate() function

My site can upload photos, the process works well on a computer. If I upload a picture through mobile is rotating. I saw it was a known problem and has a solution.
I try one of the solutions and I get an error while uploading an image to a folder:

move_uploaded_file() expects parameter 1 to be string, resource given.

This is the code I use to rotate the image:

$exif = exif_read_data($new_img['tmp_name']);
if (!empty($exif['Orientation'])) {

    $file = imagecreatefromjpeg($new_img['tmp_name']);
    switch ($exif['Orientation']) {
        case 3:
            $new_img['tmp_name'] = imagerotate($file, 180, 0);
            break;

        case 6:
            $new_img['tmp_name'] = imagerotate($file, -90, 0);
            break;

        case 8:
            $new_img['tmp_name'] = imagerotate($file, 90, 0);
            break;
    }
}

move_uploaded_file($new_img['tmp_name'], $UploadDirectory.$NewFileName )

Upvotes: 1

Views: 1004

Answers (2)

allpnay
allpnay

Reputation: 539

I changed it to:

case 3:
    $rotate = imagerotate($file, 180, 0);
    imagejpeg($rotate,$new_img['tmp_name']);
break;

Upvotes: 0

Dan
Dan

Reputation: 11084

You are storing the return value of imagerotate as though it were the filename.

$new_img['tmp_name'] = imagerotate($file, 180, 0);

But it's not since imagerotate returns a resource.

So just change all of these lines:

$new_img['tmp_name'] = imagerotate($file, 180, 0); 

with

imagerotate($file, 180, 0);

I just learned imagerotate existed. I'm not sure exactly how it works. You may need to save the file out at some point.

Upvotes: 1

Related Questions