GetImageSize - how does this work - mine doesn't

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

GetImageSize - how does this work - mine doesn't

Post by simonmlewis »

Code: Select all

$size = getimagesize('$logo');
I am trying to gather the height of an image, so I can resize a box that uses said image as a background-image (because things appear over the top of it).

Code: Select all

<?php
include "dbconn.php";
$result = mysql_query ("SELECT * FROM static WHERE section = 'logo'");
$num_rows = mysql_num_rows($result);
if ($num_rows == 0)
{
echo " style='background-image: url(/images/logo.png);'";
}
while ($row = mysql_fetch_object($result))
      {
			echo " style='background-image: url(/images/pages/$row->image);'";
			$logo = $row->image;
      }
mysql_free_result($result);
mysql_close($sqlconn);
$size = getimagesize('$logo');
echo ">$size";
?>
This produces:
[text]Warning: getimagesize($logo) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\phpMyAdmin\site\index.php on line 168
>[/text]

Line 168 is $size = getimagesize('$logo');

I've clearly done something wrong here.
I've also seen "list" queries for height, but cannot see how to put that into a variable, to know the $height, and then use that in my script.

Any help would be appreciated.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: GetImageSize - how does this work - mine doesn't

Post by Celauran »

Why do you have $logo in quotes?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: GetImageSize - how does this work - mine doesn't

Post by simonmlewis »

Because the script I found had something like that in quotes. Is that the wrong thing to do?

Even out of quotes I get:
[text]Warning: getimagesize(test.png) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\phpMyAdmin\site\index.php on line 168
>[/text]

BINGO.

Code: Select all

list($width, $height, $type, $attr) = getimagesize("images/pages/$row->image");
Then echo or use $height.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: GetImageSize - how does this work - mine doesn't

Post by social_experiment »

Wouldn't it be easier to use the array returned by getimagesize()

Code: Select all

<?php
 $pic = getimagesize($logo);
 // height value would be in $pic[1]
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: GetImageSize - how does this work - mine doesn't

Post by simonmlewis »

Does mine (that works) work much harder?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: GetImageSize - how does this work - mine doesn't

Post by social_experiment »

Without a time test it's hard to say; but the function returns an array so why not use it, the code you use is also in the php manual so i guess the format is acceptable
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: GetImageSize - how does this work - mine doesn't

Post by Celauran »

There won't be a performance difference between the two.
Post Reply