Tutorial :Is it possible to perform a lossless rotation on a JPEG Image in PHP?



Question:

I need to rotate some existing JPG images. They have already lost some detail, but I now want to rotate them and lose no further detail.

With a little research, it seems the only lossless Image rotation library for PHP is by using the jPegTran library.

Are there any other options when it somes to doing lossless jpg rotation?

Thanks!


Solution:1

Would't it be possible to call an external program say losslessrotator by exec('commandline');

Another option would be jpegtran by jpegclub


Solution:2

Be careful about jpegtran when rotating cw or ccw by 90 degrees, it will not rotate all of the pixels as expected, as it can do losslless rotation only within area which dimensions are the multiple of jpeg block size (8x8 pixels usually). It rotates pixels inside each of these blocks internally to avoid re-compression of the image, but the edge blocks can't be rotated like that. So with jpegtran -rotate 90 or 270 you will be left with a tiny strip of un-rotated pixels on the edge, and you need to use -trim option to get rid of them, but then the resulting image will be a few pixels smaller than the original.

So while it is a lossless rotation, you still end up loosing some pixels in the process.


Solution:3

JPEG is a lossy format, so the answer is no, you can't create a lossless rotate of JPEG on any application, programming language, or guru meditation.

What you can do, however, is minimize image data loss by using the $quality argument when saving the rotated JPEG, if you're saving it in JPEG format that is. If you're saving it in lossless format, then you've already minimized image data loss.

Example:

$img = imagecreatefromjpeg($file);  $rot = imagerotate($img, 90, 0);  imagejpeg($rot, $output, 100); /* set quality to 100% */  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »