Validate JPG or PNG PHP image

2

Well the point is that I do not know how to validate the following, in the database I capture the url of an image and then I show it with a echo in php, what I try to do is validate the image either jpg or png to then export it in an excel file and I see the image, for this I'm using the PHPExcel export library and it's where it gets complicated, it only works if I use jpg or png but not 2 at the same time what I try to do through a validation ... code in the library:

//Consulta
$sqli=mysqli_query($conn, "select logo from perfil where idperfil = 1");
foreach ($sqli as $key => $value) {
    $value['logo']; //ruta de la imagen 
}
?>

<img class="img-responsive" name="imagefile" id="imagefile" src="<?php echo "../".$value['logo']?>" alt="Logo"> //mostrando la imagen
<?php

$fila = 7; //Establecemos en que fila inciara a imprimir los datos

//Validación de imagen en png o jpeg Aqui falla, solo puedo utilizar 1

if($_FILES['imagefile']['type'] == "image/jpg"){

$gdImage = imagecreatefromjpeg("../".$value['logo']);//Logotipo JPG

}elseif($_FILES["imagefile"]["type"] == "image/png"){

$gdImage = imagecreatefrompng("../".$value['logo']);//Logotipo JPG
}
    
asked by Jonathan 07.05.2018 в 07:09
source

3 answers

0

How about, I hope you serve.

 ext = explode(".", $_FILES['file']['name']);
                if (strtolower($ext[1]) == "png" || strtolower($ext[1]) == "jpg" || strtolower($ext[1]) == "pdf") {
                } else {
                    echo "El archivo adjunto solo acepta pdf, jpg y png.<br>";
                    exit;
                }
    
answered by 07.05.2018 / 20:51
source
2

It happens to you because you do not use the conditional IF and ELSEIF .

  

The first elseif expression (if any) that evaluates to TRUE   it would be executed.

Your code could look like this:

//Validación de imagen en png o jpeg Aqui falla, solo puedo utilizar 1

if($_FILES['imagefile']['type'] == "image/jpg"){

   $gdImage = imagecreatefromjpeg("../".$value['logo']);//Logotipo JPG

}
if($_FILES["imagefile"]["type"] == "image/png"){

   $gdImage = imagecreatefrompng("../".$value['logo']);//Logotipo PNG
}
    
answered by 07.05.2018 в 10:24
0

In agreement I solved it in the following way, since I can not obtain the image and obtain the properties, I only have the URL in the database where the store ( ../vistas/img/perfil/15_log2.jpg ), I decided to extract the last 3 characters of the URL string remaining as a result ( jpg or png ) depending on what comes in the query, and in that way I get it to work.

//Substracion de formato jpg png

$sub=substr($value['logo'], -3);  //Aqui extraigo los ultimos 3 caracteres de la cadena

//Validacion de imgen en png o jpeg

if($sub == "jpg"){

    $gdImage = imagecreatefromjpeg("../".$value['logo']);//Logotipo JPG

}

if($sub == "png"){

    $gdImage = imagecreatefrompng("../".$value['logo']);//Logotipo PNG
}
    
answered by 07.05.2018 в 20:20