PHP echoing jpeg image fails, but writing same image to file works great. Why?
I have a function in PHP that calls curl to retrieve an image. When I print that to a file and examine it in the browser, the image looks great. When I use "echo" of the curl results as the return value from my PHP script, the browser shows the broken-image icon (see an example of that icon: http://www.artifacting.com/blog/wp-content/uploads/2007/01/error_icon.gif).
$ch = curl_init();
$options = array(
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => 0,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPPROXYTUNNEL => 1,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_BINARYTRANSFER => 1,
);
cur开发者_JAVA技巧l_setopt($ch, CURLOPT_URL, $url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_list);
// The http response code is 200, and the body length is 50kb.
$body = curl_exec($curl_params);
// This produces a file containing an image that looks good when viewed in a browser.
$bodyFile = @fopen("img.jpg", "w");
fprintf($bodyFile, "%s", $body. "\n");
fclose($bodyFile);
// This does not render in the browser. Instead I see the broken image icon.
$contentType = "image/jpeg";
header('Content-type: ' . $contentType);
echo $body;
Any ideas? Help!
The answer depends on what you mean by "broken". If the top half of the image is appearing but the bottom half isn't, then either you have a bad version in your cache from a dropped packet (empty cache, refresh, try again) or the script is being cut off prematurely from using too many resources or from running too long. If emptying the cache doesn't resolve the issue, check you php.ini settings and see if increasing the script's time to live or max memory resolves the issue.
If the image is a bunch of meaningless ASCII, you echo'd something or sent a header before this point in your code. The most common unseen cause of this is having a single empty line before your <?php
at the top of the page. Make sure there isn't a single byte (even non-printable!) before the <?php
if this is the case.
If the image is definitely an image file, but it's static, a gray box, random colors, etc.- then this is a problem with the content-type. Trying to parse a JPG image as a PNG will sometimes yield a gray box or other random "failure" images.
Please make sure you'll set the right cURL params.
This example works fine for me:
<?php
$ch = curl_init ("http://www.google.com/images/logos/ps_logo2.png");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$image=curl_exec($ch);
curl_close ($ch);
header("Content-Type: image/png");
echo $image;
?>
It turned out that I had another PHP file that was writing out an extra newline character. There was a newline at the end of that file after the "?>" line.
精彩评论