Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Reading an image from a URL

Posted on 2006-10-26
Medium Priority
Last Modified: 2012-05-05
I'm new to PHP and need help to read an image from a site and convert it to .PNG format.
In the first part, I tried the file_get_contents and readfile without success. I receive an error message (The specified CGI application misbehaved by not returning a complete set of HTTP headers.)

Please help
Question by:yelsherif
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 11

Accepted Solution

Joseph Melnick earned 375 total points
ID: 17824355
Hello yelsherif,

In order for you to achieve your objective you will need the GD library. This will allow you to take your source image and then do the conversion to jpg that you are interested in.
You can use the following to create an image resource from a jpeg image:
imagecreatefromjpeg -- Create a new image from file or URL
there are other functions to create an image resource from gif, png and others.  

I took some of this code from an example on

// define image url
$filename = "";
function output_handler($img) {
   header('Content-type: image/png');
   header('Content-Length: ' . strlen($img));
   return $img;
// create that image resource from a jpeg
$im = imagecreatefromjpeg ( $filename );
//    Image output

Joseph Melnick

Author Comment

ID: 17830316
Thanks for the suggested code, but how can I know if the GD library is installed on the host server?

Yasser El-Sherif
LVL 11

Expert Comment

by:Joseph Melnick
ID: 17833539
Hello yelsherif,

To find the modules loaded, as well as other useful information.


Joseph Melnick

Author Comment

ID: 17834100
aaach, the service is not installed on the server and hence I cannot do the task.
anyway the code you provided looks neat and correct, I accept your answer.


LVL 11

Expert Comment

by:Joseph Melnick
ID: 17835251
Hello yelsherif,

The GD library can be a bit of a resource hog so it is sometimes not made available.

You might ask your host to include this module.

Joseph Melnick

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question