Forum Moderators: coopster

Message Too Old, No Replies

Generating Image Dimensions with PHP

         

montman

9:54 pm on Aug 11, 2009 (gmt 0)

10+ Year Member



I am using php to get images into a page. I want to generate the images' dimensions dynamically for browsers to use. I looked at the following page but don't know enough about php to use what is being suggested: [docs.php.net...]

So I used a server behavior I found in a book that works with the GD extension on a server. GD is enabled on the server I am using. However, this is not working. Here is the relevant code from the page I am using:

?php
if (!empty($row_Recordset1['imgpath'])) { ?>
<img <?php echo getDims($row_Recordset1['imgpath'], 'images/'); ?> src="<?php echo $row_Recordset1['imgpath']; ?>" alt="<?php echo $row_Recordset1['alt']; ?>" name="therPhoto" id="therPhoto"/>
<?php } ?>

AND

<?php
mysql_free_result($Recordset1);
// php code to get image dimensions dynamically
function getDims($image,$folder) {
if (!empty($folder)) {
if (strrpos($folder, '/') != strlen($folder)-1) {
$folder .= '/';
}
}
if(!empty($image) && file_exists($folder.$image)) {
$image_info = getimagesize($folder.$image);
}
$retVal = isset($image_info) ? $image_info[3] : '';
return $retVal;
}
?>

I would greatly appreciate help with this.

ashishp

2:55 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Maybe this will help:

<?php
if (!empty($row_Recordset1[imgpath])) {
$file_name = trim($row_Recordset1[imgpath]);
list($width, $height, $type, $attr) = getimagesize("images/".$file_name);
echo "<img $attr src='images/".$file_name."' alt=\"".$row_Recordset1[alt]."\" name=\"therPhoto\" id=\"therPhoto\"/>";
}
?>

montman

6:57 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Thank you so much for the code. However, it is not working. It doesn't bring in the image, and I get the error message below:

Warning: getimagesize(images/images/bbb.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home4/prescot1/public_html/prescottmentalhealth/practitioners3.php on line 237

It appears that one thing wrong (and maybe the only thing) is that the code is trying to bring the image in from images/images/bbb.jpg, rather than images/bbb.jpg, the latter being the correct path. I don't know how to fix this.

montman

8:45 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



I made some changes to the code and it is now working fine. Here is the code with the modifications:

<?php
if (!empty($row_Recordset1[imgpath])) {
$file_name = trim($row_Recordset1[imgpath]);
list($width, $height, $type, $attr) = getimagesize($file_name);
echo "<img $attr src='/".$file_name."' alt=\"".$row_Recordset1[alt]."\" name=\"therPhoto\" id=\"therPhoto\"/>";
}
?>

I do have one other questions, though. Does the source code for the image need to be in any particular order for browsers? So, for example, this is the source code that is generated:
<img width="60" height="75" src='/images/carol.jpg' alt="Photo of Carol Cook, M.A., L.P.C" name="therPhoto" id="therPhoto"/>
Is that order okay?

innoscape

8:41 pm on Aug 16, 2009 (gmt 0)

10+ Year Member



No, it does not matter which order.
I actually use style sheets as well in my image tags. That is where you will run into cross browser issues. If you run into alignment or padding issues with your format cross browser, you may have to assign a class or additional styling to your id as well. The id that is already in your tag however, may be taking care of that.

rocknbil

4:25 pm on Aug 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does the source code for the image need to be in any particular order for browsers?

While the correct answer is no, this is a good question, one I haven't seen suggested in any specifications.

In printing, we always had such rules: when specifying dimensions, specify width then height. This generated a clear industry standard when someone said 8.5 X 11.

I try to specify attributes from important to trivial, or most significant to least significant, or whatever. So

<img src="image.jpg" width="20" height="20" id="image-id" border="0" alt="alt content" title="title content">

attributes may vary with doctype . . . also "significant to least significant" can be debated ad nauseum . . . which is probably why it doesn't matter.

Whatever you do, keep it consistent, always do the same thing. There's nothing worse than setting widths for heights, heights for widths because you're trying to economize your time and expect on in a certain position when it's the other. :-)

montman

9:02 pm on Aug 19, 2009 (gmt 0)

10+ Year Member



Thanks to all who responded to my question. The responses have been quite helpful!