Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP image rotate looses quality after each rotation

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.

like image 293
Paul Avatar asked Jul 04 '12 15:07

Paul


2 Answers

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.

like image 122
Ja͢ck Avatar answered Oct 21 '22 11:10

Ja͢ck


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.

like image 3
Denys Séguret Avatar answered Oct 21 '22 13:10

Denys Séguret