我正在尝试通过PHP加载图像,但我不知道如何。
文件名存储在数据库中,例如image.jpg
if($_GET['image']){
// Client requesting image, so retrieve it from DB
$id = mysql_real_escape_string($_GET['image']);
$sql = "SELECT * FROM $tbl_name WHERE id = '$id' LIMIT 1";
}
客户需要请求像这样的图像
http://example.com/index.php?image=1
然后它应该返回图像,因此它可以像这样嵌入:
<img src="http://example.com/index.php?image=1" alt="" />
这可能吗?
答案 0 :(得分:19)
$img = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($img);
刚试过它
答案 1 :(得分:2)
您可以使用GD库。首先,使用http://php.net/imagecreatefromjpeg
之类的函数创建资源。您需要提供路径作为参数。
之后,您只需使用http://php.net/imagejpeg
等函数输出资源。
请勿忘记发送内容类型标头,并在资源上使用imagedestroy
。
更新
考虑这个样本:
$im = imagecreatefromjpeg('path/to/image.jpg');
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
答案 2 :(得分:0)
我建议你先创建一个名为image.php的文件。所以你会调用image.php?id = 1
将image.php标头作为图像类型。标题('Content-Type:image / jpeg');
然后,您可以使用PHP中的GDImage库加载图像并输出。或者您可以读取文件并输出。标题()是关键。
答案 3 :(得分:0)
这是我的解决方案:
$mime = 'image/jpg';
$out_image = 'default.jpg'; //default no photo image
$user_img = 'image.jpg'; //it can be gif, png, jpg
//Check if file exist in directory
if(file_exist('/path/to/'.$user_img)) {
$s = imagesize($user_img);
$mime = $s['mime'];
$out_image = $img_data;
}
header('content-type: '.$mime);
readfile($img_data);