joy1
joy1

Reputation: 21

validate an image before uploading in php

i am uploadind an image in php using copy function. my imade doesnt display when the image height is less than 981pz & width is less than 477pz

Upvotes: 2

Views: 2447

Answers (4)

wesamly
wesamly

Reputation: 1584

If you're looking for a way to restrict uploaded image size, then as others said you can use getimagesize function, but if it is an issue with displaying the image, you can use a script like timthumb, which you pass image path to, and it will resize it to the dimensions you specify (on fly).

Upvotes: 0

bmarti44
bmarti44

Reputation: 1247

Using the copy function is not supported by all hosts, and is considered to be a security risk. If you would like to upload an image to your server, look into creating an HTML form like what has been done here. If you do this, you will be able to get the mime type from the $_FILES array, and know not only whether or not you are uploading a picture, but also what type of picture you are upload (e.g. png, jpeg, gif).

Upvotes: 0

Adi Sembiring
Adi Sembiring

Reputation: 5946

image size validator usually place in php code/server side.

after user upload the file you can check the size of image using

list($width, $height) = getimagesize($img_path)

if ($width > x )
    do something

if ($height > x)
    do something

if you want to check file type and also image resolution uploaded by user, can use

$info = getimagesize("my-great-photo.jpg");

and the result is:
Array
(
    [0] => 300 //width
    [1] => 200 //height
    [2] => 2
    [3] => width="300" height="200"
    [bits] => 8 //size
    [channels] => 3
    [mime] => image/jpeg //image type
)

$width = $info[0];
$height = $info[1];
$type = $info[mime]

check this documentation

Upvotes: 1

codaddict
codaddict

Reputation: 455410

Your question is not very clear. But if you are looking for a way in PHP to find the height and width of an image you can use the getimagesize function.

You can use it as:

list($width,$height) = getimagesize($image_file_name);

Upvotes: 0

Related Questions