Web Hosting Talk







View Full Version : php getimagesize not working


Nullified
01-08-2005, 03:24 AM
My php version: 4.3.10
My script:

if ($_POST['do'] == 'add_banner')
{
if ($_POST['site_link'] == '')
{
$error_link = "color: #FF0000";
}
if ($_POST['alt_text'] == '')
{
$error_text = "color: #FF0000";
}
if ($_FILES['banner']['tmp_name'] == '')
{
$error_file = "color: #FF0000";
}
else
{
$dimensions = getimagesize($_FILES['banner']['tmp_name']);
if ($_FILES['banner']['type'] !== 'image/gif')
{
$error_type = "<div><font color=\"#FF0000\">Image must be in gif format.</font></div>";
}
if ($_FILES['banner']['size'] > '20480')
{
$error_size = "<div><font color=\"#FF0000\">Image can't be more then 20KB in size.</font></div>";
}
if ($dimensions[0] !== '468' OR $dimensions[1] !== '60')
{
$error_dimensions = "<div><font color=\"#FF0000\">Image dimensions must be 468x60 pixels.</font></div>";
}
}
if (isset($error_type) OR ($error_size) OR ($error_dimensions) OR ($error_link) OR ($error_text) OR ($error_file))
{
$site_link = $_POST['site_link'];
$alt_text = $_POST['alt_text'];
$navbits[''] = $vbphrase['add_banner'];
$templatename = 'banners_add';
}
else
{
$ext = '.gif';
$uploaddir = '/home/whd/public_html/images/banners/';
$uploadfile = $uploaddir.$bbuserinfo['userid'].$ext;
move_uploaded_file($_FILES['banner']['tmp_name'],$uploadfile);
}
}

The code works fine except for the following portion:
if ($dimensions[0] !== '468' OR $dimensions[1] !== '60')
{
$error_dimensions = "<div><font color=\"#FF0000\">Image dimensions must be 468x60 pixels.</font></div>";
}
I have tried several different variations of this code and have even taken out the exclamation marks and tried validating the width and height independantly and nothing will work. I have tried several differently sized gifs under 20k in size and none will upload unless I take out this snippet of code. I have changed the quotation marks to apostrophes and other script variations. I have tried calling (in the main script) $dimensions = getimagesize($file); and defining $file = $_FILES['banner']['tmp_name'];. But of course that didn't work. In my site I have added the code:
$dimensions, $dimensions[0], $dimensions[1]
And of course it correctly displays: Array, 468, 60. Or whichever dimensions apply to the current image. I, also know my code isn't top notch quality, so feel free to improve on any other imperfections if you want.

Burhan
01-08-2005, 03:35 AM
The problem is that you are doing a type-safe comparison (!==) and comparing a string to an integer, which will fail.

Replace !== with != and it should work.

Nullified
01-08-2005, 12:00 PM
That worked. Thanks alot. Can you help me with on other thing? I know there must be a much simpler way to write this if statement....
if (isset($error_type) OR ($error_size) OR ($error_dimensions) OR ($error_link) OR ($error_text) OR ($error_file))
If any of those variables are set it need to do something.

ZiDev
01-08-2005, 06:30 PM
That is probably the best way to check with the or booleen...you could do a loop, but that would take up more space and be more pointless IMO.

-- HW

sonicgroup
01-08-2005, 07:33 PM
Well, you may have to rework other parts of your script, but what I would do (and have in the past) is to create a variable and set it to 0. Then you process your code and increment the variable if you run into an error. Then you can just check that variable to see if it's != 0 and run your error script.

Nullified
01-08-2005, 11:01 PM
Originally posted by sonicgroup
Well, you may have to rework other parts of your script, but what I would do (and have in the past) is to create a variable and set it to 0. Then you process your code and increment the variable if you run into an error. Then you can just check that variable to see if it's != 0 and run your error script. I'm not a php expert, but I don't think that will work because I have 6 different errors which can happen, of which up to 5 can happen at the same time (and 5 different errors displayed on the page). Thefore I need to define 6 different errors.

offtone
01-09-2005, 09:26 AM
Two arrays, one for errors, one to store thrown errors. Then loop and spit the error text:

$errors['no_image'] = 'No image found.';
$errors['bad_image'] = 'Invalid image type.';

Then:
if (conditions for bad image)
$throw[] = 'bad_image';

Then:

foreach ($throw as $error)
echo $errors[$error];

That's how I'd do it, anyway.

sonicgroup
01-09-2005, 09:29 AM
Yea, that's similar to what I was getting at Aaron. Except that rather than running through the array regardless, I use a counter variable to check to see if I have errors. It eliminates some processing - negligible, but cleaner I think.