显示标签为“图片”的博文。显示所有博文
显示标签为“图片”的博文。显示所有博文

2012年12月29日星期六

防止图片盗链

    所谓盗链是指服务提供商自己不提供服务的内容,而是通过技术手段直接在自己的网站上向最终用户提供其他服务商的服务内容,以骗取用户的浏览和点击率。受益者不提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。像图片、视频等这些比较大的文件都有可能成为被盗的对象。一旦文件被盗,就会增加服务器的流量,影响网站的访问速度。虽然我们不能100%的保护这些文件不被别人盗用,但可以通过 HTTP Referer 进行限制。使用 Apache 可以很方便地进行设置。假设主机域名为 www.example.com,在文件 .htaccess 中的配置如下:
 
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} ! http://www.example.com [NC]
RewriteCond %{HTTP_REFERER} ! http://www.google.cn [NC]
RewriteCond %{HTTP_REFERER} ! http://www.baidu.com [NC]
RewriteRule .*\.(gif|jpg)$ http://www.example.com/default.jpg [R,NC,L]

    该配置说明说下:

  •     RewriteCond %{HTTP_REFERER} !^$ [NC]
    允许 HTTP_REFERER 为空时访问,即允许用户在浏览器地址栏中直接输入图片地址时显示图片。一般来说这个选项是可选的,建议这样设置。如果将 HTTP_REFERER 强制限制不能为空,那么当用户通过代理服务器访问时,可能会导致一些问题。

  •     RewriteCond %{HTTP_REFERER} ! http://www.example.com [NC]
    设置允许访问的 HTTP_REFERER 来源,包括自身站点,以及 Baidu、Google、Yahoo!等

  • RewriteRule .*\.(gif|jpg)$ http://www.example.com/default.jpg [R,NC,L]
    设置图片被盗链时用于替换的图片,如果有图片被盗链,显示的是 default.jpg。注意,默认图片所在的路径最好跟配置文件不在同一目录下,图片越小越好。如果不想设置替换图片,可以使用下面的语句:

RewriteRule .*\.(gif|jpg)$ - [F]

NC、R、L、F 标记解释

NC:不区分大小写
R: 强制重定向
L:立即停止重写操作
F:强制禁止URL

2012年11月1日星期四

PHP 5 文字图片混合水印与缩略图

一、水印制作

   1.水印文字

    PHP 中为图片打上水印文字主要是通过 GD 库提供的 imagettftext() 函数来实现的。

    其过程为:载入图片 =》 调好水印文字的颜色 =》 打上水印

<?php
$img 'Desert.jpg';//图像的路径。这里以 Windows 7 自带的一幅沙漠的图片为例
$img_info getimagesize($img);

//载入图像到PHP,转成 PHP 可识别的编码
switch($img_info[2]{
  case 1:
    $res imagecreatefromgif($img);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res imagecreatefromjpeg($img);
    break;
  case 3:
    $res imagecreatefrompng($img);
    break;    
}

// 为一幅图像分配颜色(相当于 PhotoShop 的调色板)
// imagecolorallocate ( resource image, int red, int green, int blue )  返回一个标识符,代表了由给定的 RGB 成分组成的颜色。
$te imagecolorallocate($res,225,225,225);

//rand(0,10)倾斜度。msyh.ttf 是微软雅黑字体,可在 C:\Windows\Fonts 找到。然后拷贝到该文件的目录下
imagettftext($res,12,rand(0,10),20,80,$te,'msyh.ttf',"我的博客 www.woqilin.net");

switch($img_info[2]{
  case 1:
    header("Content-type: image/gif");
    imagegif($res);//以 GIF 格式将图像输出到浏览器
    break;
  case 2:
    header("Content-type: image/jpeg");
    imagejpeg($res);
    break;
  case 3:
    header("Content-type: image/png");
    imagepng($res);
    break;    
}
?>

   2.水印图片

    PHP 中为图片打上图片水印是通过 imagecopy() 函数来实现的。

<?php
$img 'Desert.jpg';//图像的路径。这里以 Windows 7 下一幅沙漠的图片为例,像素为 1024 X 768
$img_info getimagesize($img);

//载入图像到PHP
switch($img_info[2]{
  case 1:
    $res imagecreatefromgif($img);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res imagecreatefromjpeg($img);
    break;
  case 3:
    $res imagecreatefrompng($img);
    break;    
}

$logo 'logo-mongodb.png';//像素为 210 X 90 ,该 logo 可从 http://www.mongodb.org/ 下载
$logo_info getimagesize($logo);
switch($logo_info[2]{
  case 1:
    $res_logo imagecreatefromgif($logo);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res_logo imagecreatefromjpeg($logo);
    break;
  case 3:
    $res_logo imagecreatefrompng($logo);
    break;    
}

//bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。 
imagecopy($res,$res_logo,100,100,0,0,210,90);

switch($img_info[2]{
  case 1:
    $res imagegif($res,'new.gif');//以 GIF 格式将图像输出文件
    break;
  case 2:
    $res imagejpeg($res,'new.jpg');
    break;
  case 3:
    $res imagepng($res,'new.png');
    break;    
}
?>

二、缩略图

     在 PHP 中要生成图片的缩略图,主要是靠函数 imagecopyresized() 来实现。


<?php
$img 'Desert.jpg';//图像的路径。这里以 Windows 7 下一幅沙漠的图片为例,像素为 1024 X 768
$img_info getimagesize($img);

//载入图像到PHP
switch($img_info[2]{
  case 1:
    $res imagecreatefromgif($img);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res imagecreatefromjpeg($img);
    break;
  case 3:
    $res imagecreatefrompng($img);
    break;    
}

//新建一个真彩色图像
$new imagecreatetruecolor(400,400);
//bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
//将一幅图像中的一块正方形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_image 和 src_image 相同的话)区域,但如果区域交迭的话则结果不可预知。
imagecopyresized($new,$res,0,0,0,0,400,400,$img_info[0],$img_info[1]);

header("Content-type: image/png");
imagepng($new);// 以 PNG 格式将图像输出到浏览器
?>

2012年10月31日星期三

PHP 5 中图片验证码的制作

一、应用场景

    在 WEB 网站中,图片验证码经常被用来防止恶意地用户注册、发帖等场景。在 PHP 中,图片验证码主要是通过 GD 库提供的 API 来完成的。

二、实现的方法

    验证码一般都是随机的数字和字母组合的,可以通过随机函数,十六进制函数 dechex 简单实现。最关键的问题还是怎样生成图片。

<?php
//生成随机数-》创建图片-》随机数写进图片 -》输出到浏览器

for($i=0;$i<4;$i++{
  $rand .= dechex(rand(1,15));
}
  
$im imagecreatetruecolor(100,30);// 新建一个真彩色图像  x就是宽 ,y就是高

//设置颜色

// 为一幅图像分配颜色(相当于 PhotoShop 的调色板)

// imagecolorallocate ( resource image, int red, int green, int blue )  返回一个标识符,代表了由给定的 RGB 成分组成的颜色。

$bg imagecolorallocate($im,0,0,0);//第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色。代表了由给定的 RGB 成分组成的颜色
  
$te imagecolorallocate($im,225,225,225);


//把字符串写在图像左上角

//绘图函数  imagestring ( resource image, font, int x, int y, string s, int col ) 

//用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。  

imagestring($im,5,rand(3,70),rand(0,16),$rand,$te);

//直接输出图像到浏览器
header("Content-type: image/jpeg");

// imagejpeg ( resource image [, string filename [, int quality]] )  filename 参数为可选,如果省略,则原始图像流将被直接输出。要省略 filename 参数而提供 quality 参数,使用空字符串('')。通过 header() 发送 Content-type: image/jpeg 可以使 PHP 脚本直接输出 JPEG 图像。

imagejpeg($im);
?>

三、制作复杂的图片验证码

     有时验证码为了防止被一些图片识别工具识别,必须设置一些干扰。例如在图片上随机加上一些线条或者点。这主要通过 imageline() 和 imagesetpixel() 函数来实现。甚至制作中文验证码。

<?php

for($i=0;$i<4;$i++{
  $rand .= dechex(rand(1,15));
}
  
$im imagecreatetruecolor(100,30);

$bg imagecolorallocate($im,0,0,0);
  
$te imagecolorallocate($im,225,225,225);

//画线条
for($i=0$i<3$i++){
  $te2 imagecolorallocate($im,rand(0,225),rand(0,225),rand(0,225));
//imageline ( resource image, int x1, int y1, int x2, int y2, int color )  用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。 
  imageline($im,rand(0,100),0,rand(0,100),rand(0,30),$te2);
}
//画点
for($i 0$i 100$i++){
  //imagesetpixel ( resource image, int x, int y, int color ) 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。 
  imagesetpixel($im,rand()%100,rand()%30,$te2);
}
  
$str iconv("gbk","UTF-8","验证码");//把gbk编码转换成UTF-8,如果文件的编码是 UTF-8,则不需要这一步。因为我们的系统(Windows)自带的字体默认是用国际通用编码(UTF-8)模式来识别的,所以如果不做编码转换,可能无法正常显示。
imagettftext($im,12,rand(0,10),20,20,$te,'msyh.ttf',$str);//rand(3,10)倾斜度。msyh.ttf 是微软雅黑字体,可在 C:\Windows\Fonts (Windows XP、Windows 7)找到。然后拷贝到该文件的目录。Windows 下之所以能够显示中文,是因为系统安装了中文字体

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

imagejpeg($im);
?>