zoukankan      html  css  js  c++  java
  • 【php学习】图片处理三步走

      前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来。所以就抽空整理了一下图片操作函数。

    1. 创建画布

      从文件中创建一个新图像

    • imagecreatefromgif($filename)
    • imagecreatefromjpeg($filename)
    • imagecreatefrompng($filename)

      上面几个函数区别在于图片格式,知道了图片的格式就能选对函数。

    1     $type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
    2     //获取图片信息
    3     list($width, $height, $type) = getimagesize($filename);
    4 
    5     //创建一个画布
    6     $createFun = 'imagecreatefrom' . $type_arr[$type];
    7     $im = $createFun($f1);

    2. 图片处理

      图片处理的函数就是参数多,具体说明还是看文档的比较好!

    • imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
    • imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
    • imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

    3. 保存图片并销毁画布

    1     //保存图片
    2     $saveFun = 'image' . $type_arr[$type];
    3     $saveFun($dst, $f2);
    4     
    5     //销毁图片
    6     imagedestroy($im);    imagedestroy($dst);

      

    第一步和第三步几乎是固定的,拿来用就行了。

    下面是自己写的图片处理函数

      1 /**
      2      * 生成缩略图
      3      * @param $f1   源图片
      4      * @param $w    缩略图宽度
      5      * @param $h    缩略图高度
      6      * @param string $f2 缩略图
      7      */
      8     function imgThumb($f1, $w, $h, $f2=''){
      9         $type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
     10         //获取图片信息
     11         list($width, $height, $type) = getimagesize($f1);
     12     
     13         //1. 创建画布
     14         $createFun = 'imagecreatefrom' . $type_arr[$type];
     15         $src = $createFun($f1);
     16         $dst = imagecreatetruecolor($w, $h);    //创建空白画布
     17             
     18         //2. 复制图片
     19         imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
     20             
     21         //3. 保存图片并销毁画布
     22         if(empty($f2)) $f2 = $f1;
     23         $saveFun = 'image' . $type_arr[$type];
     24         $saveFun($dst, $f2);
     25     
     26         imagedestroy($src);
     27         imagedestroy($dst);
     28     }
     29     /**
     30      * 给图片添加水印
     31      * @param $f1   源图片
     32      * @param $f2   水印图片
     33      * @param int $coord 坐标,用数字表示,1左上角2右上角3左下角4右下角5上下居中6左右居中7全居中
     34      * @param string $f3 目标图片
     35      */
     36     function imgWater($f1, $f2, $coord=1, $f3=''){
     37         $type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
     38         //获取图片信息
     39         list($w1, $h1, $t1) = getimagesize($f1);
     40         list($w2, $h2, $t2) = getimagesize($f2);
     41     
     42         //1. 创建画布
     43         $createFun = 'imagecreatefrom' . $type_arr[$t1];
     44         $im = $createFun($f1);
     45         $createFun = 'imagecreatefrom' . $type_arr[$t2];
     46         $waterIm = $createFun($f2);
     47         
     48         //2. 图片复制到另一张图片上
     49         $px = 0; $py=0;
     50         switch($coord){
     51             case 1 :break;
     52             case 2 : 
     53                 $px = $w1-$w2;
     54             break;
     55             case 3 :
     56                 $py = $h1-$h2;
     57             break;
     58             case 4:
     59                 $px = $w1-$w2;    $py=$h1-$h2;
     60             break;
     61             case 5:
     62                 $py=($h1-$h2)/2;
     63             break;
     64             case 6:
     65                 $px = ($w1-$w2)/2;
     66             break;
     67             case 7:
     68                 $px = ($w1-$w2)/2;    $py=($h1-$h2)/2;
     69             break;
     70         }
     71         imagecopy($im, $waterIm, $px, $py, 0, 0, $w2, $h2);
     72         
     73         //3. 保存图片并销毁画布
     74         if(empty($f3)) $f3 = $f1;
     75         $saveFun = 'image' . $type_arr[$t1];
     76         $saveFun($im, $f3);
     77     
     78         imagedestroy($im);
     79         imagedestroy($waterIm);
     80     }
     81     
     82     /**
     83      * 给图片添加文字
     84      * @param $f    源图片
     85      * @param $text 文字
     86      * @param string $fc    文字颜色
     87      * @param int $px   文字x坐标
     88      * @param int $py   文字y坐标
     89      * @param int $fs   文字字体,1,2,3,4,5表示内置字体
     90      */
     91     function imgText($f, $text, $fc='#F00', $px=0, $py=0, $fs=5){
     92         $type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
     93         //获取图片信息
     94         list($width, $height, $type) = getimagesize($f);
     95     
     96         //1. 创建画布
     97         $createFun = 'imagecreatefrom' . $type_arr[$type];
     98         $im = $createFun($f);
     99         
    100         //2. 图片操作
    101         //获取颜色
    102         list($r, $g, $b) = rgbtodec($fc);
    103         $color = imagecolorallocate($im, $r, $g, $b);
    104         
    105         //计算位置(默认居中)
    106         if(empty($px) || empty($py)){
    107             $px = ($width-imagefontwidth($fs) * strlen($text))/2;
    108             $py = ($height-imagefontheight($fs))/2;
    109         }
    110         //写入字符
    111         imagestring($im, $fs, $px, $py, $text, $color);
    112         
    113         //3. 保存图片并销毁画布
    114         $saveFun = 'image' . $type_arr[$type];
    115         $saveFun($im, $f);
    116     
    117         imagedestroy($im);
    118     }
    119     //rgb值转换十进制
    120     function rgbtodec($str){
    121         $str = str_replace('#', '', $str);
    122         if(strlen($str)>4){
    123             $r = substr($str, 0, 2);
    124             $g = substr($str, 2, 2);
    125             $b = substr($str, 4, 2);
    126         }else{
    127             $r = substr($str, 0, 1); $r .= $r;
    128             $g = substr($str, 1, 1); $g .= $g;
    129             $b = substr($str, 2, 1); $b .= $b;
    130         }
    131         return array(hexdec($r), hexdec($g), hexdec($b));
    132     }
  • 相关阅读:
    java jar包运行方法
    java记录异常日志的7条规则
    java与javax有什么区别?
    java程序如何优化--技巧总结
    java中各种时间格式的转化
    java GC(Garbage Collector) | System.gc()
    java位移操作
    java 逻辑运算符 短路(条件操作)
    命令行运行java
    java使用Runtime.exec()运行windwos dos或linux shell命令
  • 原文地址:https://www.cnblogs.com/lhat/p/5567644.html
Copyright © 2011-2022 走看看