I have an image from a blob
database field.
is there any way to check the mime type of a field without开发者_如何学JAVA saving?
getimagesize() and mime_content_type()
requires filename...
i want to do something like this:
<img \n" . 'src="data:image/gif;base64,' . $base64 .
'" alt="base64 img" width="80" height="15" />
When you insert file data into your database, you really should be saving the MIME type with it. Detection isn't perfect, and is at least slower.
In any case:
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
From: Detecting image type from base64 string in PHP
You can inspect the first few bytes in a binary file to determine this. This is called inspecting the "file signature".
Here are some helpful links that I used to write my own code that does this:
- bitmap images - Ruby on Rails: How do you check if a file is an image?
- GIF and JPG images - http://www.garykessler.net/library/file_sigs.html
- PNG images - http://www.w3.org/TR/PNG/#5PNG-file-signature
As far as I'm aware of you cannot do this. It is actually the reason you should always store the image type in the database together with the blob image data.
But basically you can just print the blob data and set your header to show the image in your browser.
header ("Content-type: image/jpeg");
$sql = "SELECT blobfield FROM table WHERE ...";
$result = mysql_query($sql, $conn);
$img = mysql_fetch_row($result);
print $img[0];
It's possible, but you would have to know something about the image types you may encounter. For example, GIF says 'GIF' in the first three bytes of the data.
http://www.onicos.com/staff/iz/formats/gif.html
Using this information you could check the stream from the blob and act accordingly. Not sure if you care about that much work, but it's doable.
精彩评论