J'ai une fonction en PHP qui appelle curl pour récupérer une image. Lorsque j'imprime cela dans un fichier et que je l'examine dans le navigateur, l'image est superbe. Lorsque j'utilise "echo" des résultats de la boucle comme valeur de retour de mon script PHP, le navigateur affiche l'icône de l'image brisée (voir un exemple de cette icône: http://www.artifacting.com/blog/wp-content/uploads/2007/01/error_icon.gif).PHP écho image JPEG échoue, mais l'écriture même image au fichier fonctionne très bien. Pourquoi?
$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,
);
curl_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;
Des idées? Aidez-moi!
Vérifiez que rien n'a été accidentellement échoué avant ou pendant le réglage de l'en-tête. – BoltClock
alors l'image ne serait pas cassée, il obtiendrait une vieille erreur plaine msg – Hannes
En fait, il n'obtiendrait pas un message d'erreur. Il obtiendrait une chaîne de charabia que le navigateur interprète l'image comme ASCII – stevendesu