我有这个代码,应该从获取url我相信它是有效的,因为它确实输出了一些东西(不是错误)
<?php
$url = $_GET['image'];
$image = imagecreatefromstring(file_get_contents($url));
header('content-type: image/jpeg');
echo "<img scr='"" . imagejpeg($image, null, 100) . "'" />";
?>
虽然它输出的不是图像,而是文本…也许问题出在AJAX处理代码中:
function showImage(str) {
if (str.length == 0) {
document.getElementById("show_image_input").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("show_image_input").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","parts/display_input.php?image="+str,true);
xmlhttp.send();
}
}
但它做它的工作(输出图像,如果url是有效的)…有什么办法吗?
imagejpeg()
输出jpeg的原始二进制数据,例如从JFIF.....
开始的垃圾。<img>
html标签期望一个URL指向要加载的文件的位置。这段代码永远不能正常工作。
试
image.php:
<?php
$url = $_GET['image'];
$image = imagecreatefromstring(file_get_contents($url));
header('content-type: image/jpeg');
imagejpeg($image)
html: <img src="image.php?image=kittens.gif" />
。