Maximum width and height validation with PHP

1

I'm uploading an image directly to a database but I want to check its width and height and if it's not between (X, X) width and (X, X) height does not insert the data into the database and shows an error message saying that the measurements of the photo are not valid!

Any information you find important please ask! I hope I have explained myself correctly.

My code

 if (isset($_POST['publicar'])) {
   $nomemembro = $_POST['nomemembro'];
 $cargo = $_POST['cargo'];

 $name = $_FILES['imgmembro']['name'];
 $target_file = basename($_FILES["imgmembro"]["name"]);

     // Select file type
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

     // Valid file extensions
    $extensions_arr = array("jpg", "jpeg", "png", "gif");

   // Check extension
    if (in_array($imageFileType, $extensions_arr)) {

    // Convert to base64 
    $image_base64 = base64_encode(file_get_contents($_FILES['imgmembro'] 
 ['tmp_name']));
    $image = 'data:image/' . $imageFileType . ';base64,' . $image_base64;


    $query = "INSERT INTO equipa(nomeprof, foto, cargo) VALUES 
  ('$nomemembro','" . $image . "' ,'$cargo')";
    $query_run = mysqli_query($conn, $query);

    echo '<script>';
    echo 'alert("Membro da equipa adicionado com sucesso!")';
    echo '</script>';
} else {
    echo '<script>';
    echo 'alert("Insira um formato de imagem válida!")';
    echo '</script>';
}
      }
    
asked by anonymous 08.06.2018 / 11:11

1 answer

2

You can use getimagesize () :

list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";

try this:

if (isset($_POST['publicar'])) {
   $nomemembro = $_POST['nomemembro'];
 $cargo = $_POST['cargo'];

 $name = $_FILES['imgmembro']['name'];
 $target_file = basename($_FILES["imgmembro"]["name"]);

     // Select file type
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

     // Valid file extensions
    $extensions_arr = array("jpg", "jpeg", "png", "gif");

   // Check extension
    if (in_array($imageFileType, $extensions_arr)) {

    $img = $_FILES['imgmembro'] ['tmp_name'];
    list($width, $height) = getimagesize($img);


    echo 'SIZE : '.$width.'x'.$height;

    // Convert to base64 
    $image_base64 = base64_encode($img);
    $image = 'data:image/' . $imageFileType . ';base64,' . $image_base64;


    $query = "INSERT INTO equipa(nomeprof, foto, cargo) VALUES 
  ('$nomemembro','" . $image . "' ,'$cargo')";
    $query_run = mysqli_query($conn, $query);

    echo '<script>';
    echo 'alert("Membro da equipa adicionado com sucesso!")';
    echo '</script>';
} else {
    echo '<script>';
    echo 'alert("Insira um formato de imagem válida!")';
    echo '</script>';
}
      }
    
08.06.2018 / 12:47