Reading an image from a URL

Hello,
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
LVL 1
yelsherifAsked:
Who is Participating?
 
Joseph MelnickConnect With a Mentor Senior Software Developer - Pharmacy ApplicationsCommented:
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 php.net

<?php
// define image url
$filename = "http://www.yourdomain.com/images/someimage.jpg";
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
ob_start("output_handler");
imagepng($im);
ob_end_flush();
?>

Joseph Melnick
0
 
yelsherifAuthor Commented:
Thanks for the suggested code, but how can I know if the GD library is installed on the host server?

Yasser El-Sherif
0
 
Joseph MelnickSenior Software Developer - Pharmacy ApplicationsCommented:
Hello yelsherif,

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

<?php
phpinfo();
?>

Joseph Melnick
0
 
yelsherifAuthor Commented:
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.

Thanks

Yasser
0
 
Joseph MelnickSenior Software Developer - Pharmacy ApplicationsCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.