0

I am trying to fix orientation of images from mobile devices, using EXIF to get the orientation and switch/case to fix the orientation. Apparently my function was working offline and now it's not working on Google Apps Engine. Could Google have a specific function to do this?

The function:

$image = imagecreatefromjpeg($path);
$exif = exif_read_data($path);

if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 3:
            $image = imagerotate($image, 180, 0);
            break;
        case 6:
            $image = imagerotate($image, -90, 0);
            break;
        case 8:
            $image = imagerotate($image, 90, 0);
            break;
    }
    imagejpeg($image, $path);
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
athene
  • 408
  • 1
  • 6
  • 15
  • It will be helpful if you provided the error from the logs. But since I've encountered this before, I think it maybe the issue with passing the image path to imagick as mentioned on the issue tracker (https://code.google.com/p/googleappengine/issues/detail?id=12271). Try file_get_contents on the image and pass the result to imagick, this worked for me. – jad Mar 14 '16 at 19:39
  • Thank you. Im not getting an error, but the picture is not being rotated. How do you use file_get_contents on image rotation in php? – athene Apr 11 '16 at 11:00
  • I'm so sorry. I don't know what was I thinking when I made this comment. Your code is not using imagick at all. It's uses gd extension which is enabled on Google App Engine and your code does work fine (assuming the image path is correct and the exif data exists). Since you say it does not work, I suspect that the image you're trying to rotate is in the app source. If that's the case, then writing the image back is what fails since it's on a read-only file-system. Again, very sorry that I mislead you! – jad Apr 11 '16 at 21:05
  • Thank you, no problem, lm using bucketname.filename for file path, which file path should l be using? – athene Apr 11 '16 at 21:27
  • Thank you,no problem lm using bucketname.filename, for the path, which path should l use? – athene Apr 11 '16 at 21:31
  • The full path to your image on Cloud Storage. So, if you have a root folder called images it would look like this: "gs://bucketname/images/image_file.jpg". I tested this just to be sure and can confirm that it works. To be sure the image was saved. assign imagejpeg result to variable and log it. It returns a boolean. – jad Apr 11 '16 at 21:34
  • Thank you, I am using the bucket url which is gs://bucketname/filename but its not rorating my images, were you able to test the exact function above? – athene Apr 11 '16 at 21:38
  • Yes, I tested your code and it worked for me. Add this line after you read exif data: syslog(LOG_DEBUG, "read exif data: " . json_encode($exif)); and check the logs to see if the source image has the rotation you expect or not. – jad Apr 11 '16 at 21:42
  • Thank u Jad,lm on it – athene Apr 11 '16 at 21:44
  • The other thing l was worried abt is lm saving the image with the initial path, cld tht affect tge rotation? – athene Apr 11 '16 at 21:46
  • I just realised I was wrong again. This fails because exif_read_data returns false instead of exif data array. I have tried specifying tags but that didn't help either. I was in a hurry when I tested this, so I added a line to override $exif['Orientation'] (to check) and forgot all about it. That's why I mentioned it works. Anyway, clearly this is a bug in App Engine and I think you should start an issue for this case unless one exists already. – jad Apr 11 '16 at 22:11
  • thank you, how can l use imagemagic? – athene Apr 12 '16 at 03:50
  • http://stackoverflow.com/questions/19456036/detect-exif-orientation-and-rotate-image-using-imagemagick This question has an example in the answers. You'll need to add extension = "imagick.so" in your php.ini file, since imagick is not enabled by default. – jad Apr 12 '16 at 05:40
  • Thak you, Ive added Imagick, lm excited l finally got an error message, Uncaught exception 'ImagickException' with message 'UnableToWriteBlob `magick--12wNf_OjAqAF4': Read-only file system , would you know how l can solve this one – athene Apr 12 '16 at 08:21
  • We can't solve this. It's on Google's end. There's an open issue for this on the tracker, which I linked in my first comment. But, here's a workaround until this is fixed. Initialize Imagick without passing the file path. Read the image into a temp variable, let's say $tmp = file_get_contents('gs://bucket/file.jpg'); then use Imagick's method readimageblob and pass $tmp into that. Now, you can work with the image. Once done, don't save using writeimage. Instead use getimageblob. Let's say $tmp2 = $image->getimageblob(); then save using file_put_contents('gs://bucket/file.jpg', $tmp2); – jad Apr 12 '16 at 18:10
  • Thank you Jad ,l tried it yesterday, but still there is no image rotation, l really dont understand whts happening, lm just about to give up,kkkkkkkkkkkkkkkk – athene Apr 13 '16 at 08:45
  • Finally it works, l was putting a wrong variable for the image, extension=php_imagick.dll works better on mine, thank you Jad – athene Apr 13 '16 at 12:37
  • Glad this is resolved. Sorry again about the mess up :-) – jad Apr 13 '16 at 17:32
  • Not a problem Jad, thank you for all you did. – athene Apr 13 '16 at 17:40

0 Answers0