简体   繁体   中英

How to get width and height in PHP before upload?

I'm trying to check for the width and height of an image before uploading it and do some checking but after much Google research I came out with this, which also doesn't work.

        $tmp = $_FILES["image"]["tmp_name"];
        list($width, $height) = getimagesize($tmp); 

The error:


Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\CMS Blog\NewPost.php:12 Stack trace: #0 C:\xampp\htdocs\CMS Blog\NewPost.php(12): getimagesize('') #1 {main} thrown in C:\xampp\htdocs\CMS Blog\NewPost.php on line 12

Here's some of the HTML:

<form action="NewPost.php" method="POST" enctype="multipart/form-data">
                            <div class="form-group">
                                <p class="text-warning" style="margin-bottom: 7px;">Select Thumbnail:</p>
                                <div class="custom-file">
                                    <input class="custom-file-input" type="file" name="image" id="ImageInput" value="" accept="image/png, image/jpeg">
                                    <label for="ImageInput" class="custom-file-label">Upload Image</label>
                                </div>
                            </div>
</form>

Does anyone know how to check for the width and height before uploading it? I have been struggling for 2 days now with this problem. Thanks very much!

PS: Using the newest version of XAMPP. And PHP 8.

Before uploading you must use JavaScript.

After uploading use PHP's GD class https://www.php.net/manual/en/book.image.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM