I have an image from a blob database field.
is there any way to check the mime type of a field without 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.
Related
<?php
header("Content-type: image/jpeg;charset=utf-8'");
$path = 'example/source.jpg';
$da = file_get_contents($path);
$base64 = base64_encode($da);
$src = 'data:image/jpeg;charset=utf-8;base64,'.$base64;
echo '<img src="'.$src.'">';
?>
php v5.6.2
I tired copying the $src value in debug and pasted in img src value. still its not showing up.
what did i missed here?.
thanks in advance
header("Content-type: image/jpeg;charset=utf-8");
here you say to the browser i will send you an jpeg image,
then:
echo '<img src="'.$src.'">';
here you send HTML.
because you said it was a jpeg image, the browser will try to render your html as jpeg. since the ascii text-based HTML format is completely incompatible with the binary based jpeg-format, the browser will fail horribly when trying to render your image, and fail with some error (probably image is corrupt or something like that.)
you can either fix your Content-Type header to specify that you're sending HTML, then the browser will (probably successfully!) try to render it as such, eg:
header("Content-type: text/html;charset=utf-8");
or you can modify your code to actually send the image as jpeg, eg:
<?php
header("Content-type: image/jpeg");
$path = 'example/source.jpg';
readfile($path);
(btw a base64 encoded jpeg image will be about 33% larger than just the raw jpeg image, so if you want a fast pageload, or you want to save up on bandwidth, or you want to save up on ram, using readfile() is faster, requires less bandwidth, and requires less ram, both for the server and the client, compared to your embedded base64 approach.)
So maybe your problem is in your mime type. then try this code two solve:
$path = 'domain.com/example/source.jpg';
$content = file_get_contents($path);
$file_info = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($path));
$base64 = base64_encode($content);
$src = 'data:'.$mime_type.';charset=utf-8;base64,'.$base64;
echo '<img src="'.$src.'">';
Note: its better to use path from full address domain, if you want to use from path use readfile()
I have the following code in PHP to take the screenshot of first page of the PDF.
$name = getcwd()."\\testfile";
$img = new imagick();
$img->setResolution(200,200);
$img->readImage($name.'.pdf[0]');
$img->setImageResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_LANCZOS,1);
$img->setImageCompression(\Imagick::COMPRESSION_ZIP );
$img->setImageCompressionQuality('0');
$img->setImageFormat('png8');
$img->writeImage($name.".png");
header("Content-type : image/png");
echo $img;
This code produces the PNG of 62kb only in the Google Chrome's Resource monitor tab. But the image which is written by Imagick() is above 114kb. Just to make sure image isn't compressed and or any other issues i have used a online service called TinyPNG and they compressed the image shrinking it to exactly 62kb i get in browser...
What could be wrong in this code? Also i am using PNG8 format because thats more efficient.
Best
Ahsan
I think this is caused by your writeImage statement. If you write a PNG image without specifying png8: specifically in the filename your image will not be stored in that format. In essence setImageFormat will only affect when you retrieve the image as a string (echo $img).
If you do the following:
$img->writeImage ('png8:' . $name . ".png");
it should be stored as a png8. You can verify this with identify -verbose and checking the Depth / Channel Depth.
These are the default compression methods used for the following common image formats:
PNG: Imagick::COMPRESSION_ZIP
JPEG: Imagick::COMPRESSION_JPEG
GIF: Imagick::COMPRESSION_LZW
i'm trying to get an image from database using the following code
$this->autoRender=false;
$blob = $this->GeneralNews->findById($id,array("image_data"));
$image = imagecreatefromstring($blob["GeneralNews"]["image_data"]);
ob_start(); //You could also just output the $image via header() and bypass this buffer capture.
imagejpeg($image, null, 80);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpg;base64,' . base64_encode($blob["GeneralNews"]["image_data"]) . '" />';
the result can be seen in the next url
http://www.thedarkdimension.com/generalNews/displayImage/1678
but when i try to get this via iOS in
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
the image that i get is empty although there is a data in (imageData)
i can't figure out the problem but it's most probably came from the PHP side .. cause i tried this URL
http://www.johnquarto.com/wp-content/uploads/2008/08/gag.jpg
and it's ok with it.
Having had a very quick look it appears that what is returned from the URL http://www.thedarkdimension.com/generalNews/displayImage/1678 is not an image but a document claiming to be of type "text/html". That document isn't actually valid HTML but just contains a single img tag with the source attribute being the image base64 encoded. That makes sense given your PHP code.
I suspect what imageWithData is looking for however is just the jpeg in all its binary goodness.
Therefore you need to change your PHP to set the MIME type to image/jpeg and to just echo the jpeg data as it is - not base64 encoding required. I haven't looked at PHP in so long I don't dare give you example code but hopefully thats enough to get you going.
I stored it images in the database using an BLOB field (I'm using SQLite). Now I want to recover this image to a HTML page and show the images there.
I can retrieve the binary data from the image from the database, but what I can do to transform this data in an image and show in the page? Currently I want to show the images inside a field in a table.
You could abuse the data: protocol, but trust me, you don't want that if you can avoid it. Normally, you create a separate php-script that serves images, so in script 1:
<img src="/myimagescript.php?id=1234">
In myimagescript.php:
//get the data from the database somehow (mysql query et al.)
//let's assuma the data is in $data
header('Content-Type: image/jpeg');//alter for png/gif/etc.
echo $data;
#uscere90 is right, but an example might help (example of a PNG image):
<?php
header("Content-type: image/png");
echo $image_data;
?>
Typically this is done by creating a wrapper script or function that retrieves the BLOB and delivers it with the appropriate content headers to be used as an <img src=''>
Doing it this way also gives you the benefit of being able to deliver or not deliver the image based on other authentication factors determined by your PHP. If, for example, a user doesn't have permission to see an image, you can instead show some default or blocking image in its place.
// File getimg.php
// Retrieve the image blob specified by $_GET['imgid'] from the database
// Assuming your blob is now in the variable $image...
header("Content-type: image/jpeg");
// Just echo out the image data
echo $image;
exit();
Now in your html:
<img src='getimg.php?imgid=12345' alt='this is your img from the database' />
You can create a simple image.php page that queries your database, then prints out a content-type relevant to the image and vomits the binary data to screen. So, in your table, you'd have <img src=image.php?id=something />, and then you'd use that id in your image.php page to do your database lookup, retrieve the binary data, and print it to screen after printing the content-type header.
image.php:
<?php
header('Content-type: image/jpeg');
//DO SQL NINJA STUFF HERE
echo mysql_result($result,0,"file_content");
?>
There are two options I would say:
You create a script that returns the image data. The <img src="-field then calls that script.
You offer the data of the images directly via a data url.
Both have it's pros and cons. For the first solution you must create a new script for the images. The second method will bloat your page if the images are large.
As there are examples for the image script method already, here is some code fragment for data URIs:
<?php
function data_uri($content, $mime)
{
$base64 = base64_encode($content);
return ('data:' . $mime . ';base64,' . $base64);
}
?>
<img src="<?php echo data_uri($content,'image/png'); ?>" />
You need to set the mime-type according to your image, image/png for PNG images, image/jpeg for JPG files etc., see here for a list.
GD seems to have various imagecreatefrom_x_ functions which is great if you know the image type ahead of time. In my particular situation, I don't. Is there a way to read in the image without knowing the type?
My only option so far is to save it to disk (expensive!) and then use getimagesize (which also provides mime type) - but this results in me reading in the image twice - once to determine the MIME type and then again to read it into GD.
Alternatively, is it possible to just treat a variable as a file? something like this:
$image = file_get_contents('http://the.remote.file.com/myfile');
# doesn't work with already read in image ... can I treat this as a file w/o saving it?
$info = getimagesize($image);
You can use imagecreatefromstring(), which is indifferent to the actual file format and the best option if you've read in the file already.
But for practical purposes you should be able to use getimagesize($filename). You don't have to read in the file before. Just use the filename or url.
print_r(
getimagesize("http://sstatic.net/stackoverflow/img/sprites.png?v=2")
);
Use imagecreatefromstring, imagesx and imagesy (the latter two to prevent another request, since it seems to concern you):
$image = imagecreatefromstring(file_get_contents('http://path/to/image.jpg'));
list($width, $height) = array(imagesx($image), imagesy($image));