How to display an Image from a mysql blob

前端 未结 4 2057
臣服心动
臣服心动 2020-12-12 04:23

I am trying to display an image from a MySQL blob field. I have tried a few different things and none of them seem to work.

I have tried:

  • hea

4条回答
  •  一整个雨季
    2020-12-12 05:08

    Just get the image from the database. And print it using the correct headers.

    $image = mysql_fetch_array(...)
    header("Content-type: image/jpeg"); // change it to the right extension 
    print $image['data'];
    

    For performance reasons... this is not advisable. There are several reasons to put images in databases but the most common are:

    a) keeping them indexed (duh!)
    You can do this by storing the images flat on the server and just indexing the image filename.

    b) keeping the image hidden/protected
    Flickr and alike still store the images flat on the server and use a different approach. They generate a URL thats hard to find.

    This link points to a protected image on my account. You can still access it once you know the correct URL. Try it!

    farm2.static - a farm optimized for delivering static content
    1399 - perhaps the server
    862145282 - my username
    bf83f25865_b - the image

    In order to find all my secret images any user can hard hit Flickr with the above address and change the last part. But it would take ages and the user would probably be blocked for hammering the server with thousands of 404s.

    That said there is little reason to store images on BLOBs.

    Edit:
    Just a link pointing to someone that explained much better than I did why BLOB is not the way to go when storing images.

提交回复
热议问题