PHP 直接输出图片给 img src 引用

2020-08-07 09:45:11 +08:00
 toyst

我想<img src="bg.php">来显示图片

怎么写啊 下面这个随机图片直接可以打开,引用不行

<?php 
$img_array = glob("./img/*/*/*.jpg"); 
$img = array_rand($img_array); 
echo '<img src="'.$img_array[$img].'" />'; 
?>

用这个直接 404

<?php 
$img_array = glob("./img/*/*/*.jpg"); 
$img = array_rand($img_array);
header("Content-type: image/jpg");
echo ''.$img_array[$img].''; 
?>

请教一下 PHP 直接输出图片怎么写?

5497 次点击
所在节点    PHP
14 条回复
theswow
2020-08-07 09:52:22 +08:00
header("Content-Type: image/jpeg");
echo file_get_contents($img_array[$img]);
qingmumu
2020-08-07 09:53:17 +08:00
输出的是 url 当然没图片,先 file_get_contents 把图片读进来
garlics
2020-08-07 10:00:09 +08:00
之前封装了一个这个方法,你把里面的变量替换成你的就可以了。
```

$info = getimagesize($url);
$fun = "imagecreatefrom{$record->ext}";
$imgInfo = $fun($url); //1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename )
$mime = $info['mime'];
header('Content-Type:' . $mime);
if ($width && $height) {
$newImage = imagecreatetruecolor($width, $height);

//生成最后的图片
imagecopyresampled($newImage, $imgInfo, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
$imgInfo = $newImage;
}
$quality = 75;
if ($record->ext == 'png') $quality = -1; //输出质量,JPEG 格式(0-100),PNG 格式(0-9)
$getImgInfo = "image{$record->ext}";
$getImgInfo($imgInfo, null, $quality); //2.将图像输出到浏览器或文件。如: imagepng ( resource $image )
imagedestroy($imgInfo);

```
garlics
2020-08-07 10:01:18 +08:00
@garlics
这一段是按缩放图片的,你可以去掉
if ($width && $height) {
$newImage = imagecreatetruecolor($width, $height);

//生成最后的图片
imagecopyresampled($newImage, $imgInfo, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
$imgInfo = $newImage;
}
RickyC
2020-08-07 10:22:01 +08:00
<?php
$img_array = glob("./img/*/*/*.jpg");

$img = array_rand($img_array);

//文件路径
$path = $img_array[$img];

//读取文件
$bin = file_get_contents($path);

// 输出文件
header("Content-type: image/jpg");
echo $bin;
toyst
2020-08-07 10:23:41 +08:00
@sh7ning 感谢,可用
```
<?php
$img_array = glob("./img/*/*/*.jpg");
$img = array_rand($img_array);
header("Content-Type: image/jpeg");
echo file_get_contents($img_array[$img]);
?>
```
qiayue
2020-08-07 10:57:13 +08:00
二维码、验证码之类的后端生成图片,前端显示的,可以用这种方式,配置好 header,由 PHP 输出图片内容。

但如果你的需求仅仅是随机显示图片,那么就不要用这种方式了,因为图片也要消耗服务器带宽和流量,没办法放到 CDN 去。
你可以换一个思路,两个办法:
1 、如果显示图片也是你能控制的,如 wordpress 模板,那么可以用
<img src="<?=rand_imgs('car')?>">
后端实现一个函数
function rand_imgs($dir){
$full_dir = '/path/to/'.$dir.'/';
//然后获取目录下的所有图片,随机返回一张图片地址即可
}

2 、如果前端不是你可以控制的,就可以用 302 方式
<img src="img.php?type=car">
后端 img.php 文件
<?php
$type = $_GET['type'] ?? '';
$full_dir = '/path/to/'.$dir.'/';
//然后获取目录下所有图片
$imr_src = '随机取得一张图片,拼接图片完整地址';

header("Location:".$img_src);
edk24
2020-08-07 11:02:15 +08:00
学好 http 走遍天下都不怕, 输出图片流显示不正确? 多半是响应头设置不正确 [苦笑]
hahaxo
2020-08-07 11:03:42 +08:00
这么多好心人
edk24
2020-08-07 11:03:51 +08:00
@edk24 看错了 哈哈, 响应头+图片流数据

图片流数据需要用 fopen file_get_contents 这样的函数来读取. echo 后 exit 就可以了
flowfire
2020-08-07 15:21:35 +08:00
= =你这个输出方式就离谱。
html 是文本数据
图片是二进制数据。
你应该建两个 php 文件,一个负责输出二进制的图片数据
另一个 php 中,组建 html 文件时,把你刚刚输出的那个图片文件的 url 传给 html 。
然后让浏览器自行解析显示。
Xusually
2020-08-07 16:27:16 +08:00
你这个简直离谱。
还不如用 Data URI scheme 呢。手动狗头.gif
<img src=“data:image/png;base64,---------------your base 64 image data here---------------”/>
Norths
2020-08-12 12:12:21 +08:00
之前正好也研究过,学习了
ritaswc
2020-09-19 01:36:31 +08:00
@Xusually 正解。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/696354

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX