• 如何使用PHP生成图片


     79 /**
     80  * 从图片文件创建Image资源
     81  * @param $file 图片文件,支持url
     82  * @return bool|resource    成功返回图片image资源,失败返回false
     83  */
     84 function createImageFromFile($file){
     85     if(preg_match('/http(s)?:///',$file)){
     86         $fileSuffix = getNetworkImgType($file);
     87     }else{
     88         $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
     89     }
     90  
     91     if(!$fileSuffix) return false;
     92  
     93     switch ($fileSuffix){
     94         case 'jpeg':
     95             $theImage = @imagecreatefromjpeg($file);
     96             break;
     97         case 'jpg':
     98             $theImage = @imagecreatefromjpeg($file);
     99             break;
    100         case 'png':
    101             $theImage = @imagecreatefrompng($file);
    102             break;
    103         case 'gif':
    104             $theImage = @imagecreatefromgif($file);
    105             break;
    106         default:
    107             $theImage = @imagecreatefromstring(file_get_contents($file));
    108             break;
    109     }
    110  
    111     return $theImage;
    112 }
    113  
    114 /**
    115  * 获取网络图片类型
    116  * @param $url  网络图片url,支持不带后缀名url
    117  * @return bool
    118  */
    119 function getNetworkImgType($url){
    120     $ch = curl_init(); //初始化curl
    121     curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
    122     curl_setopt($ch, CURLOPT_NOBODY, 1);
    123     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
    124     curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    125     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
    126     curl_exec($ch);//执行curl会话
    127     $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
    128     curl_close($ch);//关闭资源连接
    129  
    130     if ($http_code['http_code'] == 200) {
    131         $theImgType = explode('/',$http_code['content_type']);
    132  
    133         if($theImgType[0] == 'image'){
    134             return $theImgType[1];
    135         }else{
    136             return false;
    137         }
    138     }else{
    139         return false;
    140     }
    141 }
    142  
    143 /**
    144  * 分行连续截取字符串
    145  * @param $str  需要截取的字符串,UTF-8
    146  * @param int $row  截取的行数
    147  * @param int $number   每行截取的字数,中文长度
    148  * @param bool $suffix  最后行是否添加‘...’后缀
    149  * @return array    返回数组共$row个元素,下标1到$row
    150  */
    151 function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
    152     $result = array();
    153     for ($r=1;$r<=$row;$r++){
    154         $result[$r] = '';
    155     }
    156  
    157     $str = trim($str);
    158     if(!$str) return $result;
    159  
    160     $theStrlen = strlen($str);
    161  
    162     //每行实际字节长度
    163     $oneRowNum = $number * 3;
    164     for($r=1;$r<=$row;$r++){
    165         if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
    166             $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
    167         }else{
    168             $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
    169         }
    170         if($theStrlen < $r * $oneRowNum) break;
    171     }
    172  
    173     return $result;
    174 }
    175  
    176 /**
    177  * 按字节截取utf-8字符串
    178  * 识别汉字全角符号,全角中文3个字节,半角英文1个字节
    179  * @param $str  需要切取的字符串
    180  * @param $len  截取长度[字节]
    181  * @param int $start    截取开始位置,默认0
    182  * @return string
    183  */
    184 function mg_cn_substr($str,$len,$start = 0){
    185     $q_str = '';
    186     $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);
    187  
    188     //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start
    189     if($start and json_encode(substr($str,$start,1)) === false){
    190         for($a=0;$a<3;$a++){
    191             $new_start = $start + $a;
    192             $m_str = substr($str,$new_start,3);
    193             if(json_encode($m_str) !== false) {
    194                 $start = $new_start;
    195                 break;
    196             }
    197         }
    198     }
    199  
    200     //切取内容
    201     for($i=$start;$i<$q_strlen;$i++){
    202         //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
    203         if(ord(substr($str,$i,1))>0xa0){
    204             $q_str .= substr($str,$i,3);
    205             $i+=2;
    206         }else{
    207             $q_str .= substr($str,$i,1);
    208         }
    209     }
    210     return $q_str;
    211 }

    接下来准备生成所需的背景图片,图片元素,以及字体库等

     1 /**
     2  * 分享图片生成
     3  * @param $gData  商品数据,array
     4  * @param $codeName 二维码图片
     5  * @param $fileName string 保存文件名,默认空则直接输入图片
     6  */
     7 function createSharePng($gData,$codeName,$fileName = ''){
     8    //载入图片路径
     9    //$bg_img   = createImageFromFile($gData['bg_image']);
    10 
    11    //获取图片大小
    12    $img_info = getimagesize($gData['bg_image']);
    13    $img_w    = $img_info[0];
    14    $img_h    = $img_info[1];
    15    $im       = @imagecreatetruecolor($img_w, $img_h);
    16 
    17    imagecopy($im, $bg_img,0,0,0,0, $img_w, $img_h);
    18    imagedestroy($bg_img);
    19 
    20    //第二种,创建画布,创建图片元素等
    21     //创建画布
    22     $im = imagecreatetruecolor(618, 1000);
    23  
    24     //填充画布背景色
    25     $color = imagecolorallocate($im, 255, 255, 255);
    26     imagefill($im, 0, 0, $color);
    27  
    28     //字体文件
    29     $font_file = "code_png/msyh.ttf";
    30     $font_file_bold = "code_png/msyh_bold.ttf";
    31  
    32     //设定字体的颜色
    33     $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
    34     $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
    35     $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);
    36     $font_color_red = ImageColorAllocate ($im, 217, 45, 32);
    37  
    38     $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);
    39  
    40     //Logo
    41     list($l_w,$l_h) = getimagesize('code_png/logo100_100.png');
    42     $logoImg = @imagecreatefrompng('code_png/logo100_100.png');
    43     imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);
    44  
    45     //温馨提示
    46     imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');
    47  
    48     //商品图片
    49     list($g_w,$g_h) = getimagesize($gData['pic']);
    50     $goodImg = createImageFromFile($gData['pic']);
    51     imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);
    52  
    53     //二维码
    54     list($code_w,$code_h) = getimagesize($codeName);
    55     $codeImg = createImageFromFile($codeName);
    56     imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);
    57  
    58     //商品描述
    59     $theTitle = cn_row_substr($gData['title'],2,19);
    60     imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);
    61     imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);
    62  
    63     imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后价¥");
    64     imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);
    65     imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "现价¥".$gData["original_price"]);
    66  
    67     //优惠券
    68     if($gData['coupon_price']){
    69         imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);
    70         imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);
    71         imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");
    72  
    73         $coupon_price = strval($gData['coupon_price']);
    74         imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);
    75         imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");
    76     }
    77  
    78     //输出图片
    79     if($fileName){
    80         imagepng ($im,$fileName);
    81     }else{
    82         Header("Content-Type: image/png");
    83         imagepng ($im);
    84     }
    85  
    86     //释放空间
    87     imagedestroy($im);
    88     imagedestroy($goodImg);
    89     imagedestroy($codeImg);
    90 }

    使用方法:

    1 //直接输出
    2 createSharePng($gData,'code_png/php_code.jpg');
    3 //输出到图片
    4 createSharePng($gData,'code_png/php_code.jpg','share.png');
  • 相关阅读:
    TD在IE7不能浏览的问题解决办法
    SqlServer中的IsNull
    C#日期加减
    this._form为空或不是对象
    清除自动保存的远程机器登录密码
    解决下载文件名乱码问题的简单方法
    createTextRange选中测试表格
    GridView改变行的颜色(二)
    行列转换(1)
    .NET中调用系统程序
  • 原文地址:https://www.cnblogs.com/feixiablog/p/9406995.html
Copyright © 2020-2023  润新知