Have a PHP page output a static image -


I want to be able to send a PHP 3 to 1 images, a $ _ GET [] < / Code> Parameter I now have 3 different PNG images, and want PHP script to embed in it, then return the specified picture, therefore, I need a PHP script instead of 3 images Is it possible is? I do not need to create special images on the fly, just print one of them Thank you!

If your images are in the file, then use the PHP function, and before that a content- Type the header to output it:

  & lt ;? Php $ imagePaths = array ('1' = & gt; 'file1.png', '2' => 'file2.png', '3' = & gt; file3.png ',); $ Type = $ _GET ['IMG']; If (isset ($ imagePaths [$ type])) $ $ imagePath = $ imagePaths [$ type]; Header ('content-type: image / png'); Readfile ($ ImagePath); } And {header ('HTTP / 1.1 404 file not found'); The echo 'file was not found.'; }? & Gt;  

Edit: You can also embed your images in the script by encoding them. , Then embed them in PHP as a string, then decode it to distribute them there:

  & lt ;? Php $ imageData = array ('1' = & gt; '...', // Base64-encoded as data string ...); $ Type = $ _GET ['IMG']; If (isset ($ imageData [$ type])) {header ('content-type: image / PNG'); Echo base64_decode ($ imageData [$ type]); } And {header ('HTTP / 1.1 404 file not found'); The echo 'file was not found.'; }? & Gt;  

You can also use PHP to encode the image on the command line. Simply execute this PHP script in the command line ( php script.php image1.png image2.png image3.png gt; output.php ) and save your output, and include it in your script:

  & lt ;? Php $ imageData = array (); Forex Currency ($ argv $ index => $ imagePath) $ imageData [(string) ($ index + 1)] = base64_encode (file_get_contents ($ imagePath)); Echo '$ imageData =' .var_export ($ imageData, true). ';'; ? & Gt;  

Comments