使用PHP发送特定图像 [英] Using PHP to send a certain image

查看:59
本文介绍了使用PHP发送特定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个PNG图片,但是在访问它时,它将运行一个PHP脚本,PHP脚本应确定要发送的图片(使用某些if语句等).然后,PHP脚本应该从Web服务器上的某个位置读取图像文件并将其输出.

这里是问题,如果我得到一个.png文件,并将PHP代码放入其中,它将无法正常工作,但是,如果我使用.php扩展名,它将正常工作,甚至可以将图像嵌入其他网站,PHP可以决定要发送的图像,但是如果我想直接查看该图像(将其URL复制到我的地址栏中)不起作用,它将为我提供图像的纯内容(乱码). /p>

有人知道该怎么办吗?

这也是我对Stack Overflow的第一个问题-请告诉我是否做错了事.

解决方案

您需要发送Content-Type标头.

对于png:

header('Content-Type: image/png');

对于其他人,将png更改为jpggifbmp或其他任何内容.

请注意,在将任何内容写入输出之前,必须使用header()函数.

I want to have a PNG picture, but when accessing it, it runs a PHP script, the PHP script should decide what picture to send (using some if statements and whatever). Then the PHP script should read the image file from somewhere on my web server and output it.

Here is the issue, if I get a .png file, and put PHP code in it, it won't work, however, if I use the .php extension, it works, and I can even embed the image into other websites, and the PHP can decide what image to send, but if I want to view that image directly (copy it's URL into my address bar) it doesn't work, it gives me the images plain contents (random jibberish).

Anyone know what to do?

Also This is my first question on Stack Overflow - please tell me if I am doing something wrong.

解决方案

You need to send Content-Type headers.

For png:

header('Content-Type: image/png');

For others change png to jpg or gif or bmp or whatever.

Please note that header() function must be used before anything is written to output.

这篇关于使用PHP发送特定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆