使用PHP脚本提供图片服务与直接加载图片 [英] Serve image with PHP script vs direct loading an image

查看:174
本文介绍了使用PHP脚本提供图片服务与直接加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视一些外部图像的加载频率. 所以我的想法不是直接像这样给uri:

I want to monitor how often some external images are loaded. So my idea is instead of giving a uri directly like this:

www.site.com/image1.jpg

我可以创建一个读取图像的PHP脚本,因此我构建了一个PHP文件,我的HTML如下所示:

I can create a PHP script which reads the image, so I built a PHP file and my HTML would look like this:

<img src="www.site.com/serveImage.php?img=image1.jpg">

但是我不知道如何从磁盘读取图像并返回它.我会返回字节数组还是设置内容类型?

but I don't know how to read the image from disk and return it. Would I return a byte array or set the content type?

亲切的问候, 米歇尔

推荐答案

您必须设置内容类型:

header("Content-type: image/jpeg");

然后您加载图像并像这样输出:

Then you load the image and output it like this:

$image=imagecreatefromjpeg($_GET['img']);
imagejpeg($image);

这篇关于使用PHP脚本提供图片服务与直接加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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