By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Serve an image with php

P: 54
Hi,

I have an image tag in my html with a src pointing to a php script.
The php script finds the url of the image from a paramater in the query string.
How can I return the image data to the html page?

[HTML]<img src="phpScript.php?id=12321321/>[/HTML]

[PHP]<php?
$id = $_REQUEST["id"];
$img_url = someFunction($id);
echo imageData????
[/PHP]

Thanks
Sep 9 '08 #1
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
Assuming that the picture is stored somewhere (so you only need the path to it).
Then it would be enough to echo the file path.
[PHP]$id = $_GET["id"];
$img_url = someFunction($id);
echo $img_url;[/PHP]
but why calling another script file... since you're starting from a php file anyway you could do also
[PHP]include_once("phpScript.php");
echo '<img src="' . someFunction(12321321) . '" alt=""/>';[/PHP]
regards
Sep 9 '08 #2

P: 54
Your first solution doesn't work because the html image tag expect the image data and not the image url.

I can't use your second solution because I'm generating the image tag dynamicaly on the client side.

I finally used the header function:
[PHP]header('Location:'.$image_url);[/PHP]

The problem is that I need to cache the image.

Can I load the image data in the php script, add expire header and then echo it?

Thanks

Assuming that the picture is stored somewhere (so you only need the path to it).
Then it would be enough to echo the file path.
[PHP]$id = $_GET["id"];
$img_url = someFunction($id);
echo $img_url;[/PHP]
but why calling another script file... since you're starting from a php file anyway you could do also
[PHP]include_once("phpScript.php");
echo '<img src="' . someFunction(12321321) . '" alt=""/>';[/PHP]
regards
Sep 9 '08 #3

Markus
Expert 5K+
P: 6,050
Generally when people link to a php file in an image URL use header()s to output the image. Something like this:

[php]

$image = $_GET['id'];

header("Content-type: image/{$img_ext}");
readfile("/path/to/my/images/{$image}");
exit(0);

[/php]

Obviously you would have to sanitize the input coming from the URL (google: mysql injection). And then you would have to use the correct 'img_ext' - perhaps you will store this in a database and use the image name to locate it in the database,
Sep 9 '08 #4

100+
P: 258
Hi
I'm not sure about your issue but I know that it's possible to output pictures with PHP GD.
You should have seen many websites using (turing numbers). When you register on some websites . On the registration page they ask you to put a number (shown on an image) in a text field.. The format of the image could be JPG, PNG or GIf but when you right click on the image and go to properties you see the address is a .php file.
PHP GD is a library built in PHP to work with images. It could easily open any photo and resize or edit edit and then save it to a file or just output it as it is.

You can have an image tag in your HTML like this:
<img src="www.yourdomain.com/anyfile.php">

and then use PHP GD to output an image.

Let me know if it's what you need. I can help you more with it. Or you can go to www.php.net and search for GD.

Good luck
Sep 9 '08 #5

P: 54
I manged to get the image using curl and then send it to the user.
The problem is I can't get IE to cache the image.
FF works fine.

Any ideas?

[PHP]// Allocate a new cURL handle
$ch = curl_init($user_thumb);
if (! $ch) {
die( "Cannot allocate a new PHP-CURL handle" );
}

// We'll be returning this transfer, and the data is binary
// so we don't want to NULL terminate
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

// Grab the jpg and save the contents in the $data variable
$data = curl_exec($ch);

// close the connection
curl_close($ch);

// Set the header to type image/jpeg, since that's what we're
// displaying
header("Content-type: image/jpeg");

header('Expires: '.gmdate('D, d M Y H:i:s', time()+100000).'GMT'); */

Header("Cache-Control: max-age=3600, must-revalidate");
[/PHP]
Sep 9 '08 #6

100+
P: 258
Hi dolittle

Here is what you can do.

Put the code bellow in a php file. in you HTML code use this PHP file as your SRC for your IMG tag

Expand|Select|Wrap|Line Numbers
  1.    <?PHP
  2.     // use this file as it is if your image is a JPG
  3.     // if the image in a GIF use imagecreatefromgif
  4.     // if the image in a PNG use imagecreatefrompng
  5.  
  6.     $img=imagecreatefromjpeg("yourImage.jpg");
  7.     imagePNG($img);
  8.     imagedestroy($img);
  9.    ?>
  10.  

and the HTML code will be:
Expand|Select|Wrap|Line Numbers
  1. <img src="phpfile.php">
  2.  

But before you use this code. Go to your php settings and make sure GD is enabled. use phpinfo()

Hope this helps
Sep 9 '08 #7

100+
P: 258
I forgot to say.
There are many websites using this method to output their pictures. It usualy is a good choice for websites with user profiles because this method helps them hide the real address of the image . one of the best examples is Facebook.com
Sep 9 '08 #8

Markus
Expert 5K+
P: 6,050
Hi dolittle

Here is what you can do.

Put the code bellow in a php file. in you HTML code use this PHP file as your SRC for your IMG tag

Expand|Select|Wrap|Line Numbers
  1.    <?PHP
  2.     // use this file as it is if your image is a JPG
  3.     // if the image in a GIF use imagecreatefromgif
  4.     // if the image in a PNG use imagecreatefrompng
  5.  
  6.     $img=imagecreatefromjpeg("yourImage.jpg");
  7.     imagePNG($img);
  8.     imagedestroy($img);
  9.    ?>
  10.  

and the HTML code will be:
Expand|Select|Wrap|Line Numbers
  1. <img src="phpfile.php">
  2.  

But before you use this code. Go to your php settings and make sure GD is enabled. use phpinfo()

Hope this helps
Your solution isn't too helpful. What if GD isn't enabled? What if the image isn't a gif or png?
Sep 9 '08 #9

100+
P: 258
Your solution isn't too helpful. What if GD isn't enabled? What if the image isn't a gif or png?
As you can see in the script it even works with JPG. And about enabling GD it usualy is enabled on every servers with php version older than 4.0.

I'm not the one telling you to use GD but there are millions of websites out there which use GD to output, resize or edit photos dinamicaly.

what would happen if websites like Facebook, Hi5, ebay, Flikr etc had to manualy open uploaded files (over thousands an hour) and resize or edit them?


It's your choice

good luck.
Sep 9 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.