Rotation of photos in PHP

Asked

Viewed 1,672 times

-4

How do I rotate an already saved photo file on the server. The photos sometimes come lying down and I want them standing up or vice versa. My system is in PHP.

  • What code are you testing ?

  • 2

    read this post here http://meta.pt.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions? cb=1

  • Your question is very vague. You need to be more specific by adding codes etc.

2 answers

3

The problem is that it is no use to rotate with PHP, you would have to create an intelligent algorithm to detect that the photo is either -90° or +90° or +180°, PHP will not be able to do it alone (and neither will any language).

Imagine you have a photo that is +180°, so you can use imagerotate($imagem, 180), the next photo will be -90°, so the correct one would be to use imagerotate($imagem, 90), which only rotate incorrectly.

For most of the photos it is not possible rotate (read the end of the answer to see the alternative solution), yet still some photos come directly from a digital camera (or advanced cell phone) and has the header exif, it will be possible to rotate (if you have passed through a less advanced image editor maybe the data is not expected) then you can try something like this answer from Soen:

function corrigeOrientacao($filename)
{
    $exif = exif_read_data($filename);
    $rotation = null;

    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation'])
        {
            case 3:
                $rotation = 180;
                break;

            case 6:
                $rotation = -90;
                break;

            case 8:
                $rotation = 90;
                break;
        }
    }

    if ($rotation !== null) {
        $target = imagecreatefromjpeg($filename);
        $target = imagerotate($target, $rotation, 0);

        //Salva por cima da imagem original
        imagejpeg($target, $filename);

        //libera da memória
        imagedestroy($target);
        $target = null;
    }
}

Using:

corrigeOrientacao('pasta/minhafoto.jpg');

The functions with prefix exif_ may be disabled, for this search in PHP.INI of your server these lines and take the ; forward:

;extension=php_mbstring.dll
;extension=php_exif.dll

Should stay like this:

extension=php_mbstring.dll
extension=php_exif.dll

If it is Unix-like it is so:

extension=mbstring.so
extension=exif.so

If it is PHP7.2+, both windows and Unix-like is so (for both):

extension=mbstring
extension=exif

After saving the modifications you need to restart the Apache/Nginx/IIS server


Passing the task to the user (solving with Javascript/front-end)

However as I said many photos will not have the exif, or may be affected by an image editor, the more is guaranteed is to pass the task to the user in a front-end applications with and , however there are plugins ready, as:

A plugin just happens to use Angular.js:

If you use jQuery

2

You can use the function imagerotate().

For example:

// Define o básico
$graus = 90;
$arquivo = "imagem.jpg";

// Cria a imagem de JPEG (se for PNG deve usar imagecreatefrompng(), por exemplo)
$imagem = imagecreatefromjpeg($arquivo);

// Rotaciona
$rotate = imagerotate($imagem, $graus, 0);

See the documentation of "Functions GD" and specifically of imagerotate().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.