Photo rotation in PHP [closed]

-4

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

    
asked by anonymous 08.12.2016 / 01:15

2 answers

2

You can use the imagerotate() function.

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 "GD Functions" documentation and specifically the imagerotate() .

    
08.12.2016 / 16:02
2

The problem is that it does not help to rotate with PHP, you would have to create an intelligent algorithm to detect that the picture is either -90 ° or + 90 ° or + 180 °, PHP will not be able to do it alone language).

Imagine that you have a photo that is + 180 °, so you can use imagerotate($imagem, 180) , the next photo will be at -90 °, so the correct one would be to use imagerotate($imagem, 90) , which only rotates in the wrong way. / p>

For most photos it is not possible to to rotate (read the end of the answer to see the workaround), but still some photos coming directly from a digital camera or advanced cell) and you have the exif header, you can rotate (if you have gone through a less advanced image editor maybe the data is not what you expected) then you can try something like this 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');

Functions with exif_ prefix can be disabled, so look in the PHP.INI of your server for these lines and get the ; from the front:

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

It should look like this:

extension=php_mbstring.dll
extension=php_exif.dll

If it's Unix-like it looks like this:

extension=mbstring.so
extension=exif.so

If it's PHP7.2 +, both windows and unix-like are like this (for both):

extension=mbstring
extension=exif

After saving the changes 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 exif , or may be affected by some image editor, the more is guaranteed is to pass the task to the user in a front- end with and

A plugin if you use Angular.js:

If you use jQuery

08.12.2016 / 18:51