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

yipeiwu_com7年前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实现数据库的增删改查

php实现数据库的增删改查

1.查询: 数据的显示,这里就可以嵌入php来进行数据的输出 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona...

php专用数组排序类ArraySortUtil用法实例

本文实例讲述了php专用数组排序类ArraySortUtil用法。分享给大家供大家参考。具体如下: ** * ArraySortUtil is a array sort utili...

PHP观察者模式示例【Laravel框架中有用到】

本文实例讲述了PHP观察者模式。分享给大家供大家参考,具体如下: <?php //观察者模式 //抽象主题类 interface Subject { public...

php 安全过滤函数代码

复制代码 代码如下: //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_repl...

php取整函数ceil,floo,round的用法及介绍

ceil  是向上进位得到一个值的函数;floor 是舍掉小数位得到一个值的函数;round 是用来四舍五入的函数 ceil定义和用法:ceil() 函数向上舍入为最接近的整数...