如何编写php文件的脚本,以显示类似< img src ="/img.php?imageID = 32"的图像. /&gt ;? [英] How do I script a php file to display an image like <img src="/img.php?imageID=32" />?

查看:70
本文介绍了如何编写php文件的脚本,以显示类似< img src ="/img.php?imageID = 32"的图像. /&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将横幅广告存储在mysql表中,或者我应该仅说出文件名,例如:"imagename.jpg"或"banner3.gif".

I'm storing banner ads in a mysql table or I should say just the file name e.g: 'imagename.jpg' or 'banner3.gif'.

很明显,我想跟踪这些横幅广告的加载时间,因此我正在考虑创建一个名为img.php的php文件,并像这样调用它

Obviously I am wanting to track when these banners are loaded so I'm thinking to create a php file called img.php and call it like so

<img src="/img/img.php?imageID=3" />

这会提取ID为3的图像,并更新我的匹配表等.

And that would pull out the image with the id 3 along with updating my hits table etc.

我知道如何更新命中表...但是我要尝试的是如何对img.php文件进行编码,以便它简单地检索文件名并将其打印到屏幕上,从而像常规图片.

I know how to update my hits table... but what I'm trying to work out is how to code the img.php file so that it simply retrieves the file name and prints it to screen so it works like a regular image.

到目前为止,我已经...

So far I've got...

<?php
header("Content-Type: image/jpeg");
// insert my db connect file
// update the hits table etc

$sql = 'SELECT * FROM ads WHERE id="' . $_GET['imageID'] .  '" ';
$res = mysql_query($sql);

$row = mysql_fetch_array($res);

$image = '/img/' . $row['file'];

imagejpeg($image);

// and this is where I'm at...

?>

上面的代码存在一些问题,这些代码是我从各个地方提取来的.这些图片可能是png/gif/jpg,而标题的内容类型似乎只能容纳一种类型的空间.

I have some problems with the above code which i've extracted from various places to get here. The images could be png/gif/jpg while the header content-type seems to only have space for one type.

有没有一种方法可以使该文件对多种图像类型正常工作?我在想我应该先查询表,计算出文件扩展名,然后基于该表名插入标头函数.

Is there a way to have this one file work ok for multiple image types? I'm thinking I should query the table firstly, work out the file extension and then insert the header function based off that.

但是,当我做对了,但我想让图像刚刚出现时,我该怎么办呢?

But what do I actually do when I've got that right and I want the image to then just appear?

感谢您的帮助,这里是最终的工作文件

thanks to your help here is the final working file

<?php

// make sure we're only getting a number for the query b4 doing stuff
if(is_numeric($_GET['imid'])) {

include('thedbfile.php');

$types['jpg'] = 'image/jpeg';
$types['png'] = 'image/png';
$types['gif'] = 'image/gif';



$sql = 'SELECT file FROM ads WHERE id="' . $_GET['imid'] . '" ';
$res = mysql_query($sql);

$row = mysql_fetch_array($res);

$image = 'img/banners/' . $row['file'];

$extension = end(explode('.', $row['file']));


header('Content-Type: ' . $types[$extension]);

echo file_get_contents($image);


}

?>

推荐答案

您可能希望将图像内容直接存储在数据库中.

You may want to store the image content directly in your Database.

正如您所说,您需要从文件名中解析内容类型.您还可以在数据库中添加一个包含扩展名的字段.

As you said, you need to parse the content type out of the filename. You could also add a field to your database which contains the extension.

$extension = end(explode('.', $row['file']));

然后,您必须创建一个包含标题内容类型的数组:

Then you have to create an array which contains the header content-type:

$types['jpg'] = 'image/jpeg';
$types['png'] = 'image/png';

然后发送标头(您将要检查 types 数组是否包含 extension 键,如果没有则包含错误)

then send the header (you will want to check whether the types array contains the extension key and error if it doesn't)

header('Content-Type: ' . $types[$extension]);

然后使用

echo file_get_contents($image);

应该这样做.请注意,出于安全考虑,您实际上必须检查ImageID参数是否为整数:

That should do it. Note that you really have to check whether the ImageID parameter is an integer, for safety reasons:

if(!ctype_digit($_GET['ImageID'])) // error

这篇关于如何编写php文件的脚本,以显示类似&lt; img src ="/img.php?imageID = 32"的图像. /&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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