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里面的抽象类

当然,可能存在多个根类,用来实现不同的功能. 在一个良好设计的体系中,每个根类都应该有一个有用的接口, 可以被应用代码所使用. 如果我们的应用代码被设计成与根类一起工作,那么它也可以和任...

php使用file函数、fseek函数读取大文件效率对比分析

php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下...

php根据日期或时间戳获取星座信息和生肖等信息

php根据日期或时间戳获取星座信息和生肖等信息

分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birt...

PHP调用Webservice实例代码

它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com...

PHP生成图片缩略图类示例

本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下: class App_image_helper { protected $imgFileName; pr...