PHP操作图像

jopen 9年前

创建、合并、添加水印等

PHP确实是一个简单的语言,PHP能够那么流行也是因为该语言能够从简单出发,用最简单的语句就实现功能,摒弃了过多的繁琐配置,寥寥几句就可以实现JAVA需要写一堆才能实现的功能;这个既是有点也是缺点,不过无论如何,PHP引领了一种潮流,就是简单,simple is all;

php操作图像最简单的,就是给图像加水印和加文字;

1,加水印:

<?php   $one_path = 'be.jpg';  $two_path = 's.png';  //创建图片的实例  $one = imagecreatefromstring(file_get_contents($one_path));  $two = imagecreatefromstring(file_get_contents($two_path));  //获取水印图片的宽高  list($two_w, $two_h) = getimagesize($two_path);  // 将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果  // imagecopymerge($one, $two, 800, 300, 0, 0, $two_w, $two_h, 100);  // 如果水印图片本身带透明色,则使用imagecopy方法  imagecopy($one, $two, 800, 300, 0, 0, $two_w, $two_h);  //输出图片  list($one_w, $one_h, $one_type) = getimagesize($one_path);  switch ($one_type) {      case 1://GIF          header('Content-Type: image/gif');          imagegif($one);          break;      case 2://JPG          header('Content-Type: image/jpeg');          imagejpeg($one);          //$filename='mess.jpg';          //imagejpeg($one,$filename);          break;      case 3://PNG          header('Content-Type: image/png');          imagepng($one);          break;      default:          break;  }  imagedestroy($one);  imagedestroy($two);


imagejpeg($img,$filename,qulity):这个关键方法有三个参数,第一个是图像对象,第二个是图像保存路径,第三个是图像质量;

如果直接显示,只填第一个参数;如果保存成文件,填上第二个参数,比如D:/img/i.jpg,就会保存下来;

2,加文字:

<?php  $dst_path = 'be.jpg';    //创建图片的实例  $dst = imagecreatefromstring(file_get_contents($dst_path));    //打上文字  $font = './tt.ttf';//字体  $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色  imagefttext($dst, 100, 0, 100, 500, $black, $font, '快乐编程');    //输出图片  list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);  switch ($dst_type) {      case 1://GIF          header('Content-Type: image/gif');          imagegif($dst);          break;      case 2://JPG          header('Content-Type: image/jpeg');          imagejpeg($dst);          break;      case 3://PNG          header('Content-Type: image/png');          imagepng($dst);          break;      default:          break;  }    imagedestroy($dst);


imagefttext ( resource $image , float $size , float $angle , int $x , int $y ,int $col , string $font_file , string $text [, array $extrainfo ] ):这个函数是关键

3,生成注册码:

<?php    //echo PHP_VERSION; 输出php版本    /**imagecreatetruecolor -- 新建一个真彩色图像 imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。     * imagecolorallocate -- 为一幅图像分配颜色 imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。    * imagefilledrectangle -- 画一矩形并填充 imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。    * imagepng -- 以 PNG 格式将图像输出到浏览器或文件 imagepng ( resource image [, string filename] )    * imagedestroy -- 销毁一图像  imagedestroy ( resource image )释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符.    * header 1.更改网页头部信息2.跳转3.改成字符串    * imagerectangle -- 画一个矩形  imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col ) imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。    * imagesetpixel -- 画一个单一像素  imagesetpixel ( resource image, int x, int y, int color ) imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。     * imagearc -- 画椭圆弧 imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color ) imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。    * imagefttext -- 使用 FreeType 2 字体将文本写入图像 imagefttext ( resource image, float size尺寸, float angle角度, int x, int y, int col颜色, string font_file字体文件, string text [, array extrainfo]字符 )    * strlen -- Get string length  strlen ( string string ) 获取字符串长度    *    *    **/    //imagecreatetruecolor -- 新建一个真彩色图像    $im = imagecreatetruecolor(300,200);    //定义背景色imagecolorallocate(图像资源,红,绿,蓝)0——255 。255.255.255    $back_color =imagecolorallocate($im,100,150,200);    //imagefilledrectangle -- 画一矩形并填充    imagefilledrectangle($im,0,0,300,200,$back_color);    //$back_colors = imagecolorallocate($im,255,255,255);    //imagerectangle -- 画一个矩形 作为边框    //imagerectangle($im, 2,2,298,198,$back_colors);    for ($i=0;$i<800;$i++){    $point_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));    //imagesetpixel -- 画一个单一像素 画点    $point = imagesetpixel($im,mt_rand(0,300),mt_rand(0,200),$point_color);    }    //imagearc-- 画椭圆弧 画线    for($i=0;$i<300;$i++){    $line_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));    imagearc($im, mt_rand(0,500),mt_rand(0,500), mt_rand(0,500),mt_rand(0,500) ,mt_rand(0,360), mt_rand(0,360), $line_color);    }    //imagefttext -- 使用 FreeType 2 字体将文本写入图像    $str ="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";    $code = '';    for($i=1;$i<5;$i++){    $code.=$str{mt_rand(0, strlen($str)-1)};    }    $text_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));    //$str ='eng';    imagefttext($im,50, mt_rand(0,45),120,120, $text_color,'c:\\WINDOWS\\Fonts\\simsun.ttc',$code);    //输出图像必须更改头部信息    header('Cache-Control:max-age=1,s-maxage=1,no-cache,must-revalidate');    header('Content-type:image/png;charset=utf8');    //imagepng -- 以 PNG 格式将图像输出到浏览器或文件    imagepng($im);    //imagedestroy -- 销毁一图像    imagedestroy($im);