Maximum width and height validation with PHP

Asked

Viewed 68 times

1

I am uploading an image directly to a database but want to check its width and height and if it is 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 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>';
}
      }

1 answer

2


can use getimagesize() :

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

try like 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>';
}
      }
  • In this example you are checking an image and I want to check an image that comes from a POST is possible?

  • an image and an image, that it is from the POST or is not the same thing, does not make any difference !

  • I’m not getting the height and width value!!

  • See my EDIT !!!

Browser other questions tagged

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