首页 > 开发 > ThinkPHP > 正文

thinkphp框架 如何利用GD库在图片上编写文字(代码范例)

2024-09-09 15:20:31
字体:大 中 小
来源:转载
供稿:网友

本篇文章的主要内容是关于用GD库在图片上编写文字的代码范例,具有一定的参考价值,感兴趣的朋友可以学习了解一下,希望对你有帮助。

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * Created by PhpStorm. 
  6.  
  7.  * User: Administrator 
  8.  
  9.  */ 
  10.  
  11. namespace Home/Event; 
  12.  
  13.   
  14.  
  15. use /Think/Image; 
  16.  
  17. use /Think/Upload; 
  18.  
  19. class ImgEvent 
  20.  
  21. { 
  22.  
  23.   
  24.  
  25.     private $THINK_IMAGE = null; 
  26.  
  27.     private $THINK_UPLOAD = null; 
  28.  
  29.   
  30.  
  31.     public function __construct() 
  32.  
  33.     { 
  34.  
  35.         $this->THINK_IMAGE =  new Image(); 
  36.  
  37.         $this->THINK_UPLOAD =  new Upload(); 
  38.  
  39.     } 
  40.  
  41.   
  42.  
  43.     /* 
  44.  
  45.      * 保存base64文件 
  46.  
  47.      * $img    string    base64类型的文件 
  48.  
  49.      * $type   string    保存的文件类型 
  50.  
  51.      *      app_user_head_img   用户头像 
  52.  
  53.      * 
  54.  
  55.      * 
  56.  
  57.      */ 
  58.  
  59.     public function saveImg_base64($img = null , $type = null) 
  60.  
  61.     { 
  62.  
  63.         //获取保存图片配置 
  64.  
  65.         $imgConfig_savePath = C("img_save.save_path"); 
  66.  
  67.         $imgConfig_size     = C("img_save.size"); 
  68.  
  69.         $saveFlag = false; 
  70.  
  71. //        dump($imgConfig_savePath[$type]); 
  72.  
  73. //        dump($imgConfig_size); 
  74.  
  75.   
  76.  
  77.         if (preg_match('/^(data:/s*image//(/w+);base64,)/', $img, $result) && $imgConfig_savePath[$type]) 
  78.  
  79.         { 
  80.  
  81.   
  82.  
  83.             $img_ext                = $result[2]; //图片后缀 
  84.  
  85.             $img_header             = $result[1];//图片头信息 
  86.  
  87.             $new_file_name          = date('Ymd').'/'.uniqid().'.'.$img_ext; 
  88.  
  89.             $origin_img_path        = '';//原图的保存路径 
  90.  
  91.             $origin_img_save_flag   = true;// 
  92.  
  93.             foreach($imgConfig_savePath[$type] as $k => $v) 
  94.  
  95.             { 
  96.  
  97.   
  98.  
  99.                 if(!is_dir($v.date('Ymd'))) 
  100.  
  101.                 { 
  102.  
  103.                     mkdir($v.date('Ymd'),0777,true); 
  104.  
  105.                 } 
  106.  
  107.   
  108.  
  109.                 if ($k == 'origin') 
  110.  
  111.                 { 
  112.  
  113.                     //先保存一份原图,然后其他尺寸的保存直接调用原图路径origin_img_path. 
  114.  
  115.                     $origin_res = file_put_contents($v.$new_file_name, base64_decode(str_replace($img_header, '', $img))); 
  116.  
  117.                     if (!$origin_res) 
  118.  
  119.                     { 
  120.  
  121.                         $origin_img_save_flag = false; 
  122.  
  123.                         break; 
  124.  
  125.                     } 
  126.  
  127.                     else 
  128.  
  129.                     { 
  130.  
  131.                         $saveFlag = $new_file_name; 
  132.  
  133.                         $origin_img_path = $v.$new_file_name; 
  134.  
  135.                         $this->THINK_IMAGE->open($origin_img_path); 
  136.  
  137.                     } 
  138.  
  139.                 } 
  140.  
  141.                 else 
  142.  
  143.                 { 
  144.  
  145.                     if ($origin_img_save_flag) 
  146.  
  147.                     { 
  148.  
  149.                         $width = $imgConfig_size[$type][$k]['w']; 
  150.  
  151.                         $height = $imgConfig_size[$type][$k]['h']; 
  152.  
  153.                         $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$new_file_name); 
  154.  
  155.                     } 
  156.  
  157.   
  158.  
  159.                 } 
  160.  
  161.             } 
  162.  
  163.   
  164.  
  165.         } 
  166.  
  167.         return $saveFlag; 
  168.  
  169.     } 
  170.  
  171.   
  172.  
  173.   
  174.  
  175.     /* 
  176.  
  177.      *  保存file类型文件 
  178.  
  179.      * 
  180.  
  181.      */ 
  182.  
  183.     public function saveImg_file($img = null , $type = null) 
  184.  
  185.     { 
  186.  
  187.   
  188.  
  189.         //获取保存图片配置 
  190.  
  191.         $imgConfig_savePath = C("img_save.save_path"); 
  192.  
  193.         $imgConfig_size     = C("img_save.size"); 
  194.  
  195.         $saveFlag = false; 
  196.  
  197.   
  198.  
  199.         if ($img && $imgConfig_savePath[$type]) 
  200.  
  201.         { 
  202.  
  203.   
  204.  
  205.             $origin_img_save_flag   = true; 
  206.  
  207.             foreach($imgConfig_savePath[$type] as $k => $v) 
  208.  
  209.             { 
  210.  
  211.   
  212.  
  213.                 if(!is_dir($v.date('Ymd'))) 
  214.  
  215.                 { 
  216.  
  217.                     mkdir($v.date('Ymd'),0777,true); 
  218.  
  219.                 } 
  220.  
  221.   
  222.  
  223.                 if ($k == 'origin') 
  224.  
  225.                 { 
  226.  
  227.                     $this->THINK_UPLOAD->rootPath = $v; 
  228.  
  229.                     $this->THINK_UPLOAD->subName = date('Ymd'); 
  230.  
  231.                     $this->THINK_UPLOAD->saveName = array('uniqid',''); 
  232.  
  233.                     $saveRes = $this->THINK_UPLOAD->uploadOne($img); 
  234.  
  235.                     if (!$saveRes) 
  236.  
  237.                     { 
  238.  
  239.                         $origin_img_save_flag = false; 
  240.  
  241.                     } 
  242.  
  243.                     else 
  244.  
  245.                     { 
  246.  
  247.                         $saveFlag = true; 
  248.  
  249.                         $origin_img_path = $v.$saveRes['savepath'].$saveRes['savename']; 
  250.  
  251.                         $this->THINK_IMAGE->open($origin_img_path); 
  252.  
  253.                         $fileName = $saveRes['savepath'].$saveRes['savename']; 
  254.  
  255.                     } 
  256.  
  257.                 } 
  258.  
  259.                 else 
  260.  
  261.                 { 
  262.  
  263.                     if ($origin_img_save_flag) 
  264.  
  265.                     { 
  266.  
  267.                         $width = $imgConfig_size[$type][$k]['w']; 
  268.  
  269.                         $height = $imgConfig_size[$type][$k]['h']; 
  270.  
  271.                         $compress_img_path=$v.$fileName; 
  272.  
  273.                         $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName); 
  274.  
  275.   
  276.  
  277.                         if (!$saveFlag) 
  278.  
  279.                         { 
  280.  
  281.                             $saveFlag = false; 
  282.  
  283.                             $origin_img_save_flag = false; 
  284.  
  285.                         } 
  286.  
  287.   
  288.  
  289.                     } 
  290.  
  291.   
  292.  
  293.                 } 
  294.  
  295.   
  296.  
  297.   
  298.  
  299.             } 
  300.  
  301.   
  302.  
  303.             if($saveFlag) 
  304.  
  305.             { 
  306.  
  307.                 #$saveFlag = $fileName; 
  308.  
  309.                 $saveFlag = array('origin'=>get_current_Host().'/'.$origin_img_path,'compress'=>get_current_Host().'/'.$compress_img_path); 
  310.  
  311.             } 
  312.  
  313.   
  314.  
  315.         } 
  316.  
  317.         return $saveFlag; 
  318.  
  319.     } 
  320.  
  321.   
  322.  
  323.     public function save_file($img = null , $type = null) 
  324.  
  325.     { 
  326.  
  327.   
  328.  
  329.         //获取保存图片配置 
  330.  
  331.         $imgConfig_savePath = C("img_save.save_path"); 
  332.  
  333.         $saveFlag = false; 
  334.  
  335.   
  336.  
  337.         if ($img && $imgConfig_savePath[$type]) { 
  338.  
  339.   
  340.  
  341.   
  342.  
  343.                 if (!is_dir($imgConfig_savePath[$type] . date('Ymd'))) { 
  344.  
  345.                     mkdir($imgConfig_savePath[$type] . date('Ymd'), 0777, true); 
  346.  
  347.                 } 
  348.  
  349.                 $this->THINK_UPLOAD->rootPath = $imgConfig_savePath[$type]; 
  350.  
  351.                 $this->THINK_UPLOAD->subName = date('Ymd'); 
  352.  
  353.                 $this->THINK_UPLOAD->saveName = array('uniqid', ''); 
  354.  
  355.                 $saveRes = $this->THINK_UPLOAD->uploadOne($img); 
  356.  
  357.                 if ($saveRes) { 
  358.  
  359.                     $saveFlag = true; 
  360.  
  361.                     $origin_img_path = $imgConfig_savePath[$type] . $saveRes['savepath'] . $saveRes['savename']; 
  362.  
  363.   
  364.  
  365.                     //$fileName = $saveRes['savepath'] . $saveRes['savename']; 
  366.  
  367.                 } 
  368.  
  369.             } 
  370.  
  371.   
  372.  
  373.   
  374.  
  375.             if($saveFlag) 
  376.  
  377.             { 
  378.  
  379.                 $saveFlag = $origin_img_path; 
  380.  
  381.                 return get_current_Host().'/'.$saveFlag; 
  382.  
  383.             }else 
  384.  
  385.             { 
  386.  
  387.                 return $saveFlag; 
  388.  
  389.             } 
  390.  
  391.   
  392.  
  393.   
  394.  
  395.     } 
  396.  
  397.   
  398.  
  399.   
  400.  
  401.   
  402.  
  403.   
  404.  
  405.     /* 
  406.  
  407.      *  保存file类型文件,多文件 
  408.  
  409.      * 
  410.  
  411.      */ 
  412.  
  413.     public function saveImgs_files($imgs = null , $type = null) 
  414.  
  415.     { 
  416.  
  417.   
  418.  
  419.         //获取保存图片配置s 
  420.  
  421.         $imgConfig_savePath = C("img_save.save_path"); 
  422.  
  423.         $imgConfig_size     = C("img_save.size"); 
  424.  
  425.         $saveFlag = false; 
  426.  
  427.         $imgResArr = array();//保存图片路径 
  428.  
  429.         $origin_img_path = array();//原图路径 
  430.  
  431.   
  432.  
  433.         if ($imgs && $imgConfig_savePath[$type]) 
  434.  
  435.         { 
  436.  
  437.   
  438.  
  439.             $origin_img_save_flag   = true; 
  440.  
  441.             foreach($imgConfig_savePath[$type] as $k => $v) 
  442.  
  443.             { 
  444.  
  445.   
  446.  
  447.                 if(!is_dir($v.date('Ymd'))) 
  448.  
  449.                 { 
  450.  
  451.                     mkdir($v.date('Ymd'),0777,true); 
  452.  
  453.                 } 
  454.  
  455.   
  456.  
  457.                 if ($k == 'origin') 
  458.  
  459.                 { 
  460.  
  461.                     $this->THINK_UPLOAD->rootPath = $v; 
  462.  
  463.                     $this->THINK_UPLOAD->subName = date('Ymd'); 
  464.  
  465.                     $this->THINK_UPLOAD->saveName = array('uniqid',''); 
  466.  
  467.                     $saveRes = $this->THINK_UPLOAD->upload($imgs); 
  468.  
  469.   
  470.  
  471.                     if ($saveRes) 
  472.  
  473.                     { 
  474.  
  475.                         $saveFlag = true; 
  476.  
  477.                         foreach ($saveRes as $srK => $srV) 
  478.  
  479.                         { 
  480.  
  481.                             $origin_img_path[] = $v.$saveRes[$srK]['savepath'].$saveRes[$srK]['savename']; 
  482.  
  483.                             $fileName = $saveRes[$srK]['savepath'].$saveRes[$srK]['savename']; 
  484.  
  485.                             $imgResArr[] = $fileName; 
  486.  
  487.                         } 
  488.  
  489.   
  490.  
  491.   
  492.  
  493.                     } 
  494.  
  495.                 } 
  496.  
  497.                 else 
  498.  
  499.                 { 
  500.  
  501.                     foreach($origin_img_path as $oipK => $oipV) 
  502.  
  503.                     { 
  504.  
  505.                         if ($saveFlag) 
  506.  
  507.                         { 
  508.  
  509.                             $width = $imgConfig_size[$type][$k]['w']; 
  510.  
  511.                             $height = $imgConfig_size[$type][$k]['h']; 
  512.  
  513.                             $this->THINK_IMAGE->open($oipV); 
  514.  
  515.                             $saveThumb = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$imgResArr[$oipK]); 
  516.  
  517.                             if (!$saveThumb) 
  518.  
  519.                             { 
  520.  
  521.                                 $saveFlag = false; 
  522.  
  523.                                 break; 
  524.  
  525.                             } 
  526.  
  527.                         } 
  528.  
  529.                     } 
  530.  
  531.   
  532.  
  533.                 } 
  534.  
  535.   
  536.  
  537.             } 
  538.  
  539.   
  540.  
  541.             if ($saveFlag) 
  542.  
  543.             { 
  544.  
  545.                 $saveFlag = $imgResArr; 
  546.  
  547.             } 
  548.  
  549.         } 
  550.  
  551.         return $saveFlag; 
  552.  
  553.     } 
  554.  
  555.   
  556.  
  557.     /* 
  558.  
  559.      * 保存微信头像 - url 
  560.  
  561.      * 
  562.  
  563.      */ 
  564.  
  565.     public function save_WxImg($picUrl = '',$type = null) 
  566.  
  567.     { 
  568.  
  569.   
  570.  
  571.         $ch = curl_init($picUrl); 
  572.  
  573.         curl_setopt($ch, CURLOPT_HEADER, 0); 
  574.  
  575.         curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。 
  576.  
  577.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  578.  
  579.         $package = curl_exec($ch); 
  580.  
  581.         $httpInfo = curl_getinfo($ch); 
  582.  
  583.         curl_close($ch); 
  584.  
  585.         $media = array_merge(array('mediaBody' => $package), $httpInfo); 
  586.  
  587.   
  588.  
  589.         //求出文件格式 
  590.  
  591.         preg_match('//w//(/w+)/i', $media["content_type"], $extmatches); 
  592.  
  593.         $fileExt = $extmatches[1]; 
  594.  
  595.   
  596.  
  597.         $saveName = md5(microtime(true)).".{$fileExt}"; 
  598.  
  599.   
  600.  
  601.         //获取保存图片配置 
  602.  
  603.         $imgConfig_savePath = C("img_save.save_path"); 
  604.  
  605.         $imgConfig_size     = C("img_save.size"); 
  606.  
  607.         $saveFlag = false; 
  608.  
  609.   
  610.  
  611.         if ($imgConfig_savePath[$type]) 
  612.  
  613.         { 
  614.  
  615.   
  616.  
  617.             $origin_img_save_flag   = true; 
  618.  
  619.             foreach($imgConfig_savePath[$type] as $k => $v) 
  620.  
  621.             { 
  622.  
  623.   
  624.  
  625.                 if(!is_dir($v.date('Ymd'))) 
  626.  
  627.                 { 
  628.  
  629.                     mkdir($v.date('Ymd'),0777,true); 
  630.  
  631.                 } 
  632.  
  633.   
  634.  
  635.                 if ($k == 'origin') 
  636.  
  637.                 { 
  638.  
  639.                     file_put_contents($v.'/'.date('Ymd').'/'.$saveName,$media['mediaBody']); 
  640.  
  641.   
  642.  
  643.                     $origin_img_path = $v.'/'.date('Ymd').'/'.$saveName; 
  644.  
  645.                     $this->THINK_IMAGE->open($origin_img_path); 
  646.  
  647.                     $fileName = date('Ymd').'/'.$saveName; 
  648.  
  649.                 } 
  650.  
  651.                 else 
  652.  
  653.                 { 
  654.  
  655.                     if ($origin_img_save_flag) 
  656.  
  657.                     { 
  658.  
  659.                         $width = $imgConfig_size[$type][$k]['w']; 
  660.  
  661.                         $height = $imgConfig_size[$type][$k]['h']; 
  662.  
  663.                         $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName); 
  664.  
  665.   
  666.  
  667.                         if (!$saveFlag) 
  668.  
  669.                         { 
  670.  
  671.                             $saveFlag = false; 
  672.  
  673.                             $origin_img_save_flag = false; 
  674.  
  675.                         } 
  676.  
  677.                         else 
  678.  
  679.                         { 
  680.  
  681.                             $saveFlag = $fileName; 
  682.  
  683.                         } 
  684.  
  685.                     } 
  686.  
  687.   
  688.  
  689.                 } 
  690.  
  691.             } 
  692.  
  693.         } 
  694.  
  695.         return $saveFlag; 
  696.  
  697.   
  698.  
  699.     } 
  700.  
  701.   
  702.  
  703.     /* 
  704.  
  705.      *  保存file类型文件 
  706.  
  707.      * 
  708.  
  709.      */ 
  710.  
  711.     public function saveImg_Wx($WxImg = array() , $type = null) 
  712.  
  713.     { 
  714.  
  715.   
  716.  
  717.         //获取保存图片配置 
  718.  
  719.         $imgConfig_savePath = C("img_save.save_path"); 
  720.  
  721.         $imgConfig_size     = C("img_save.size"); 
  722.  
  723.         $saveFlag = false; 
  724.  
  725.         $imgResArr = array(); 
  726.  
  727.           
  728.  
  729.         if ($WxImg && $imgConfig_savePath[$type]) 
  730.  
  731.         { 
  732.  
  733.   
  734.  
  735.   
  736.  
  737.             foreach ($WxImg as $wiK => $wiV) 
  738.  
  739.             { 
  740.  
  741.   
  742.  
  743.                 $wxImg = $this->getWxImg($wiV); 
  744.  
  745.   
  746.  
  747.                 $origin_img_save_flag   = true; 
  748.  
  749.                 foreach($imgConfig_savePath[$type] as $k => $v) 
  750.  
  751.                 { 
  752.  
  753.   
  754.  
  755.                     if(!is_dir($v.date('Ymd'))) 
  756.  
  757.                     { 
  758.  
  759.                         mkdir($v.date('Ymd'),0777,true); 
  760.  
  761.                     } 
  762.  
  763.   
  764.  
  765.                     if ($k == 'origin') 
  766.  
  767.                     { 
  768.  
  769.                         file_put_contents($v.'/'.date('Ymd').'/'.$wxImg['saveName'],$wxImg['imgMedia']['mediaBody']); 
  770.  
  771.   
  772.  
  773.                         $origin_img_path = $v.'/'.date('Ymd').'/'.$wxImg['saveName']; 
  774.  
  775.                         $this->THINK_IMAGE->open($origin_img_path); 
  776.  
  777.                         $fileName = date('Ymd').'/'.$wxImg['saveName']; 
  778.  
  779.                         $imgResArr[] = $fileName; 
  780.  
  781.                     } 
  782.  
  783.                     else 
  784.  
  785.                     { 
  786.  
  787.                         if ($origin_img_save_flag) 
  788.  
  789.                         { 
  790.  
  791.                             $width = $imgConfig_size[$type][$k]['w']; 
  792.  
  793.                             $height = $imgConfig_size[$type][$k]['h']; 
  794.  
  795.                             $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName); 
  796.  
  797.   
  798.  
  799.                             if (!$saveFlag) 
  800.  
  801.                             { 
  802.  
  803.                                 $saveFlag = false; 
  804.  
  805.                                 $origin_img_save_flag = false; 
  806.  
  807.                             } 
  808.  
  809.                             else 
  810.  
  811.                             { 
  812.  
  813.                                 $saveFlag = $fileName; 
  814.  
  815.                             } 
  816.  
  817.                         } 
  818.  
  819.   
  820.  
  821.                     } 
  822.  
  823.                 } 
  824.  
  825.             } 
  826.  
  827.   
  828.  
  829.   
  830.  
  831.             if($saveFlag) 
  832.  
  833.             { 
  834.  
  835.                 $saveFlag = $imgResArr; 
  836.  
  837.             } 
  838.  
  839.   
  840.  
  841.         } 
  842.  
  843.         return $saveFlag; 
  844.  
  845.     } 
  846.  
  847.    
  848.  
  849.   
  850.  
  851.     /* 
  852.  
  853.      * 保存用户的推广图片   在图片上写字!!!! 
  854.  
  855.      */ 
  856.  
  857.     public function save_userShareImg($bg_img_path = '' , $head_img_path = '' , $qrCode_img_path = '' , $saveFile = '' , $userName = '' , $userDscp = '' , $font_path = '') 
  858.  
  859.     { 
  860.  
  861.   
  862.  
  863.           
  864.  
  865.         //设置头像图片为圆角 
  866.  
  867.         $head_img_radius = $this->radius_img($head_img_path,240); 
  868.  
  869.         //合并到背景图中 
  870.  
  871.         $this->mergeImage($bg_img_path,$head_img_radius,$saveFile,array('left' => 200, 'top' => 55, 'width' => 480, 'height' => 480)); 
  872.  
  873.         //设置名称 
  874.  
  875.         $this->writeText($saveFile, $saveFile, $userName,255,255,255,1010,32,$font_path, array()); 
  876.  
  877.         //设置二维码内容 
  878.  
  879.         $this->mergeImage($saveFile,$qrCode_img_path,$saveFile,array('left' => 255, 'top' => 684, 'width' => 250, 'height' => 245)); 
  880.  
  881.   
  882.  
  883.         return $saveFile; 
  884.  
  885.     } 
  886.  
  887.   
  888.  
  889.     /* 
  890.  
  891.      * 合并图片 
  892.  
  893.      */ 
  894.  
  895.     private function mergeImage($bg_img, $main_img, $saveFile, $param) 
  896.  
  897.     { 
  898.  
  899.   
  900.  
  901.         extract($param); 
  902.  
  903. //        list($bgWidth, $bgHeight) = getimagesize($bg_img); 
  904.  
  905.         $bgImg = $this->imagecreate($bg_img); 
  906.  
  907.         if (!is_resource($main_img)) 
  908.  
  909.         { 
  910.  
  911.             list($mainWidth, $mainHeight) = getimagesize($main_img); 
  912.  
  913.             $mainImg = $this->imagecreate($main_img); 
  914.  
  915.         } 
  916.  
  917.         else 
  918.  
  919.         { 
  920.  
  921.             $mainImg = $main_img; 
  922.  
  923.   
  924.  
  925.             $mainWidth = $width; 
  926.  
  927.             $mainHeight = $height; 
  928.  
  929.         } 
  930.  
  931.         imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $mainWidth, $mainHeight); 
  932.  
  933. //        imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $width,$height); 
  934.  
  935.         ob_start(); 
  936.  
  937.         // output jpeg (or any other chosen) format & quality 
  938.  
  939.         imagejpeg($bgImg, NULL, 100); 
  940.  
  941.         $contents = ob_get_contents(); 
  942.  
  943.         ob_end_clean(); 
  944.  
  945.         imagedestroy($bgImg); 
  946.  
  947.         imagedestroy($mainImg); 
  948.  
  949.         $fh = fopen($saveFile, "w+"); 
  950.  
  951.         fwrite($fh, $contents); 
  952.  
  953.         fclose($fh); 
  954.  
  955.     } 
  956.  
  957.   
  958.  
  959.     /* 
  960.  
  961.      * 图片写文字 
  962.  
  963.      * $bg_img 背景图片 
  964.  
  965.      * $saveFile 保存图片路径 
  966.  
  967.      * $text 文字 
  968.  
  969.      * $fontColor 字体颜色 
  970.  
  971.      * $top 图片距离顶端高度 
  972.  
  973.      * $fontSize 字体大小 
  974.  
  975.      * $font 字体路径 
  976.  
  977.      * $param 传递的参数 
  978.  
  979.      */ 
  980.  
  981.     private function writeText($bg_img, $saveFile, $text, $colorR,$colorG,$colorB , $top , $fontSize , $font , $param = array()) 
  982.  
  983.     { 
  984.  
  985.         list($bgWidth, $bgHeight) = getimagesize($bg_img); 
  986.  
  987.         $im = imagecreatefromjpeg($bg_img); 
  988.  
  989.   
  990.  
  991.         $fontColor = imagecolorallocate($im, $colorR, $colorG, $colorB);//字的RGB颜色 
  992.  
  993.         $str = mb_convert_encoding($text, "html-entities", "utf-8");;//解决乱码问题 
  994.  
  995.   
  996.  
  997.         $fontBox = imagettfbbox($fontSize, 0, $font, $str);//文字水平居中实质 
  998.  
  999.         $width = imagesx($im); 
  1000.  
  1001.         imagettftext($im, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $top, $fontColor, $font, $str); 
  1002.  
  1003.   
  1004.  
  1005.   
  1006.  
  1007.         ob_start(); 
  1008.  
  1009.         // output jpeg (or any other chosen) format & quality 
  1010.  
  1011.         imagejpeg($im, NULL, 100); 
  1012.  
  1013.         $contents = ob_get_contents(); 
  1014.  
  1015.         ob_end_clean(); 
  1016.  
  1017.         imagedestroy($im); 
  1018.  
  1019.         $fh = fopen($saveFile, "w+"); 
  1020.  
  1021.         fwrite($fh, $contents); 
  1022.  
  1023.         fclose($fh); 
  1024.  
  1025.     } 
  1026.  
  1027.   
  1028.  
  1029.     /** 
  1030.  
  1031.      * 处理圆角图片 
  1032.  
  1033.      * @param  string $imgPath 源图片路径 
  1034.  
  1035.      * @param  integer $radius 圆角半径长度默认为15,处理成圆型 
  1036.  
  1037.      * @return [type]           [description] 
  1038.  
  1039.      */ 
  1040.  
  1041.     public function radius_img($imgPath = '', $radius = 65) 
  1042.  
  1043.     { 
  1044.  
  1045.         $ext = pathinfo($imgPath); 
  1046.  
  1047.         $src_img = null; 
  1048.  
  1049.         switch ($ext['extension']) { 
  1050.  
  1051.             case 'jpg': 
  1052.  
  1053.                 $src_img = imagecreatefromjpeg($imgPath); 
  1054.  
  1055.                 break; 
  1056.  
  1057.             case 'png': 
  1058.  
  1059.                 $src_img = imagecreatefrompng($imgPath); 
  1060.  
  1061.                 break; 
  1062.  
  1063.             default: 
  1064.  
  1065.                 $src_img = imagecreatefromjpeg($imgPath); 
  1066.  
  1067.                 break; 
  1068.  
  1069.         } 
  1070.  
  1071.         $wh = getimagesize($imgPath); 
  1072.  
  1073.         $w = $wh[0]; 
  1074.  
  1075.         $h = $wh[1]; 
  1076.  
  1077.         // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius; 
  1078.  
  1079.         $img = imagecreatetruecolor($w, $h); 
  1080.  
  1081.         //这一句一定要有 
  1082.  
  1083.         imagesavealpha($img, true); 
  1084.  
  1085.         //拾取一个完全透明的颜色,最后一个参数127为全透明 
  1086.  
  1087.         $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); 
  1088.  
  1089.         imagefill($img, 0, 0, $bg); 
  1090.  
  1091.         $r = $radius; //圆 角半径 
  1092.  
  1093.         for ($x = 0; $x < $w; $x++) { 
  1094.  
  1095.             for ($y = 0; $y < $h; $y++) { 
  1096.  
  1097.                 $rgbColor = imagecolorat($src_img, $x, $y); 
  1098.  
  1099.                 if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) { 
  1100.  
  1101.                     //不在四角的范围内,直接画 
  1102.  
  1103.                     imagesetpixel($img, $x, $y, $rgbColor); 
  1104.  
  1105.                 } else { 
  1106.  
  1107.                     //在四角的范围内选择画 
  1108.  
  1109.                     //上左 
  1110.  
  1111.                     $y_x = $r; //圆心X坐标 
  1112.  
  1113.                     $y_y = $r; //圆心Y坐标 
  1114.  
  1115.                     if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { 
  1116.  
  1117.                         imagesetpixel($img, $x, $y, $rgbColor); 
  1118.  
  1119.                     } 
  1120.  
  1121.                     //上右 
  1122.  
  1123.                     $y_x = $w - $r; //圆心X坐标 
  1124.  
  1125.                     $y_y = $r; //圆心Y坐标 
  1126.  
  1127.                     if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { 
  1128.  
  1129.                         imagesetpixel($img, $x, $y, $rgbColor); 
  1130.  
  1131.                     } 
  1132.  
  1133.                     //下左 
  1134.  
  1135.                     $y_x = $r; //圆心X坐标 
  1136.  
  1137.                     $y_y = $h - $r; //圆心Y坐标 
  1138.  
  1139.                     if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { 
  1140.  
  1141.                         imagesetpixel($img, $x, $y, $rgbColor); 
  1142.  
  1143.                     } 
  1144.  
  1145.                     //下右 
  1146.  
  1147.                     $y_x = $w - $r; //圆心X坐标 
  1148.  
  1149.                     $y_y = $h - $r; //圆心Y坐标 
  1150.  
  1151.                     if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { 
  1152.  
  1153.                         imagesetpixel($img, $x, $y, $rgbColor); 
  1154.  
  1155.                     } 
  1156.  
  1157.                 } 
  1158.  
  1159.             } 
  1160.  
  1161.         } 
  1162.  
  1163.         return $img; 
  1164.  
  1165.     } 
  1166.  
  1167.   
  1168.  
  1169.     private function imagecreate($bg) 
  1170.  
  1171.     { 
  1172.  
  1173.         $bgImg = @imagecreatefromjpeg($bg); 
  1174.  
  1175.         if (FALSE == $bgImg) { 
  1176.  
  1177.             $bgImg = @imagecreatefrompng($bg); 
  1178.  
  1179.         } 
  1180.  
  1181.         if (FALSE == $bgImg) { 
  1182.  
  1183.             $bgImg = @imagecreatefromgif($bg); 
  1184.  
  1185.         } 
  1186.  
  1187.         return $bgImg; 
  1188.  
  1189.     } 
  1190.  
  1191.   
  1192.  
  1193.   
  1194.  
  1195.     /* 
  1196.  
  1197.      * 下载微信服务器的图片 
  1198.  
  1199.      * media_id    微信的媒体id 
  1200.  
  1201.      * type        保存图片类型 
  1202.  
  1203.      * */ 
  1204.  
  1205.     public function getWxImg($mediaId) 
  1206.  
  1207.     { 
  1208.  
  1209.         if(!class_exists('/JSSDK') && !class_exists('/WxPayConfig')) 
  1210.  
  1211.         { 
  1212.  
  1213.             vendor('wxsdkapi.jssdk'); 
  1214.  
  1215.             vendor('wxpayapi.lib.WxPay#Config'); 
  1216.  
  1217.         } 
  1218.  
  1219.         $res = new /JSSDK(C('WEIXINPAY_CONFIG.APPID'),C('WEIXINPAY_CONFIG.APPSECRET')); 
  1220.  
  1221.         $accessToken = $res->getAccessToken(); 
  1222.  
  1223.       
  1224.  
  1225. //    $mediaId = 'cVqPIhDPPThUtayzS27GK6RN4-dseA6AZnTJxcg7_VaeuAaKcL53X32hmKxhgiSD'; 
  1226.  
  1227. //    $accessToken = 'KHaopJY444kJ06LcH9ESj-73pkEfFTgIr3L45RURuUxv1KTjgXyFepJ-cTIF_oUyAKyYb5dK2r9L_wC-WDw2USqGQBMobC6DzPImuhPqxB8jxZ41CuLDtJiaEZk8Vf5kWSSjAAAAWW'; 
  1228.  
  1229.         $picUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=$mediaId"; 
  1230.  
  1231.   
  1232.  
  1233.         $ch = curl_init($picUrl); 
  1234.  
  1235.         curl_setopt($ch, CURLOPT_HEADER, 0); 
  1236.  
  1237.         curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。 
  1238.  
  1239.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  1240.  
  1241.         $package = curl_exec($ch); 
  1242.  
  1243.         $httpInfo = curl_getinfo($ch); 
  1244.  
  1245.         curl_close($ch); 
  1246.  
  1247.         $media = array_merge(array('mediaBody' => $package), $httpInfo); 
  1248.  
  1249.   
  1250.  
  1251.         //求出文件格式 
  1252.  
  1253.         preg_match('//w//(/w+)/i', $media["content_type"], $extmatches); 
  1254.  
  1255.         $fileExt = $extmatches[1]; 
  1256.  
  1257.   
  1258.  
  1259.         $saveName = md5(microtime(true)).".{$fileExt}"; 
  1260.  
  1261.   
  1262.  
  1263.         $img = array('imgMedia'=>$media,'saveName'=>$saveName); 
  1264.  
  1265.   
  1266.  
  1267.         return $img; 
  1268.  
  1269.     } 
  1270.  
  1271.         /* 
  1272.  
  1273.      * 下载微信服务器的图片 
  1274.  
  1275.      * media_id    微信的媒体id 
  1276.  
  1277.      * type        保存图片类型 
  1278.  
  1279.      * */ 
  1280.  
  1281.     public function getWxImg2($mediaId,$file) 
  1282.  
  1283.     { 
  1284.  
  1285.         if(!class_exists('/JSSDK') && !class_exists('/WxPayConfig')) 
  1286.  
  1287.         { 
  1288.  
  1289.             vendor('wxsdkapi.jssdk'); 
  1290.  
  1291.             vendor('wxpayapi.lib.WxPay#Config'); 
  1292.  
  1293.         } 
  1294.  
  1295.         $res = new /JSSDK(C('WEIXINPAY_CONFIG.APPID'),C('WEIXINPAY_CONFIG.APPSECRET')); 
  1296.  
  1297.         $accessToken = $res->getAccessToken(); 
  1298.  
  1299.         $picUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=$mediaId"; 
  1300.  
  1301.       
  1302.  
  1303.           
  1304.  
  1305.          
  1306.  
  1307.   
  1308.  
  1309.         return httpcopy($picUrl,$file); 
  1310.  
  1311.     } 
  1312.  
  1313.   
  1314.  
  1315. } 

调用方法:

  1. /** 
  2.  
  3.      * 生成图片 
  4.  
  5.      * @param varchar  tid 任务id 
  6.  
  7.      * @httpmethod POST 
  8.  
  9.      * @response 响应数据 
  10.  
  11.      * { 
  12.  
  13.      *   "ResultType": "1000", 
  14.  
  15.      *   "Message": "上传成功", 
  16.  
  17.      *   "AppendData": "图片路径", 
  18.  
  19.      * } 
  20.  
  21.      */ 
  22.  
  23.     public function UploadNavImg() 
  24.  
  25.     { 
  26.  
  27.         $tid=Util::getSafeText(I('post.tid')); 
  28.  
  29.         $imgEven = A('Gszc/Img','Event'); 
  30.  
  31.         //查出公司名称 
  32.  
  33.         $res = M('gszc_tasks as t') 
  34.  
  35.             ->join('app_gszc_userinfo as u on u.tid=t.id') 
  36.  
  37.             ->where('t.id='.$tid) 
  38.  
  39.             ->field('u.*,t.lastname') 
  40.  
  41.             ->find(); 
  42.  
  43.   
  44.  
  45.         //主营业务范围 
  46.  
  47.         $zyywfws = explode('、',$res['zyywfw']); 
  48.  
  49.         $zyywfw = array_slice($zyywfws, 0,3);  //获取前三个主营业范围 
  50.  
  51.         $zyywfw2 = array_slice($zyywfws, 3,6);  //获取后三个主营业务范围 
  52.  
  53.   
  54.  
  55.         $imageName = date("His",time())."_".rand(1111,9999).'.jpg'; 
  56.  
  57.         $dir = date('Ymd'); 
  58.  
  59.         $path = 'uploads/company/'.$dir; 
  60.  
  61.         if (!is_dir($path)){ //判断目录是否存在 不存在就创建 
  62.  
  63.             mkdir($path,0777,true); 
  64.  
  65.         } 
  66.  
  67.         $img = $imgEven->save_userShareImg($_SERVER['DOCUMENT_ROOT']."/uploads/test.png",'','',$_SERVER['DOCUMENT_ROOT']."/".$path."/". $imageName,$res['lastname'],$zyywfw, $zyywfw2, '',$_SERVER['DOCUMENT_ROOT']."/uploads/PingFang Bold.ttf"); 
  68.  
  69.   
  70.  
  71.         if($img){ 
  72.  
  73.             //生成照片成功 保存到任务表里 
  74.  
  75.             $tasks = M('gszc_tasks')->where('id='.$tid)->setField('lastpic',$path."/".$imageName); 
  76.  
  77.             if($tasks){ 
  78.  
  79.                 $lujing = $path."/".$imageName; 
  80.  
  81.                 $this->array_return['errno']=self::__OK__; 
  82.  
  83.                 $this->array_return['errmsg']='生成成功!'; 
  84.  
  85.                 $this->array_return['data']= $lujing ; 
  86.  
  87.                 $this->ajaxReturn($this->array_return); 
  88.  
  89.             } 
  90.  
  91.         }else{ 
  92.  
  93.             $this->array_return['errno']=self::__ERROR__; 
  94.  
  95.             $this->array_return['errmsg']='生成失败!'; 
  96.  
  97.             $this->array_return['data']=[]; 
  98.  
  99.             $this->ajaxReturn($this->array_return); 
  100.  
  101.         } 
  102.  
  103.     } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选