0

I have an image in a directiory, lets say: test.jpg.

I also have 2 other PHP file in this directory called: new.php and load.php

Now, what I want to do is, when people visit the website new.php, they should need to see the test.jpg image.

However! I would also need that if I place the new.php into an <img> tag, it should also display the image correctly.

I have tried to call the image in an img tag, but it doesn't load correctly, when I insert the new.php into img tags.

<img src="test.jpg">

How is that even possible to make a php work as an image - if you know what I mean?

Radical_Activity
  • 2,618
  • 10
  • 38
  • 70

2 Answers2

7

using:

header('Content-Type: image/jpeg');
readfile('path/to/image/screenshot.jpg');

other variants:

@readfile('path/to/image/screenshot.jpg');

won't have any errors if file does not exist.

AND

readfile('path/to/image/screenshot.jpg') or die('Image file not found');

will kill your script with a predefined error message if file does not exist.

The most useful alternative, as suggested by @hd:

$file = 'path/to/image/screenshot.jpg';
readfile(file_exists($file) ? $file : 'path/to/image/image_not_found.jpg');

Which will choose $file or if it does not exist, the default image_not_found.jpg

You may need to change the mime-type in the header() for other image types.

Note: The other answer is very much as functional as this one, but this one uses no memory and in general is faster and more flexible than the other answer.

t3chguy
  • 1,018
  • 7
  • 17
  • I'd also check that the file actually exists, before reading it. You know, for those rare occasions. – ʰᵈˑ Jul 23 '14 at 12:48
  • Thank you this is working very well. This is what I was looking for. – Radical_Activity Jul 23 '14 at 12:48
  • @hd, as much as I hate them; in this case `@readfile();` would be simplest. – t3chguy Jul 23 '14 at 12:49
  • Ew. Suppressing errors on production is a big no-no, in my books. A simple `file_exists` should suffice! – ʰᵈˑ Jul 23 '14 at 12:49
  • Without testing, wouldn't the second alternate work? – t3chguy Jul 23 '14 at 12:51
  • It would, but you could check if the file exists first, and if it doesn't display a "Image not found" image, rather than destroying the user experience with a dead image. – ʰᵈˑ Jul 23 '14 at 12:54
  • you could die an Image not found image, while it really wouldn't look nice on the backend, having the raw bytes in the string. – t3chguy Jul 23 '14 at 12:56
  • 2
    I was thinking of `$imagePath = file_exists('path/to/image/screenshot.jpg') ? 'path/to/image/screenshot.jpg' : 'path/to/image/image_not_found.jpg';`. This way it will render an image, and not show the [broken image link](http://www.underconsideration.com/brandnew/archives/google_broken_image_00_a_logo.gif) – ʰᵈˑ Jul 23 '14 at 12:59
  • For the sake of argument I will add a ternary statement into one of the alts. – t3chguy Jul 23 '14 at 13:08
1

Try this:

new.php

header("Content-Type: image/jpeg");

$im = imagecreatefromjpeg("test.jpg");

imagejpeg($im);
imagedestroy($im);

and use it like this

<img src="new.php"/>
simeg
  • 1,889
  • 2
  • 26
  • 34
  • I'm guessing the `imagejpeg(imagecreatefromjpeg())` would be more CPU costly then a simple `readfile()` – t3chguy Jul 23 '14 at 12:46
  • Yes. But it's one way of doing it :) – simeg Jul 23 '14 at 12:46
  • 3
    +1'ing because it's an alternative to @t3chguy's approach. To make it valid HTML, add `/>` at the end of the `img` tag - closing the tag properly. – ʰᵈˑ Jul 23 '14 at 12:55