在php文件中返回图像的内容?

Ver*_*cas 7 php image

我真的不知道任何PHP,但我喜欢做一件简单的事情:
我从a内部访问一个php页面<img src="/myhumbleimage.php" />,我想从另一个URL返回一个图像.


我提出了:

<?php
header('Content-Type: image/png');
readfile('i' . rand(1,3) . '.png');
exit;
Run Code Online (Sandbox Code Playgroud)

它有效:
阿凡达选择http://vercas.webuda.com/img.php?.png
(重新加载页面几次!)

Phi*_*hil 16

检查readfile().

基本思想是发送适当的MIME类型标题(使用header())然后使用传递文件内容readfile().

例如

<?php
// myhumbleimage.php

// Do whatever myhumbleimage.php does before the image is delivered

header('Content-Type: image/jpeg');
readfile('path/or/url/of/image/file.jpg');
exit;
Run Code Online (Sandbox Code Playgroud)

  • "我真的不知道任何PHP".你能告诉我一个有效的例子,比如说我是完全愚蠢的吗? (2认同)