PHP使用GD库生成文件

2020-08-14 11:24:10 浏览数 (1)

代码语言:javascript复制
<?php
$xgcs_result = $this->get_xgcs_result($key);
if (!empty($xgcs_result) && isset($xgcs_result['type']) && isset($xgcs_result['content']) && isset($xgcs_result['image']) && $name) {
    $tip_string = $xgcs_result['content'];
    $cat = date("Ymd", time());
    $path = SITEPATH . 'uploads/xinggeceshi/' . $cat;//生成的图片路径
    if (!file_exists($path)) {
        mkdir($path, 0755, true);
    }
    $file_name = sha1(microtime() . $_SERVER['REMOTE_ADDR'] . "sdgjasdruisodgjketidfg" . rand(1, 10000000) . rand(1, 10000000)) . '.png';//生成的图片名称
    $pic_path = $path . '/' . $file_name;//生成图片完整路径
    $pic_width = 420;//生成图片的宽高
    $pic_height = 700;

    $local_image = SITEPATH . "static/activity/img/xinggeceshi/" . $xgcs_result['image'];
    $src_image = imagecreatefrompng($local_image);
    $src_width = imagesx($src_image);
    $src_height = imagesy($src_image);
    //创建图片
    //$dest_image = imagecreate($pic_width, $pic_height);
    $dest_image = @imagecreatetruecolor($pic_width, $pic_height) or die('Cannot Initialize new GD image stream');
    //创建颜色
    $result = imagecopy($dest_image, $src_image, 0, 0, 0, 0, $pic_width, $pic_height);
    $white = imagecolorallocate($dest_image, 255, 255, 255);
    imagefilledrectangle($dest_image, 0, $src_height, $pic_width, $pic_height, $white);
    $text_color = imagecolorallocate($dest_image, 102, 102, 102);
    $tip_length = mb_strlen($tip_string);
    $tip_num = 20;
    $row = ceil($tip_length / $tip_num);
    $font_size = 14;//文字大小
    $fontPath = ROOTPATH . 'sitedata/fonts/font2.ttf';//楷体
    $heitiPath = ROOTPATH . 'sitedata/fonts/simhei.ttf';//黑体
    //var_dump($tip_string);
    $name_length_ext = strlen($name);
    $name = mb_convert_encoding($name, "UTF-8", "GBK");
    $name_color = imagecolorallocate($dest_image, 77, 61, 68);
    $name_length = strlen($name);

    $name_right = mb_convert_encoding("的测试结果是:", "UTF-8", "GBK");
    $name_right_color = imagecolorallocate($dest_image, 151, 153, 162);
    if ($name_length > 12) {
        imagettftext($dest_image, 16, 0, 18, $src_height   50, $name_color, $heitiPath, $name);
        imagettftext($dest_image, $font_size, 0, 18   11 * $name_length_ext, $src_height   50, $name_right_color, $fontPath, $name_right);
    } else {
        imagettftext($dest_image, 28, 0, 18, $src_height   50, $name_color, $heitiPath, $name);
        imagettftext($dest_image, $font_size, 0, 18   20 * $name_length_ext, $src_height   50, $name_right_color, $fontPath, $name_right);
    }
    $type = mb_convert_encoding($xgcs_result['type'], "UTF-8", "GBK");
    $type_length = strlen($type);
    $type_color = imagecolorallocate($dest_image, 255, 87, 80);
    $type_right = mb_convert_encoding("型血", "UTF-8", "GBK");
    $type_right_color = imagecolorallocate($dest_image, 255, 87, 80);
    imagettftext($dest_image, 20, 0, 18, $src_height   90, $type_color, $heitiPath, $type);
    imagettftext($dest_image, $font_size, 0, 18   18 * $type_length, $src_height   90, $type_right_color, $fontPath, $type_right);
    for ($i = 0; $i < $row; $i  ) {
        $authOne = mb_substr($tip_string, $i * $tip_num, $tip_num, 'GBK');
        $authOne = mb_convert_encoding($authOne, "UTF-8", "GBK");
        imagettftext($dest_image, $font_size, 0, 18, $src_height   120   30 * $i, $text_color, $fontPath, $authOne);
    }

    if (imagepng($dest_image, $pic_path)) {
        imagedestroy($dest_image);
        $pic_url = NET_URL . 'uploads/xinggeceshi/' . $cat . '/' . $file_name;
        return $pic_url;
    } else {
        return FALSE;
    }
}

以上代码是在做一次活动海报生成时使用的。

0 人点赞