4

Let's say I have a user enter the URL of an image.

After URL validation, etc. I want to get the image and store it in a PHP variable. Not the image path, but the actual image itself.

I am adding this image-holding variable in between other strings, so I cannot change use header() before echoing the image. Is it possible to use <img> HTML tags? I really don't want to copy the images to my own server...

How do I:

  1. Store the image in a variable such that,
  2. Echo the image from the variable without changing headers.

Edit:

I said above that I am putting this image inside another variable, e.g.:

$str = "blah blah" . $var_holding_img . "more text";

Is it possible to insert something in the string above that will be replaced with the images? Can parse the variable $str later to replace some random text like "abg30j-as" with the image...

apparatix
  • 1,492
  • 7
  • 22
  • 37
  • Hopefully this will get you pointed in the right direction: http://stackoverflow.com/questions/4110907/how-to-decode-a-base64-string-gif-into-image-in-php-html – Jeff Hines Sep 12 '12 at 22:06

4 Answers4

5

I found an answer to my own question:

First, I created another PHP file, called img.php:

<?php
$url = $_GET['imgurl'];
/*
Conduct image verification, etc.
*/
$img_ext = get_ext($url); //Create function "get_ext()" that gets file extension
header('Content-type: image/' . $img_ext);
echo file_get_contents($url);
?>

Then, in the original PHP file, I used this PHP code:

<?php
$var_holding_img = '<img src="img.php?imgurl=http://example.com/image.png"/>';
$string = "This is an image:<br \>" . $var_holding_img . "<br \>displayed dynamically with PHP.";
echo $string;
?>

This way, the PHP file "img.php" can use the proper headers and the image can be inserted as HTML into any other PHP variable.

apparatix
  • 1,492
  • 7
  • 22
  • 37
1

How do I:

Store the image in a variable such that, Echo the image from the variable without changing headers.

You can do this in two ways.

In way one, you serialize the image in a string, and save the string in the session. Which is exactly the same as saving it server side, except that now the session GC should take care of clearing it for you. Then the IMG SRC you use will redirect to a script that takes the image and outputs it as image with proper MIME type.

In way two, if the image is small enough, you can encode it as BASE64 and output it into a specially crafted IMG tag:

http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html

This saves you some time in connection, also. Of course the image must be reasonably small.

LSerni
  • 55,617
  • 10
  • 65
  • 107
-1
  1. You can't save the actual image in a variable. Either you save the URL or copy the image (what you obvious don't want) to your server and save the path to the image
  2. See answer 1, you can't echo the image itself, only link it

Edit: Okay obviously you can save images directly to a variable, but I don't recommend you to do this.

Chris
  • 4,255
  • 7
  • 42
  • 83
  • You can store images in variables. And there are ways to output the image itself into the html, but they aren't that great. – John V. Sep 12 '12 at 22:00
  • I haven't seen this method anywhere. Can you give me a link where this method is described? Couldn't find anything with google. – Chris Sep 12 '12 at 22:02
  • Basically you embed a base64 string into the page of the image, but it's messy, it doesn't like to let you save them correctly, and adds bloat to the page. http://en.wikipedia.org/wiki/Data_URI_scheme – John V. Sep 12 '12 at 22:04
-2

No, that isn't possible. If you want to serve something, it has to exist on the server.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39