php图像处理函数大全

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

  1. <?PHP
  2.  /**
  3.  * 图片锐化处理
  4.  */
  5.  $red= imagecolorallocate($img, 255, 0, 0);


  6.  imageline($img, 0, 0, 100, 100, $red);
  7.  imageellipse($img, 200, 100, 100, 100, $red);
  8.  imagegif($img, "./images/map2.gif");
  9.  imagedestroy($img);
 图片普通缩放
 
  1. <?php
  2.  $filename="./images/hee.jpg";
  3.  $per=0.3;
  4.  list($width, $height)=getimagesize($filename);
  5.  $n_w=$width*$per;
  6.  $n_h=$width*$per;
  7.  $new=imagecreatetruecolor($n_w, $n_h);
  8.  $img=imagecreatefromjpeg($filename);
  9.  //拷贝部分图像并调整
  10.  imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
  11.  //图像输出新图片、另存为
  12.  imagejpeg($new, "./images/hee2.jpg");
  13.  imagedestroy($new);
  14.  imagedestroy($img);
 图片等比例缩放、没处理透明色
 
  1. <?php
  2.  function thumn($background, $width, $height, $newfile) {
  3.  list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度
  4.  if ($width && ($s_w < $s_h)) {
  5.  $width = ($height / $s_h) * $s_w;
  6.  } else {
  7.  $height = ($width / $s_w) * $s_h;
  8.  }
  9.  $new=imagecreatetruecolor($width, $height);
  10.  $img=imagecreatefromjpeg($background);
  11.  imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
  12.  imagejpeg($new, $newfile);
  13.  imagedestroy($new);
  14.  imagedestroy($img);
  15.  }
  16.  thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");
 gif透明色处理
 
  1. <?php
  2.  function thumn($background, $width, $height, $newfile) {
  3.  list($s_w, $s_h)=getimagesize($background);
  4.  if ($width && ($s_w < $s_h)) {
  5.  $width = ($height / $s_h) * $s_w;
  6.  } else {
  7.  $height = ($width / $s_w) * $s_h;
  8.  }
  9.  $new=imagecreatetruecolor($width, $height);
  10.  $img=imagecreatefromgif($background);
  11.  $otsc=imagecolortransparent($img);
  12.  if($otsc >=&& $otst < imagecolorstotal($img)){//判断索引色
  13.  $tran=imagecolorsforindex($img, $otsc);//索引颜色值
  14.  $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
  15.  imagefill($new, 0, 0, $newt);
  16.  imagecolortransparent($new, $newt);
  17.  }
  18.  imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
  19.  imagegif($new, $newfile);
  20.  imagedestroy($new);
  21.  imagedestroy($img);
  22.  }
  23.  thumn("images/map.gif", 200, 200, "./images/map3.gif");
图片裁剪

  1. <?php
  2. /**
  3. * 图片裁剪处理
  4. * edit by www.jbxue.com
  5. */
  6. function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
  7. $back=imagecreatefromjpeg($background);
  8. $new=imagecreatetruecolor($cut_width, $cut_height);
  9. imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
  10. imagejpeg($new, $location);
  11. imagedestroy($new);
  12. imagedestroy($back);
  13. }
  14. cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");
  15. ?>
图片加水印 文字水印

  1. <?PHP
  2.  /**
  3.  *
  4.  * 图片添加文字水印
  5.  */
  6.  function mark_text($background, $text, $x, $y){
  7.  $back=imagecreatefromjpeg($background);
  8.  $color=imagecolorallocate($back, 0, 255, 0);
  9.  imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);
  10.  imagejpeg($back, "./images/hee7.jpg");
  11.  imagedestroy($back);
  12.  }
  13.  mark_text("./images/hee.jpg", "细说PHP", 150, 250);
  14.  //图片水印
  15.  function mark_pic($background, $waterpic, $x, $y){
  16.  $back=imagecreatefromjpeg($background);
  17.  $water=imagecreatefromgif($waterpic);
  18.  $w_w=imagesx($water);
  19.  $w_h=imagesy($water);
  20.  imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
  21.  imagejpeg($back,"./images/hee8.jpg");
  22.  imagedestroy($back);
  23.  imagedestroy($water);
  24.  }
  25.  mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);
图片旋转

  1. <?PHP
  2.  /**
  3.  * 图片旋转
  4.  */
  5.  $back=imagecreatefromjpeg("./images/hee.jpg");


  6.  $new=imagerotate($back, 45, 0);
  7.  imagejpeg($new, "./images/hee9.jpg");
  8.  ?>

图片水平翻转垂直翻转

点击(此处)折叠或打开

  1. <?php
  2.  /**
  3.  * 图片水平翻转 垂直翻转
  4.  */
  5.  function turn_y($background, $newfile){
  6.  $back=imagecreatefromjpeg($background);
  7.  $width=imagesx($back);
  8.  $height=imagesy($back);
  9.  $new=imagecreatetruecolor($width, $height);
  10.  for($x=0; $x < $width; $x++){
  11.  imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
  12.  }
  13.  imagejpeg($new, $newfile);
  14.  imagedestroy($back);
  15.  imagedestroy($new);
  16.  }
  17.  function turn_x($background, $newfile){
  18.  $back=imagecreatefromjpeg($background);
  19.  $width=imagesx($back);
  20.  $height=imagesy($back);
  21.  $new=imagecreatetruecolor($width, $height);
  22.  for($y=0; $y < $height; $y++){
  23.  imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
  24.  }
  25.  imagejpeg($new, $newfile);
  26.  imagedestroy($back);
  27.  imagedestroy($new);
  28.  }
  29.  turn_y("./images/hee.jpg", "./images/hee11.jpg");
  30.  turn_x("./images/hee.jpg", "./images/hee12.jpg");
  31.  ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值