Most of the content of web resources today is dynamically generated by server-side scripts. But in this way, mostly text information is displayed (page layout, style sheets, sitemap). Images, music, videos, archives, as a rule, are located on the server in the form of static files. However, sometimes it becomes necessary to generate similar data using a script. So, if you need to display a static image, you can completely insert it into the script.
Necessary
the ability to create or edit server scripts
Instructions
Step 1
Insert these pictures into the script as text, which is part of the program code. Use the most convenient data structures and syntactic constructs. The choice is usually determined by the capabilities of the programming language used. So, in many cases it is convenient to insert a picture into a script as a regular character string, the content of which is image data encoded with an algorithm like Base64. In PHP it might look like this (2x2 pixel.
Step 2
Prepare the image for output. If necessary, decode the original information. You should receive a buffer containing the binary data of the image. For example, in PHP, decoding the string given in the first step might look like this: $ text = base64_decode ($ str);
Step 3
In the header of the server's HTTP response, add a field containing data on the length of the response body (the size of the displayed image). Determine the scope using functions or methods that return the length of arrays, strings, etc. For example: header ('Content-length:'.strlen ($ text));
Step 4
Add a field to the HTTP response header of the server indicating the mime-type of the transferred content. For example: header ('Content-type: image / gif'); The content type must already be known (determined based on the format of the original image).
Step 5
If you need to force the image to be saved instead of being displayed by the browser, add the appropriate field to the response header: header ("Content-Disposition: attachment; filename = my_image.gif");
Step 6
If you want to avoid caching the image by the browser, enter the Pragma and cache-Control fields with the appropriate values in the HTTP response: header ("Pragma: no-cache"); header ("Cache-Control: no-cache, must-revalidate, no- store "); header (" Cache-Control: pre-check = 0, post-check = 0 ", false); It is also worth giving the dates of the last modification and the expiration date of the resource. In this case, the second of them must be later: header ("Expires: Mon, 4 Jan 1993 00:00:01 GMT"); header ("Last-Modified:".gmdate ("D, d MYH: i: s"). "GMT");
Step 7
Form the body of the server's HTTP response, which is the image data. Use functions or methods of objects that provide output of binary data without additional processing. For example: printf ('% s', $ text);