0

我正在尝试显示图像以及数据库中的其他信息。

PHP

  <?php
               mysql_connect("localhost","111","222") or die("Could not connect to localhost");
               mysql_select_db("movies") or die( "Could not connect to database");

               $result = mysql_query("SELECT * FROM allmovies");
               if ($result == false )  die(mysql_error()); 
               while($row = mysql_fetch_array($result))
               {
                    echo "<img src=' . $row['image'] . '>";
               ?>

喜欢:

标题:Blah
价格:Blah
图片:<img src=rarara">

全部来自 MySQL 的一页?

4

2 回答 2

2
  1. Don't store image data in a database, they are generally not suited to this and incurs extra overhead on your MySQL connections returning the data. You should be storing the path to the image file, and serving that.
  2. If you insist on doing it you you should only be returning one image at a time with the proper header based on the image type using something like the following:

    $imagedata = data_from_mysql();
    header('Content-Length: ' . sizeof($imagedata) );
    header('Content-Type: image/png');
    echo $imagedata;
    exit;
    
  3. If you really want to make your page source bloated, slow, unmanageable, and nigh-uncacheable:

    while( $imagedata = data_from_mysql() ) {
      echo "<img src='data:image/png;base64," . base64_encode($imagedata) . "'>";
    }
    

I cannot stress enough how these are terrible ideas that you should not use, but if you cannot listen to reason you can at least do bad things the right way.

于 2013-10-04T17:56:11.350 回答
1

你可以使用imagecreatefromstring()

$im = imagecreatefromstring($row['image']);
if ($im !== false) {
    ob_start();
    imagejpeg($im);
    $data = ob_get_contents();
    ob_end_clean();
    echo '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '" />';
}

只是我的意见,但是将图像保存到文件服务器然后将路径的引用而不是整个图像存储为 blob 可能会更理智一些?

于 2013-10-04T18:02:26.217 回答