dscc90150010 2017-02-14 18:57
浏览 75
已采纳

PHP - 使用URL读取图像

Here my problem, I do a query to MySQL (PDO) for give me the last 5 URLs of a table nammed avatar who contains the ID and the URL :

$response = $dbh->query("SELECT url FROM avatar ORDER BY id_URL DESC LIMIT 0,5 ");

And I did :

  while ($donnees = $response->fetch())
{

$urlImage = $donnees['url']; //'url' contains the URL
$result = file_get_contents($urlImage);        
header('Content-Type: image/png');
echo $result;   
?>

But the header just return a small empty white square. However, the "$result = file_get_contents($urlImage);" takes properly the URL because when I do :

   $urlImage = $donnees['url']; //'url' contains the URL
$result = file_get_contents($urlImage);        
echo $result;   
?>

It just shows the "encodage of the image" (a ton of special characters) but not displays the image.

I also try with "imagick" but it says to me that the class doesn't exist and I don't think that the imagecreatefrompng can be use with URL.

Thanks !

  • 写回答

2条回答 默认 最新

  • dotdx80642 2017-02-14 19:01
    关注

    If you're trying to use a dynamic image source where your url is the image source, and it isn't working, then the problem might be that there's a space or extra character somewhere on the page, which will make the browser treat it like a document instead of an image in some cases.

    Your problem is that the browser isn't understanding that it's supposed to be an image.

    You could always do:

    <img src="<?=$urlImage?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络