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

创建图片资源

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);

原文来源:https://www.newbii.cn/20200428100000.htm

猜你喜欢

转载自www.cnblogs.com/svip7/p/12795994.html
今日推荐