PHP - Borrar exif de imagenes

 
Vista:

Borrar exif de imagenes

Publicado por Bryan Bruneth (1 intervención) el 20/09/2020 00:51:38
Hola, estoy tratando de buscar algún código para borrar los metadatos de una imagen subida por el usuario ya que si la imagen fue tomada desde un móvil un metadato hace que la imagen se gire 90 grados.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de javier
Val: 1.542
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Borrar exif de imagenes

Publicado por javier (547 intervenciones) el 20/09/2020 08:18:47
No creo que puedas borrar los metadatos con PHP, creo que hay programas para PC para ello.

podrias hacer dos cosas, si tienes en el server la libreria GD instalada (en local normalmente si) puedes buscar un codigo para redimensionar la imagen y de esta manera se deberian borrar los metadatos

te pongo una clese:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class foto_re  {
 
   public $ruta;
   public $alto;
   public $ancho;
   public $foto_defecto;
 
   public function __construct($arg_ruta="",$arg_alto="",$arg_ancho="", $arg_foto_defecto ="") //funcion que se autoejecuta cuando defines un objeto, le puedes poner argumentos de inicialización, por defecto todo es vacio
    {
        $this->ruta =$arg_ruta; //ponemos el argumento pasado cuando defines el objeto
        $this->alto = $arg_alto;
        $this->ancho = $arg_ancho;
        $this->foto_defecto = $arg_foto_defecto;
 
    }
 
   public function redimensiona(){
 
        // la ruta es absoluta.
        $dim = getimagesize($this->ruta);
 
        if($dim[1]){
        $cociente = $dim[0] / $dim[1];
        }
        if($alto){
        $coc_max = $this->ancho / $this->alto;
        }
 
            if(($dim[0]<=$this->ancho)&&($dim[1]<=$this->alto)){
            $ancho = $dim[0];
            $this->alto = $dim[1];
            }else{
                if($cociente>=$coc_max){
 
                $this->alto = $this->ancho / $cociente;
                }else{
 
                $this->ancho = $this->alto * $cociente;
                 }
               }
 
 
            $exists = file_exists( $this->ruta );
            if($exists=='true'){
 
                echo "<img src='$this->ruta' width='$this->ancho' height='$this->alto'>";
 
            }
            else
            {
 
                echo "<img src='$this->foto_defecto' width='$this->ancho' height='$this->alto'>";
            }
 
 
 
    }
 
}
 
//creamos el objeto
$foto = new foto_re();
//asignamos valores
$foto->ruta = '/images/prueba.png';
$foto->alto = 500;
$foto->ancho = 500;
 
//llamada al metodo que redimensiona
$fotoRed = $foto->redimensiona();

y si no puedes usar una funcion como esta que he encontrado en el manual oficial de php, ya que no se pueden cambiar, lo mejor es leerlo y rotar la imagen, hay mas codigos que hacen parecido para las fotos de los moviles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// auto rotates an image file based on exif data from camera
// if destination file is specified then it saves file there, otherwise it will display it to user
// note that images already at normal orientation are skipped (when exif data Orientation = 1)
 
if(!function_exists("gd_auto_rotate")){
    function gd_auto_rotate($original_file, $destination_file=NULL){
 
        $original_extension = strtolower(pathinfo($original_file, PATHINFO_EXTENSION));
        if(isset($destination_file) and $destination_file!=''){
            $destination_extension = strtolower(pathinfo($destination_file, PATHINFO_EXTENSION));
        }
 
        // try to auto-rotate image by gd if needed (before editing it)
        // by imagemagik it has an easy option
        if(function_exists("exif_read_data")){
 
            $exif_data = exif_read_data($original_file);
            $exif_orientation = $exif_data['Orientation'];
 
            // value 1 = normal ?! skip it ?!
 
            if($exif_orientation=='3'  or $exif_orientation=='6' or $exif_orientation=='8'){
 
                $new_angle[3] = 180;
                $new_angle[6] = -90;
                $new_angle[8] = 90;
 
                // load the image
                if($original_extension == "jpg" or $original_extension == "jpeg"){
                    $original_image = imagecreatefromjpeg($original_file);
                }
                if($original_extension == "gif"){
                    $original_image = imagecreatefromgif($original_file);
                }
                if($original_extension == "png"){
                    $original_image = imagecreatefrompng($original_file);
                }
 
                $rotated_image = imagerotate($original_image, $new_angle[$exif_orientation], 0);
 
                // if no destination file is set, then show the image
                if(!$destination_file){
                    header('Content-type: image/jpeg');
                    imagejpeg($rotated_image, NULL, 100);
                }
 
                // save the smaller image FILE if destination file given
                if($destination_extension == "jpg" or $destination_extension=="jpeg"){
                    imagejpeg($rotated_image, $destination_file,100);
                }
                if($destination_extension == "gif"){
                    imagegif($rotated_image, $destination_file);
                }
                if($destination_extension == "png"){
                    imagepng($rotated_image, $destination_file,9);
                }
 
                imagedestroy($original_image);
                imagedestroy($rotated_image);
 
            }
        }
    }
}


este ultimo codigo esta sacado de aqui

https://www.php.net/manual/es/function.exif-read-data.php


saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar