1

I would like to add images to a dynamically generated page (I use my own template system) with PHP.

NOTE: I regulate image access for security reason.

The folder that contains the images is above the site root, therefore not accessible by HTML links.

I believe there is a method in which PHP returns a file as a resource, specifying the type in a header, and (correct me if I am wrong) a function specifically designed for that imagejpeg().

Please advise, and if possible write a simple example.

Dean
  • 7,814
  • 8
  • 30
  • 31
  • 1
    You are correct, PHP can incorporate external and local files and then output them without giving away their source, outputting them setting the PHP headers. See the various examples of imagejpeg on the PHP.net manual – Martin Mar 13 '15 at 12:40
  • What is your template system? And your question sounds more like "How to output images via a PHP page" because this is not a controlled access situation, as in, hotlinking etc. – Martin Mar 13 '15 at 12:41
  • Cant you just read it with `readfile($location);` after setting the headers? – Jelle de Fries Mar 13 '15 at 12:42
  • @Martin: I think you are right, it is misphrased, I'll edit it. – Dean Mar 13 '15 at 12:43
  • @JelledeFries I think OP would need some advice about setting headers, but in principle, yes. – Martin Mar 13 '15 at 12:44
  • Could you write a simple example please? – Dean Mar 13 '15 at 12:48
  • 1
    http://stackoverflow.com/questions/1851849/output-an-image-in-php, I could put it in an answer but you know that feels dirty :) – Jelle de Fries Mar 13 '15 at 12:49
  • @JelledeFries: Tanks, and +1 for decency. – Dean Mar 13 '15 at 12:51
  • 1
    You can find all the headers types at: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields, Also take a look at the second answer from my link: http://stackoverflow.com/questions/1851849/output-an-image-in-php/1851873#1851873 – Jelle de Fries Mar 13 '15 at 13:11

1 Answers1

2

What you need to do to output image files is in order:

PHP loads the image from the file, this is file_get_contents or otherwise fopen to open and access the file itself. If the file is a specific image file you can open the file with imagecreatefromjpeg() which will do just that, generate an image file from a JPEG source.

Then, once the file is loaded from anywhere on your filesystem, including directories outside of your web root, PHP can output the data caught in point 1, above, with some HTTP Headers and direct reference to the loaded image.

NOTE: this means that the sole output of this PHP file is the image, so file.php === image.jpg in this case.

So a brief example:

image is stored in /home/images/image1.jpg

PHP file runs from /home/site/imagecall.php

PHP file says:

<?php
if (file_exists('/home/images/image1.jpg')){
    $image = imagecreatefromjpeg('/home/images/image1.jpg');
    if ($image){
         header('Content-Type: image/jpeg');
         imagejpeg($image);
         imagedestroy($image);
    }  
    else {
        die("Image could not be loaded");
    }
}

This is a starting point for you and by no means an absolute guide. Explore.

Useful references:

http://php.net/manual/en/function.imagecreatefromjpeg.php

http://php.net/manual/en/function.file-get-contents.php

Martin
  • 22,212
  • 11
  • 70
  • 132