我想通过如下URL返回一个图像http://placehold.it/500x500.我有我的URLhttp://example.inc/assets/image/35345,调用控制器上的操作。控制器从数据库中获取一些数据(名称、id等),还获取图像内容的二进制字符串。
在前端网站上,我有我的img标记,我想在那里调用src属性中的url。
<img src="http://example.inc/assets/image/35345">
更多信息,我使用瘦PHP框架,我的服务器是一个ubuntu13.x系统(流浪者等)。我是一个典型的前端开发人员,没有很好的PHP技能。
以下片段有效:
$file = fopen($name, 'wb');
fwrite($file, $binaryData);
fclose($file);
但是我不想在目录中生成文件。这可能吗?
编辑:设置了内容类型和内容长度标题,这不是问题所在。
获取图像的内容,对其进行base_64编码,然后返回一个base64图像。
$file = file_get_contents($name);
list($width, $height, $type, $attr) = getimagesize($file);
echo '<img src="data:image/'.$type.';'.base64_encode($file).'"/>';
- 您应该使用这样的东西上传目录中的图像。此代码将在目录中上载您的图像
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!");
}
- 在数据库中插入图像名称(带扩展名)。(此处为$filename)
- 从数据库中获取图像名称并存储在变量中(此处为$profile_image),在img src中使用它
<a href='../images/profile_images/$profile_image'><img alt='Avatar' src='../images/profile_images/$profile_image'></a>
- 在浏览器中的另一个选项卡中,您只能使用"锚点"标记重定向图像上的用户
希望这个答案能对你有所帮助。
因为我有一个带有iso字符集的mssql数据库,所以我已经将所有结果转换为utf-8,问题是字节串也转换为utf-8。
在未转换字节串之后,我还返回了字节串,并将头内容类型设置为image/extension