【问题标题】:Issue while trying to display a remote image (GET php) - corrupt/empty image尝试显示远程图像(GET php)时出现问题 - 图像损坏/空
【发布时间】:2019-03-22 10:30:55
【问题描述】:

我想使用 GET 方法(3 个参数:Key、ImageID、Size)显示来自 php url 的一些远程图像。 例如:http://www.example.com/getImage?Key=thekey&ImageId=2&Size=1

我关注了这个:https://stackoverflow.com/a/12629513(将内容类型替换为 jpeg),尝试使用网络上的随机 jpeg 图像并成功了。

但是,当我使用我的 php url 尝试此方法时:文件已损坏。

<?php
function img_create($filename, $mime_type)
{
  $content = file_get_contents($filename);
  $base64   = base64_encode($content);
  return ('data:' . $mime_type . ';base64,' . $base64);
}
?>
<img src="<?php print img_create('http://www.example.com/getImage?Key=thekey&ImageId=2&Size=1','image/jpeg'); ?>" alt="random logo" />

任何想法..?

【问题讨论】:

  • html 输出是什么?你真的得到一个base64字符串吗?

标签: php get


【解决方案1】:

试试这个:

<?php

function img_create($filename, $mime_type) { 
   $content = file_get_contents($filename); 
   $base64   = base64_encode($content);
   $src_string = 'data:'.$mime_type.';base64,'.$base64;
   echo $src_string;
}
?>

<img src="<?php img_create('http://www.example.com/getImage?Key=thekey&ImageId=2&Size=1','image/jpeg'); ?>" alt="random logo" />

【讨论】:

  • 非常感谢!那是缺失的: $src_string = 'data:'.$mime_type.';base64,'.$base64;回声 $src_string;
【解决方案2】:

您的代码运行良好只是,我认为您的图像 URl 存在一些问题

<?php

函数 img_create($filename, $mime_type) {

$content = file_get_contents($filename);

$base64 = base64_encode($content);

return ('data:' . $mime_type . ';base64,' . $base64);

}

?>

<img src="<?php print img_create('https://images.unsplash.com/photo-1497316730643-415fac54a2af?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80','image/jpeg'); ?>" alt="random logo" />

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多