Reputation: 653
I'm trying to echo the width and height of an image into a looping array I have. Everything other than the width and height seems to be there. The images are displayed in a carousel that I have. For the carousel to work properly it likes to have the image width and height. I don't want to enter manual values and distort the images!!
Here is a snippet of my code.
<?php do {
$image = $_SERVER['DOCUMENT_ROOT'].$row_rs_imgpath['userimagespath'].$row_rs_smimg['thumbfile'];
$x= imagesx($image);
$y = imagesy($image);
?>
<img src="<?php echo $row_rs_imgpath['userimagespath'].$row_rs_smimg['thumbfile']; ?>" alt="<?php echo $row_rs_smimg['imgname']; ?>" width="<?php echo $x;?>" height="<?php echo $y;?>" />
<?php } while ($row_rs_smimg = mysql_fetch_assoc($rs_smimg)); ?>
And when you view the page source code you get the following:
<img src="/images/uploads/my-future-car-1358783315_thumb.jpg"width="" height="" />
<img src="/images/uploads/albert_docks_liverpool-1358872736_thumb.jpg" width="" height="" />
I have also tried
list($width, $height)= getimagesize($image);
but that doesn't work either. Any ideas would be appreciated!
Upvotes: 0
Views: 3685
Reputation: 3712
Your current $image
is a string. The functions you are trying to use are meant to be used on a resources from the GD library.
The easier way to do things, if you just need the dimensions, is to not use the GD library at all (it's overkill for what you're doing). I would recommend using getimagesize
. http://php.net/manual/en/function.getimagesize.php
$size = getimagesize($filename);
echo $size[0]; //width
echo $size[1]; //height
Upvotes: 1
Reputation: 17471
$source = imagecreatefrompng(...);
$width = imagesx($source);
$height = imagesy($source);
For imagesx
and imagesy
function, you need to pass a resource, not string like you are doing.
$size= getimagesize('sample-image.jpg');
$width = $gis[1];
$height = $gis[1];
Or
list($width, $height) = getimagesize($filename)
Upvotes: 0
Reputation: 93276
imagesx
and imagesy
work on image resources, not file names. That line of code with getimagesize
is the right one, but the way you're addressing images will only work in a browser, not in the file system.
/images/image.gif
will work in the browser, but you need either a relative path (../../images/image.gif
) or a file-system specific path (/users/joe/www/images/image.gif
) for the getimagesize
function to find the right file.
Upvotes: 0
Reputation: 630
Did you try using the full path to your file, when using getimagesize()? It doesn't work when it couldn't find the image.
Upvotes: 1