I have a server-side script which generates an image in response to an AJAX call.
In order to show the image on the page, I am encoding it with Base64.
However, I am not sure what to put in the "content-type" header.
"image/png" seems wrong because the content is actually text which encodes a png, and not actually a png.
"text/plain" seems wrong because it implies plain text and also nginx will try to compress it which is a waste of time as the compression savings will be minimal.
What is the actual correct way to go?
(Also, could I send it as a png and let the client convert it to base64?)
I'm seeing some images online that end in .png but appear as GIF. How is this possible?
Example:
https://www.khanacademy.org/computer-programming/loading/6267221601681408/5689792285114368.png
This is a GIF file, with an .png extension. Though the extension is "wrong", many image viewers (including browsers) can still it interpret them correctly because they don't believe blindly what the extension says (remember that the "extension" is just a hint), but they look into the image content. The first bytes of most common image formats allow to easily identify the image type. In this case, you can check (looking at the image content, say, in some hexadecimal editor/viewer) that the file content starts with the ASCII characters "GIF89a".
I have an image encoded in base64 and in an HTML page.
Example:
<img src="data:image/jpeg;base64,/9j/4S/+...">
Simple question:
Is the above code cached or is it loaded every time the page loads? I find conflicting answers when I research the topic.
By "the above code cached" you mean if the image is decoded each time or if the browser keeps a decoded image cache in memory? If so, to me the answer is no, it won't keep the decoded image, only the data to decode the image again (the whole HTML in this case). Maybe I'm wrong, but that's what I will expect.
I interested to work with data types and file formats.
For example I want to open a jpeg file with php and work with it.
For example get the size, or change it to black and white without any library.
I want to know that how can decode bytes of a file and get information about it?
I opened a jpeg file with HxD and saw some data in hexadecimal.
Please give me a reference to know more about files and structures...
Sorry for bad English.
Thanks a lot ...
A lot of image files are encoded using the Exchangeable Image File Format
In PHP you could use something like this method:
http://php.net/manual/en/function.exif-read-data.php
That will allow you to have access to the different properties of an image which are stored in the image header, such as resolution, endianess, etc. which you can then use to read in the raw image data.
The raw image data is usually stored immediately after the image header.
Here is the spec for the Jpeg File Interchange Format (JFIF):
http://www.jpeg.org/public/jfif.pdf
Also, in PHP if you're just reading raw image data you would use:
$file = 'picture.jpg';
readFile($file);
and you can then display it in a browser using:
header('Content-type: image/jpeg');
I have a requirement where in I have to determine whether a photo is corrupted and accordingly tag it as such.
Another thing, I need is to determine if an Image has got wrong extension. What I mean by wrong extension is that sometimes I have come across a photo that has extension of jpg but when I load this photo into IrfanView it reports that the photo is in different format that the extension.
How can I do this in Delphi.
I have a requirement where in I have to determine whether a photo is corrupted and accordingly tag it as such.
You can try some things, but with certain file formats (example: BMP, JPEG to some extent) only a human can ultimately decide if the file is OK or corrupted. The simplest test is to simply load the file into a corresponding object (TJpegImage, TPngObject, etc). If you get an exception while loading you've surely got a corrupted file. Unfortunately if no exception is raised you can't really say the file is not corrupted. I've seen corrupted JPEG files that load just fine into a Delphi TImage and can be opened with Windows's Image Viewer, but are obviously corrupted to a human observer. With BMP images it's even clearer: open up a bitmap, overwrite some bytes in the middle of the file and then open it in a viewer. How can any automated system tell those wrongly colored bits in the middle of the bitmap are actually wrong?
Another thing, I need is to determine if an Image has got wrong extension. What I mean by wrong extension is that sometimes I have come across a photo that has extension of jpg but when I load this photo into IrfanView it reports that the photo is in different format that the extension.
How about doing some of the same, trying to load the file into the object that corresponds to it's extension, and if you fail, try opening up with some other formats? This should be easy.
Alternatively you can investigate image headers: Most file formats start with a short signature, a few bytes. You can look up the documentation of all image file formats and find the signature, or you can simply open up an large number of files and look for a pattern in the first 4 bytes. I'd go for this second alternative since finding proper documentation for all image file formats might be a challenge.
The only way to check if file is corrupted is to try reading it as it is described in file format, ie. load BMP as BMP with reading BMP header, BMP data etc. There are many web pages that describe graphics file formats. Of course if you transmit files and are afraid that it will be corrupted after transmitting then save such files with some sum like CRC32, or even cryptographic MD5 or SHA1. Then after transmitting check if calculated sum is the same as original.
In Delphi there is unit jpeg and types TJPEGImage and TBitmap. Try loading it with data and check exception. For others formats there are many libraries, just look for required file formats.
To check if file extension is good try reading some first bytes of file and check it with some dictionary of graphics file headers. For example GIF files should start with GIF, BMP files starts with BM, and in JPEG header you will find JFIF. I think unix utility file works this way.
Since you used the term "requirement", I suspect that you're doing a job for someone, possibly as a contract. So make sure that you nail the requirements before worrying about the code.
IMO, you need to get samples of test cases. As others mentioned, failure to load the file as a particular format will be one test. But what about a .jpg that loads ok, but the bottom third is missing? Or a .jpg that loads ok but has green "static" lines in the middle where an error occurred upstream somewhere (on the camera, photoshop, whatever) but then the processing recovered and resumed? In this case, the .jpg may really have green lines in it. Is that considered "corrupt" or not? This is where you need to be careful, especially if it's a contract job.
I have handled this situation by reading the suspicious image and trying to getting its shape. The task is done within try-except block. Following is the code:
import cv2
image = cv2.imread('./image.jpg')
try:
dummy = image.shape # this line will throw the exception
except:
print("[INFO] Image is not available or corrupted.")
This approach should cover all your needs like:
Detecting a corrupted image
Non-image file with an image-type extension detection
Missing image detection etc.