473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Serve an image with php

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
9 4835
Dormilich
8,658 Expert Mod 8TB
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
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
6,050 Expert 4TB
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
bnashenas1984
258 100+
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
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
bnashenas1984
258 100+
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
bnashenas1984
258 100+
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
6,050 Expert 4TB
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
bnashenas1984
258 100+
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

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

Similar topics

2
by: mep | last post by:
Hi, After lookup in cherrypy site and google for a while, I haven't found any information about cherrypy how to serve dynamic binary file(some generated charts).Is there any easy way to do this?...
3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
1
by: Ray Schumacher | last post by:
I'm trying to make a small camera server using VideoCapture.py and socket. I needed to construct a complete image file with headers etc for a browser to recognize it, but I couldn't find a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.