I'm currently using GD and some PHP to rotate images. After each rotation the quality of the image gets worse. Is there a better way to do this or is this expected?
$img = new ImageClass;
list($original, $info) = $img->load($src);
// Determine angle
$angle = strtolower($angle);
if( $angle == 'cw' || $angle == 'clockwise' ) $angle = 270;
if( $angle == 'ccw' || $angle == 'counterclockwise' ) $angle = 90;
$rgb = $img->hex2rgb($bg_color);
$bg_color = imagecolorallocate($original, $rgb['r'], $rgb['g'], $rgb['b']);
$new = imagerotate($original, $angle, $bg_color);
Is it possible to rotate the image client side with maybe jquery and then save the image via PHP to the server? I'm assuming this will help with the image quality.
In our sharing service we handle this differently. We keep the original image at all times and store the initial rotation (0).
Upon rotation, the original image is loaded, rotated according to rotation +/- 90 and a copy is written onto the file system. Afterwards the database is updated with the new rotation.
You will need two more columns in your database though, for the rotation and the rotated copy location. I'm also assuming that you always do an initial resize (and perhaps multiple for different sizes), so that the extra column always points to the copy rather than sometimes pointing to the original instead until rotated.
If you rotate an image in a finite matrix, you can't avoid degradation of the quality, server side or client side and those degradations are added at each rotation.
If you need many rotated images from the same one, you must always use the first (never rotated) image as source and increment the angle instead of rotating the last rotated image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With