0

How can I request that PHP rotates an image a certain degree? Some images need to be rotated 90 degress clockwise and some need a 180 degree rotation.

When I use this code:

rotate  picture.jpg -rotate 90 picture.jpg;

I get the following error:

Parse error: syntax error, unexpected T_DNUMBER on line 2

I'd like a permanent modification of the image on the server. Any help would be great. I am new to this so if I am missing vital information please let me know and I will edit. Thanks.

Mike
  • 105
  • 12

2 Answers2

1

A solution with would be straight forward. Simply provided a background color, and degree to Imagick::rotateImage method.

$degree = (int)$_GET['degree'] % 360;     // Enforce given integer between 0 ~ 360
$background = new ImagickPixel('none');   // No background color
$wand = new Imagick($source_file_path);   // Read file from server
$wand->rotateImage($background, $degree); // Rotate
$wand->writeImage($output_file_path);     // Save image on server
emcconville
  • 23,800
  • 4
  • 50
  • 66
0

Granted I'm a bit unfamiliar with PHP, from what I could find this looks as though it would work. You'll want to change 180 to what ever degree you'd like to rotate, eg 45/90/etc Hopefully it was of some use and best of luck!

http://www.php.net/manual/en/function.imagerotate.php

<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

// Content type
header('Content-type: image/jpeg');

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
imagejpeg($rotate);

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

Edit: Ok my appologies there, I was taking a shot in the dark I'll keep digging and reply back, if I can find anything worth mentioning. I'm going to politely bow out, outside of reformating the whole image my brains wrecked on this one bud. Best of luck though. I'm going to thread stalk because I'm curious now.

BrandonB
  • 26
  • 4
  • Thanks for the response. That code merely mimicks a rotation. I am wanting to change the actual file, so that I don't have to download, edit with photoshop and re-upload. – Mike Apr 20 '14 at 19:59
  • Actually, @BrandonB is pretty much there. The `imagejpeg($rotate)` line just needs an additional parameter to write the file over the top of the original ([see docs](http://www.php.net/manual/en/function.imagejpeg.php)). `imagejpeg($rotate, $filename);` should do it. – jonnu Apr 20 '14 at 20:18