/    Sign up×
Community /Pin to ProfileBookmark

Check image height/width on upload

I just found a simple image upload script here on the forums, but I would like to modify it so that only an image of specific dimensions can be uploaded.

e.g 200×300 pixels.

[code=php]<?PHP

$uploaddir = “uploads/”; # must be chmoded to 777

if (isset($_POST[‘submit’])) {
$name = $_FILES[‘File’][‘name’];
$tmpname = $_FILES[‘File’][‘tmp_name’];
$size = $_FILES[‘File’][‘size’];

// If the user uploads not a valid image, then display an error msg.
$imgSize = @getImageSize($tmpname);
if (empty($imgSize)) {
print (“<p>Please upload a valid image.</p>n”);
} else {
/*** Get the mime type of the image and check, manager entered the valid image or not. ***/
$imageType = explode(“/”,$imgSize[‘mime’]);
$imageType = trim(strtolower($imageType[1]));
$validImageTypes = array(“jpg”, “jpeg”, “png”, “gif”); // you can also use the same code for checking other type of files like, PDF.
if (!in_array($imageType, $validImageTypes)) {
print(“<p>Please upload a valid image for cover.</p>n”);
} else {
if (move_uploaded_file($tmpname, $uploaddir . $name)) {
print (“<p>File Name: $name</p>n”);
print (“<p>File Size: $size</p>n”);
print (“<p>Your file was successfully uploaded!</p>n”);
} else {
print (“<p>Your file could not be uploaded.</p>”);
}
}
}
}

?>[/code]

Any ideas?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 08.2008 — [url=http://www.php.net/getimagesize]getimagesize[/url]() will give you that info.
Copy linkTweet thisAlerts:
@nnhubbardauthorMay 08.2008 — [url=http://www.php.net/getimagesize]getimagesize[/url]() will give you that info.[/QUOTE]

So, something like the following would work?

[code=php]<?php

$tmpName = $_FILES['userfile']['tmp_name'];

list($width, $height, $type, $attr) = getimagesize($tmpName);

if($width>275 || $height>275)
{
die("exceeded image dimension limits.");
}

?>[/code]
×

Success!

Help @nnhubbard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...