Search code examples
phpfunctionglobal-variablesgetimagesize

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 ?


Solution

  • 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.