Inside this article we will see the concept i.e How to validate height and width of image in PHP. In several cases while development we need to validate file upload either with it’s size, file extension, height, width, etc.
This article contains classified information about file validation with height and width. This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well.
The getimagesize() function in PHP is an inbuilt function which is used to get the size of an image.
Learn More –
- How To Generate Fake Data in PHP Using Faker Library
- How to Generate Fake Image URLs in PHP Using Faker
- How to Generate Random Name of Person in PHP
- How To Get All Dates Between Two Dates in PHP Tutorial
Let’s get started.
Create an Application
Create a folder php-upload in your localhost directory.
Inside this folder, create a file called index.php.
<!DOCTYPE html> <html lang="en"> <head> <title>How To Validate Height and Width of Image in PHP</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <?php if (isset($_POST['btnsubmit'])) { // form submitted list($width, $height) = getimagesize($_FILES['file']['tmp_name']); $valid_extensions = [ "image/png", "image/jpg", "image/jpeg" ]; if ( $width <= 400 && $height <= 200 && in_array($_FILES['file']['type'], $valid_extensions) && $_FILES['file']['size'] <= 13000) { // validating width, height, file extension and size // 13000 is in bytes and KB it is 13 Kb //.. do code }else{ // handle error here } } ?> </head> <body> <div class="container"> <h2>Upload Form</h2> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" placeholder="Enter name" name="name"> </div> <div class="form-group"> <label for="file">Upload File:</label> <input type="file" class="form-control" name="file"> </div> <button type="submit" class="btn btn-primary" name="btnsubmit">Submit</button> </form> </div> </body> </html>
- getimagesize() is a PHP function which returns file attributes when we pass file path into it. We are getting height and width from it.
- $width <= 400, It is 400px. According to given condition width should be less than or equal to 400px.
- $height <= 200, It is 200px.
- in_array($_FILES[‘file’][‘type’], $valid_extensions) Checking file type with given image extensions.
- $_FILES[‘file’][‘size’] <= 13000 Condition to check file size. Here 13000 is in bytes, so it is 13Kb.
Application Testing
Open browser and hit this URL: http://localhost/php-upload
We hope this article helped you to learn How To Validate Height and Width of Image in PHP Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.