原理就是将图片地址保存进数据库,然后用php调用数据库,利用图片地址生成图片在浏览器显示。显示的图片地址是php的地址。这样就隐藏了其真实地址!
[Copy to clipboard]
PHP CODE:
<?php
#img.php
require_once('global.php');
$id=$_GET[id];
$sql=$db->get_one("select imgurl from pics where id='{$id}'");//这个是从数据库里调图片地址的,前面的程序我就不写了
$image_path=$sql[0];
define(MAX_WIDTH, 150);
define(MAX_HEIGHT, 150);//定义图片大小,有缩略图的功能
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}
if ($img) {
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
header("Content-type: image/jpeg");
imagejpeg($img);//显示
?>
运用方法:test.html
[Copy to clipboard]
PHP CODE:
<img src="img.php?id=123">