zoukankan      html  css  js  c++  java
  • php 图片添加文字水印 以及 图片合成(微信快码传播)

    1、图片添加文字水印:

    $bigImgPath = 'backgroud.png';
        $img = imagecreatefromstring(file_get_contents($bigImgPath));
    
        $font = 'msyhl.ttc';//字体
        $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB
    
        $fontSize = 20;   //字体大小
        $circleSize = 60; //旋转角度
        $left = 50;      //左边距
        $top = 150;       //顶边距
    
        imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, 'Rhythmk| 坤');
    
        list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
        switch ($bgType) {
            case 1: //gif
                header('Content-Type:image/gif');
                imagegif($img);
                break;
            case 2: //jpg
                header('Content-Type:image/jpg');
                imagejpeg($img);
                break;
            case 3: //jpg
                header('Content-Type:image/png');
                imagepng($img);
                break;
            default:
                break;
        }
        imagedestroy($img);
    

      效果:

        

    2、图片合成

            $bigImgPath = 'backgroud.png';
            $qCodePath = 'qcode.png';
    
            $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
            $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
    
            list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
            // imagecopymerge使用注解
            imagecopymerge($bigImg, $qCodeImg, 200, 300, 0, 0, $qCodeWidth, $qCodeHight, 100);
    
            list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
    
    
            switch ($bigType) {
                case 1: //gif
                    header('Content-Type:image/gif');
                    imagegif($bigImg);
                    break;
                case 2: //jpg
                    header('Content-Type:image/jpg');
                    imagejpeg($bigImg);
                    break;
                case 3: //jpg
                    header('Content-Type:image/png');
                    imagepng($bigImg);
                    break;
                default:
                    # code...
                    break;
            }
    
            imagedestroy($bigImg);
            imagedestroy($qcodeImg);
    

    函数注解:

    imagecopymerge()

    imagecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 TRUE ,否则返回 FALSE 。

    语法:

    bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )

    参数说明:

    dst_im 目标图像
    src_im 被拷贝的源图像
    dst_x 目标图像开始 x 坐标
    dst_y 目标图像开始 y 坐标,x,y同为 0 则从左上角开始
    src_x 拷贝图像开始 x 坐标
    src_y 拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
    src_w (从 src_x 开始)拷贝的宽度
    src_h (从 src_y 开始)拷贝的高度
    pct 图像合并程度,取值 0-100 ,当 pct=0 时,实际上什么也没做,反之完全合并。

     

     效果图:

  • 相关阅读:
    Python3之random模块常用方法
    Go语言学习笔记(九)之数组
    Go语言学习笔记之简单的几个排序
    Go语言学习笔记(八)
    Python3之logging模块
    Go语言学习笔记(六)
    123. Best Time to Buy and Sell Stock III(js)
    122. Best Time to Buy and Sell Stock II(js)
    121. Best Time to Buy and Sell Stock(js)
    120. Triangle(js)
  • 原文地址:https://www.cnblogs.com/rhythmK/p/5426050.html
Copyright © 2011-2022 走看看