【问题标题】:Retrieving height and width of image from MySQL db从 MySQL db 中检索图像的高度和宽度
【发布时间】:2012-08-10 13:27:14
【问题描述】:

我在 MySQL 表中有图像位置的 url。我如何检索该图像的高度和宽度并将其插入到下面的 img 标签中?

<img id="profileImg" alt="" height="96" width="87" src="<?=base_url().$row1->imgURI?>" />

【问题讨论】:

    标签: php mysql html codeigniter


    【解决方案1】:

    http://php.net/manual/en/function.getimagesize.php

    <?php
    list($width, $height, $type, $attr) = getimagesize(base_url() . $row1->imgURI);
    ?>
    <img id="profileImg" alt="" height="<?=$height?>" width="<?=$width?>" src="<?=base_url().$row1->imgURI?>" />
    

    此外,如果您可以导出图像文件的本地路径以避免必须通过其 url 访问文件,那就太好了。但我会留给它。

    【讨论】:

    【解决方案2】:

    例如通过使用 GD 库函数 getimagesize():http://php.net/manual/en/function.getimagesize.php

    【讨论】:

      【解决方案3】:

      这是使用getimagesize 函数最简单的检索方法:

      list($width, $height, $type, $attr) = getimagesize ( base_url() . $row1->imgURI );
      <img id="profileImg" alt="" <?=$attr?> src="<?=base_url().$row1->imgURI?>" />
      

      【讨论】:

        【解决方案4】:

        如果您没有实际的widthheight 存储在数据库中,您可以使用getimagesize()

        <?php
          $image = base_url().$row1->imgURI;
          $size = getimagesize($image);
        ?>
        <img id="profileImg" alt="" height="<?=$size[1];?>" width="<?=$size[0];?>" src="<?=$image;?>" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-15
          • 2014-12-01
          相关资源
          最近更新 更多