Solved

Reading an image from a URL

Posted on 2006-10-26
5
205 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:yelsherif
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Joseph Melnick earned 125 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 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
 
LVL 1

Author Comment

by:yelsherif
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
0
 
LVL 11

Expert Comment

by:Joseph Melnick
ID: 17833539
Hello yelsherif,

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

<?php
phpinfo();
?>

Joseph Melnick
0
 
LVL 1

Author Comment

by:yelsherif
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.

Thanks

Yasser
0
 
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now