V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
toyst
V2EX  ›  PHP

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

  •  1
     
  •   toyst · 2020-08-07 09:45:11 +08:00 · 5434 次点击
    这是一个创建于 1320 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想<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 直接输出图片怎么写?

    14 条回复    2020-09-19 01:36:31 +08:00
    theswow
        1
    theswow  
       2020-08-07 09:52:22 +08:00   ❤️ 2
    header("Content-Type: image/jpeg");
    echo file_get_contents($img_array[$img]);
    qingmumu
        2
    qingmumu  
       2020-08-07 09:53:17 +08:00   ❤️ 1
    输出的是 url 当然没图片,先 file_get_contents 把图片读进来
    garlics
        3
    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
        4
    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
        5
    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
        6
    toyst  
    OP
       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
        7
    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
        8
    edk24  
       2020-08-07 11:02:15 +08:00
    学好 http 走遍天下都不怕, 输出图片流显示不正确? 多半是响应头设置不正确 [苦笑]
    hahaxo
        9
    hahaxo  
       2020-08-07 11:03:42 +08:00
    这么多好心人
    edk24
        10
    edk24  
       2020-08-07 11:03:51 +08:00
    @edk24 看错了 哈哈, 响应头+图片流数据

    图片流数据需要用 fopen file_get_contents 这样的函数来读取. echo 后 exit 就可以了
    flowfire
        11
    flowfire  
       2020-08-07 15:21:35 +08:00
    = =你这个输出方式就离谱。
    html 是文本数据
    图片是二进制数据。
    你应该建两个 php 文件,一个负责输出二进制的图片数据
    另一个 php 中,组建 html 文件时,把你刚刚输出的那个图片文件的 url 传给 html 。
    然后让浏览器自行解析显示。
    Xusually
        12
    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
        13
    Norths  
       2020-08-12 12:12:21 +08:00
    之前正好也研究过,学习了
    ritaswc
        14
    ritaswc  
       2020-09-19 01:36:31 +08:00
    @Xusually 正解。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4897 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:01 · PVG 12:01 · LAX 21:01 · JFK 00:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.