My website features an image upload script that’s working fine. I’ve included exif_read_data to read and correct rotation if needed. This works. Nonetheless, I would like to add the possibility to correct rotation of images after they have been uploaded, but without re-uploading the file from the computer. But by rotating an image that’s already uploaded.
What I did now is create a dropdown that contains rotation values 0,90,180,-90. I select an image from my computer, and upload it, sending the rotation value along with it to have it uploaded with a new rotation angle. This also works.
But I want, is the already uploaded image to be corrected without having to reselect from my computer
EDIT ==> I don’t mean how to rotate an image, this is something a can quite easily do, indeed with Jquery. I mean to rotate and save this rotation again to server, but in a way that does not involve reselecting the file form my computer
Anyone suggestions in what way to look? I googled / stackoverflowed quite a bit, but can’t seem to find a lot on this.
Thanks
EDIT 2=>
the php page that executes the sent form has a code that look like this.
if(!$fileNamephoto){ if($rotationAngle != "0"){ $degrees = $rotationAngle; header('Content-type: image/jpeg'); $filename = $originalImage; $source = imagecreatefromjpeg($filename) or notfound(); $rotate = imagerotate($source,$degrees,0); imagejpeg($rotate); imagedestroy($source); imagedestroy($rotate); }
in which $originalImage is the path of the already uploaded image. The one that the rotation should get applied to.
I figured to check if a new image was selected form the dropdown, and if not if a rotation was set. If yes, I want the Originalfilename to be uploaded / saved with rotation angle
But maybe (I think now) it need not to go through the whole image-upload script, since it’s al;ready resized and compressed and renamed and so on.
Advertisement
Answer
I think I solved my issue.
What I do, is send the original imagename in a hidden field to the php file that processes the upload.
if I do not upload a new file, but if I do set a different rotation angle than 0, I use a hidden $_POST variable that contains the image path to rotate the image, and replace it by calling imagejpeg on the original filename
$originalImage = $_POST['originalImage']; $rotationAngle = addslashes($_POST['rotationAngle']); if(!$fileNamephoto){ if($rotationAngle != "0"){ $degrees = $rotationAngle; header('Content-type: image/jpeg'); $filename = $originalImage; $source = imagecreatefromjpeg($filename) or notfound(); $rotate = imagerotate($source,"$degrees",0); imagejpeg($rotate, $filename); imagedestroy($source); imagedestroy($rotate); }