Is it possible to perform a lossless rotation on a JPEG Image in PHP?
Asked Answered
B

3

6

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!

Blacklist answered 27/7, 2010 at 11:51 Comment(3)
jpg is lossy by nature. maybe try PNG?Eliga
JPG's compression has a special property that allows lossless rotation. Each 8x8 cell of the image is compressed separately in a way that can be rotated and reordered.Viniculture
@Talvi I think the idea behind lossless rotation on a JPEG is to not introduce any more loss. If you simply open a JPEG, rotate it and save it again (further compressing the image) then the resulting image is going to be more lossy than the one you started with.Mcnelly
B
7

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

Another option would be jpegtran by jpegclub

Brawl answered 27/7, 2010 at 12:4 Comment(4)
I would definitely go with this one; PHP is incredibly slow (and this includes its image manipulation libraries) and you'll be better off calling a program that's actually compiled.Dittography
I have no issue with calling other libraries. I will give this a try. As long as it works on Linux.Blacklist
+1 for suggesting looking outside the PHP sphere - beyond its well-drilled strengths, PHP is not a 100% coverage tool for the web.Storekeeper
Looks like the new replacement for jpegtran is exiftran which is also EXIF aware. (linux.die.net/man/1/exiftran)Brainpan
P
3

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.

Paid answered 6/4, 2011 at 5:4 Comment(3)
Looks like the new replacement for jpegtran is exiftran which is also EXIF aware (linux.die.net/man/1/exiftran). I don't know if it has the same weakness?Brainpan
Yes, it seems to have the same weakness. Which makes is unusable as well. Why can't they do it correctly - since my FastStone Image Viewer can do it?Brainpan
Extra detail about why cropping is necessary for some images: impulseadventure.com/photo/rotation-partial-mcu.html tl;dr not cropping would either shift and requantize the whole image, or extend the image with data that may have been uninitialized.Proline
S
-11

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% */
Sanalda answered 27/7, 2010 at 18:6 Comment(3)
Sorry, the JPG is already saved so has already lost some detail. I now want to rotate the lossy image but lose no further detailBlacklist
You'll achieve that with my advice. If you do not need the output to be JPEG, you can use some other lossless format, like png: imagepng($rot, $output);Sanalda
It is possible to losslessly rotate JPEGs. Apple's Photos does this, for example. I was looking into how this works when I came across this question. Here's an explanation: betterjpeg.com/lossless-rotation.htmPhocine

© 2022 - 2024 — McMap. All rights reserved.