一创建图片资源

imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);画出各种图像 imagegif(图片资源,保存路径);
imagepng()
imagejpeg();

二获取图片属性

imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = gif,2 = jpg,3 = png,4 = swf,5 = psd,6 = bmp,7 = tiff(intel byte order),8 = tiff(motorola byte order),9 = jpc,10 = jp2,11 = jpx,12 = jb2,13 = swc,14 = iff,15 = wbmp,16 = xbm。这些标记与 php 4.3.0 新加的 imagetype 常量对应。索引 3 是文本字符串,内容为“height=”yyy” width=”xxx””,可直接用于 img 标记。
销毁图像资源
imagedestroy(图片资源);

三透明处理

png、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();

四图片的裁剪

imagecopyresized()
imagecopyresampled();

五加水印(文字、图片)

字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )

六图片旋转

imagerotate();//制定角度的图片翻转

七图片的翻转

沿x轴 沿y轴翻转

八锐化

imagecolorsforindex()
imagecolorat()
在图片上画图形 $img=imagecreatefromgif(“./images/map.gif”);

以下php的图像处理实例分享给大家供大家参考:

1、添加文字水印

//1、打开图片资源
  $src="./images/logo.png";
  $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="./images/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="./images/logo.png";
$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);

原文来源: