• php 多个图片合并为一张


    文字换行生成一张图片

    且和多张图片合并为一张

    <?php
    $fontUrl = '/usr/share/fonts/truetype/myyuanjian/汉仪细中圆简.ttf';
    $fontUrl = '/usr/share/fonts/truetype/arphic/ukai.ttc';
    header("Content-type: image/png");
    mb_internal_encoding("UTF-8"); // 设置编码
    
    
    
    
    function autowrap($fontsize, $angle, $fontface, $string, $width) {
    // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
        $content = "";
        // 将字符串拆分成一个个单字 保存到数组 letter 中
        for ($i = 0; $i < mb_strlen($string); $i++) {
            $letter[] = mb_substr($string, $i, 1);
        }
        foreach ($letter as $l) {
            $teststr = $content . "" . $l;
            $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
    //        var_dump($teststr, $testbox);
            // 判断拼接后的字符串是否超过预设的宽度
            if (($testbox[2] > $width) && ($content !== "")) {
                
                $content .= "
    ";
            }
            $content .= $l;
        }
        return array($content, $testbox);
    }
    //
    $imgWidth = 640;
    $imgHeight = 1024;
    $text = "前段时间练习使用 PHP 的 GD 库时,为了文本的自动换行纠结了很久。虽然可以通过插入 \n 实现换行,但考虑到文本中既有中文又有英文,强制限定每多少个文字就换行的效果很差。后来终于找到了一个英文下的自动换行的方法,其大概原理是将空格作为分隔符,将字符串分割为一个个单词,然后再一个接一个地拼接在一起,判断其长度是否超过画布,若超过则换行再拼接,否则继续拼接。考虑到中文需要将每个文字都拆开,所以我进行了一点修改,完整代码如下。";
    list($text, $textBox) = autowrap(14, 0, $fontUrl, $text, 640); // 自动换行处理
    $imgHeight = $textBox[1] - $textBox[7] + 20;
    $bg = imagecreate($imgWidth, $imgHeight); // 创建画布
    $color = imagecolorAllocate($bg,250,250,250);   //分配一个灰色
    imagefill($bg,0,0,$color); 
    $white = imagecolorallocate($bg, 0, 0, 0); // 创建白色
    // 若文件编码为 GB2312 请将下行的注释去掉
    // $text = iconv("GB2312", "UTF-8", $text);
    imagettftext($bg, 13, 0, 20, 30, $white, $fontUrl, $text);
    imagejpeg($bg, '/tmp/words.jpg');
    imagedestroy($bg);
    //exit();
    
    //人物
    $path_1 = "/tmp/2.jpg";
    //装备图片
    $path_2 = "/tmp/5.jpg";
    
    $path_3 = "/tmp/words.jpg";
    
    $img1Info = getimagesize($path_1);
    $img2Info = getimagesize($path_2);
    $img3Info = getimagesize($path_3);
    
    $mWidth = 640;
    $mHeight = $img1Info[1] + $img2Info[1] + $img3Info[1];
    
    //将人物和装备图片分别取到两个画布中
    $image_1 = imagecreatefromjpeg($path_1);
    $image_2 = imagecreatefromjpeg($path_2);
    
    $image_4 = imagecreatefromjpeg($path_3);
    ////创建一个和人物图片一样大小的真彩色画布(ps:只有这样才能保证后面copy装备图片的时候不会失真)
    //$image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
    
    $image_3 = imageCreatetruecolor($mWidth,$mHeight);
    
    
    
    //为真彩色画布创建白色背景,再设置为透明
    $color = imagecolorallocate($image_3, 255, 255, 255);
    imagefill($image_3, 0, 0, $color);
    //imageColorTransparent($image_3, $color);
    //首先将人物画布采样copy到真彩色画布中,不会失真
    $xPos = $img1Info[0] > $mWidth ? 0 : ($mWidth - $img1Info[0])/2;
    $tmpWidth = $img1Info[0] > $mWidth ? $mWidth : $img1Info[0];
    imagecopyresampled($image_3,$image_1,$xPos,0,0,0,$tmpWidth,imagesy($image_1),imagesx($image_1),imagesy($image_1));
    //再将装备图片copy到已经具有人物图像的真彩色画布中,同样也不会失真
    
    $xPos = $img2Info[0] > $mWidth ? 0 : ($mWidth - $img2Info[0])/2;
    $tmpWidth = $img2Info[0] > $mWidth ? $mWidth : $img2Info[0];
    imagecopyresampled($image_3,$image_2,$xPos,$img1Info[1],0,0,$tmpWidth,imagesy($image_2),imagesx($image_2),imagesy($image_2));
    
    imagecopyresampled($image_3,$image_4,0,$img1Info[1] + $img2Info[1],0,0,min($mWidth, imagesx($image_4)),imagesy($image_4),imagesx($image_4),imagesy($image_4));
    
    
    //imagecopymerge($image_3,$image_2, 0,$img1Info[1],0,0,imagesx($image_2),imagesy($image_2), 100);
    //imagecopymerge($image_3,$image_4, 0,$img1Info[1] + $img2Info[1],0,0,imagesx($image_2),imagesy($image_2), 100);
    //将画布保存到指定的gif文件
    //imagegif($image_3);
    imagepng($image_3);
    imagedestroy($image_3);
  • 相关阅读:
    shell 远程调用脚本
    Shell 特殊符号
    Centos 7 安全加固命令行
    unix2dos
    这都是啥啥啥
    发现一个识图比较厉害的网站
    Java 中用正则表达式修改 Email 地址
    PHP 7.0 中各种 Hash 速度比较
    利用create_ap软件创建无线AP
    &#129418; 自用Firefox和AdBlockPlus黑白名单
  • 原文地址:https://www.cnblogs.com/bandbandme/p/6071356.html
Copyright © 2020-2023  润新知