1

我想通过http://placehold.it/500x500 之类的 URL 返回图像。我有我的 URL http://example.inc/assets/image/35345,它在控制器上调用一个动作。控制器从数据库中获取一些数据(名称、id 等)以及图像内容的二进制字符串。

在前端站点上,我有我的 img 标签,我想在我的 src 属性中调用 url。

<img src="http://example.inc/assets/image/35345">

更多信息,我使用 slim PHP Framework,我的服务器是 ubuntu 13.x 系统(vagrant 等)。我是一个典型的前端开发人员并且没有很好的技能@ PHP。

以下片段有效:

$file = fopen($name, 'wb');
fwrite($file, $binaryData);
fclose($file);

但我不想在目录中生成文件。这可能吗?

编辑: Content-Type 和 Content-Length 标头已设置,这不是问题。

4

3 回答 3

0

抓取图像的内容,base_64 对其进行编码,然后返回一个 base64 图像。

$file = file_get_contents($name);
list($width, $height, $type, $attr) = getimagesize($file);
echo '<img src="data:image/'.$type.';'.base64_encode($file).'"/>';
于 2015-03-30T07:00:35.257 回答
0
  1. 您应该使用类似这样的方法将图像上传到目录中。此代码会将您的图像上传到目录中。

if ($_FILES['file']['name'] != "") {
            $filename = $_FILES['file']['name']; //getting name of the file from form
            $filesize = $_FILES['file']['size'];
            $info = new SplFileInfo($filename);
            $ext = $info->getExtension();
            $filesize1 = ($filesize * .0009765625) * .0009765625;
            if (!($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg')) {
                //set some error message and redirect
            }
            if ($filesize1 >= 5.0) {
                 //set message image size should be less than 5 mb
            }

            $target_path = $_SERVER['DOCUMENT_ROOT'] . "../images/profile_images/";
            move_uploaded_file($_FILES['file']['tmp_name'], "$target_path" . $_FILES['file']['name']) or
                    die("Could not copy file!");
        }

  1. 在数据库中插入图像名称(带扩展名)。(此处为 $filename)
  2. 从数据库中获取图像名称并存储在变量中(此处为 $profile_image),在 img src 中使用它。

<a href='../images/profile_images/$profile_image'><img alt='Avatar' src='../images/profile_images/$profile_image'></a>

  1. 您只能使用 Anchor 标记在浏览器的另一个选项卡中重定向图像上的用户。

希望这个答案对您有所帮助。

于 2015-03-30T08:39:01.797 回答
0

因为我有一个带有 iso charset 的 mssql 数据库,所以我已将所有结果转换为 utf-8,问题是字节串也转换为 utf-8。

在不转换字节串后,我还返回了字节串并将标头内容类型设置为图像/扩展名

于 2015-04-09T11:08:37.993 回答