zoukankan      html  css  js  c++  java
  • 将小程序的logo换成属于自己的头像

       $logo = file_get_contents(IMG_URL.$logo); //根据头像的路径
       $logo =  yuanImg1($logo); //将头像改为圆形
       $data = file_get_contents(IMG_URL.$img_url);//小程序的路径。如果没有保存,可以直接用微信小程序返回的资源
       $data = qrcodeWithLogo($data,$logo);
    /**
     * 在二维码的中间区域镶嵌图片
     * @param $QR 二维码数据流。比如file_get_contents(imageurl)返回的东东,或者微信给返回的东东
     * @param $logo 中间显示图片的数据流。比如file_get_contents(imageurl)返回的东东
     * @return  返回图片数据流
     */
    function qrcodeWithLogo($QR,$logo){
        $QR   = imagecreatefromstring ($QR);
        $logo = imagecreatefromstring ($logo);
        $QR_width    = imagesx ( $QR );//二维码图片宽度
        $QR_height   = imagesy ( $QR );//二维码图片高度
        $logo_width  = imagesx ( $logo );//logo图片宽度
        $logo_height = imagesy ( $logo );//logo图片高度
        $logo_qr_width  = $QR_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
        $scale  = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height / $scale;//组合之后logo的高度
        $from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
        /**
         * 重新组合图片并调整大小
         * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
         */
        imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
        /**
         * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
         * 并且去掉缓存区函数
         */
        //获取输出缓存,否则imagepng会把图片输出到浏览器
        ob_start();
        imagepng ( $QR );
        imagedestroy($QR);
        imagedestroy($logo);
        $contents =  ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    /**
     * 剪切图片为圆形
     * @param  $picture 图片数据流 比如file_get_contents(imageurl)返回的东东
     * @return 图片数据流
     */
    function yuanImg1($picture) {
        $src_img = imagecreatefromstring($picture); //创建一个图像资源从字符串中的图像流。
        $w   = imagesx($src_img);
        $h   = imagesy($src_img);
        $w1   = min($w, $h);//截取的圆形图片的直径取图片的宽度、高度两者的最小值
        $h1   = $w1;
        $img = imagecreatetruecolor($w1, $h1); //创建一幅空白图像
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w1 / 2; //圆半径
        $y_x = $w / 2; //圆心X坐标 // 圆心位置取图片原宽度、高度的一半
        $y_y = $h / 2; //圆心Y坐标
        for ($x = 0; $x < $w;$x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);//取得某像素的颜色索引值
                if (((($x - $y_x) * ($x - $y_x) + ($y -  $y_y) * ($y -  $y_y)) < ($r * $r))) {
                    imagesetpixel($img, $x-$r, $y, $rgbColor);//$x一定要减去半径。不然头像只有一半
                }
            }
        }
    
        /**
         * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
         * 并且去掉缓存区函数
         */
        //获取输出缓存,否则imagepng会把图片输出到浏览器
        ob_start();
        imagepng ( $img );
        imagedestroy($img);
        $contents =  ob_get_contents();
        ob_end_clean();
        return $contents;
    }
  • 相关阅读:
    PHP_Code_challenge(代码审计)
    超全局变量$GLOBALS
    upload-labs(文件上传)
    CTF bugku 论剑场 web20
    多次Printf()是否使用用同一栈帧的参数?
    (转载于度盘)小迪安全渗透-学习讲义
    SQL数据库操作练习(2)
    .htaccess绕过
    PHP序列化思考(9.14已修改)
    SQL数据库操作练习(1)
  • 原文地址:https://www.cnblogs.com/f-rt/p/10044974.html
Copyright © 2011-2022 走看看