PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP的图像处理。分享给大家供大家参考,具体如下:

1、添加文字水印

//1、打开图片资源
  $src="./material/sea.jpg";
  $info=getimagesize($src);//获取图片信息
  $type=image_type_to_extension($info[2],false);//转化图片类型
  //var_dump($info);
  $fun="imagecreatefrom{$type}";//拼接成为imagecreatefromjpeg()方法
  $image=$fun($src);//新建GD图片资源
//操作图片
  $font="./material/segoepr.ttf";
  $content="@SuperTory";
  $color=imagecolorallocate($image,255,255,255);
  imagettftext($image,10,0,0,$info[1]-5,$color,$font,$content);//图片上写文字
//输出图片
  header("content-type:".$info['mime']);//$imfo['mine']='image/jpeg'
  $output="image{$type}";//拼接成为imagejpeg()方法
  $output($image);//输出到页面
  $output($image,'./material/watermarked.'.$type);//输出到本地路径
//销毁图片内存资源
  imagedestroy($image);

2、压缩图像

//打开图像
$src="./material/logo.png";
$info=getimagesize($src);
$type=image_type_to_extension($info[2],false);
$create="imagecreatefrom".$type;
$image=$create($src);
//压缩
$tinyImg=imagecreatetruecolor(100,100); //新建压缩后的图像资源
//将原图映射到压缩后的图像资源上
imagecopyresampled($tinyImg,$image,0,0,0,0,100,100,$info[0],$info[1]);
header("content-type:".$info['mime']);
$output="image{$type}";
//$output($image);
$output($tinyImg);
//销毁
imagedestroy($image);
imagedestroy($tinyImg);

3、添加水印图片

//获取原图片
$src="./material/sea.jpg";
$info=getimagesize($src);
$type=image_type_to_extension($info[2],false);
$create="imagecreatefrom".$type;
$image=$create($src);
//获取水印图片资源
$markSrc="./material/logo.png";
$markInfo=getimagesize($markSrc);
$markType=image_type_to_extension($markInfo[2],false);
$create="imagecreatefrom".$markType;
$markImage=$create($markSrc);
$tinyImg=imagecreatetruecolor(100,100);
imagecopyresampled($tinyImg,$markImage,0,0,0,0,
  100,100,$markInfo[0],$markInfo[1]);
imagecopymerge($image,$tinyImg,$info[0]-100,$info[1]-100,
  0,0,100,100,100);
//合并图片:(原图,水印图,原图x位置,原图y位置,水印x起点,水印y起点,水印x终点,水印y终点,不透明度)
header("content-type:".$info['mime']);
$output="image{$type}";
$output($image);
imagedestroy($image);
imagedestroy($markImage);

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php面向对象重点知识分享

1、$this是什么 当前类实例化的对象 2、访问对象中的成员 对象->成员 3、构造方法 通常用来初始化对象的属性,不用把属性写死,不同的对象就有了不同的属性 4、get、set...

PHP关联数组实现根据元素值删除元素的方法

本文实例讲述了PHP关联数组实现根据元素值删除元素的方法。分享给大家供大家参考。具体如下: <?php $array1 = array("a" => "gree...

PHP中的integer类型使用分析

integer 可以已10进制,8进制,16进制表示。 用八进制表示的时候,数字需要已0(零)开头; 用十六进制表示的时候,数字需要已0x(零x)或者0X(零大写X)开头; intege...

PHP获取文件相对路径的方法

本文实例讲述了PHP获取文件相对路径的方法。分享给大家供大家参考。具体实现方法如下: <?php $a = '/a/b/c/d/e.php'; $b = '/a/b/1...

drupal 代码实现URL重写

以下是实现例子: 复制代码 代码如下: /* * 伪地址转原地址 (url_alter) */ function example_url_inbound_alter(&$path, $o...