简体   繁体   中英

how to globalize php getimagesize function variables

I am using following code in my web pages to get image dimension.

<?php
    list($width, $height) = getimagesize('images/'.$img);
?>
<img src="images/<?php echo $img?>" width="<?php echo $width?>" height=<?php echo $height?>" alt="<?php echo $heading?>">

I know it isn't professional way to include this function in every mysql queries. But When I include getimagesize function in top of the webpage , then it doesn't work. I want to include like this

<?php includes('include/connect.php') 
list($width, $height) = getimagesize('images/'.$img);
?>

From this procedure, result is empty like width='' height='' . I try creating width and height as global variables and custom function. But none of them work for me. Perhaps my little knowledge about php function doesn't work. How to globalize these width and height variable which will works all local and nested mysql queries ?

Try this?

<?php
  function imageTag($imageFile,$altText) {
    list($width, $height) = getimagesize('images/'.$imageFile);
    echo '<img src="images/'.$imageFile.
         '" width="'.$width.
         '" height="'.$height.
         '" alt="'.$altText.'">'.PHP_EOL;
  }
?>

You can use this function from anywhere.

It does, however, most certainly not globalize getimagesize results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM