0

0

php怎么把二维码透明

(*-*)浩

(*-*)浩

发布时间:2019-10-17 10:36:29

|

3752人浏览过

|

来源于php中文网

原创

php怎么把二维码透明

直接把二维码图片背景变透明   (推荐学习:PHP视频教程

//二维码内容
		$qrcodeContent = '此处存链接的话,参数不宜过长,否则导致生成二维码时间太长!!!';
		//容错级别
		$errorCorrectionLevel = 'L';
		//生成图片大小
		$matrixPointSize = 6;
		//生成二维码图片
		\QRcode::png($qrcodeContent, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
		$QR = 'qrcode.png';//已经生成的原始二维码图
		//将二维码背景变透明
		$resource = imagecreatefrompng($QR);
		@unlink($QR);
		$bgcolor = imagecolorallocate($resource, 255, 255, 255);
		imagefill($resource, 0, 0, $bgcolor);
		imagecolortransparent($resource, $bgcolor);
		imagepng($resource,'qrcode_copy.png');	/*先处理成透明图再进行缩放就不会出现白黑点情况了!!!(至少效果好多了,而先进行缩放再处理背景透明就会出现很多白黑点!)*/
		@imagedestroy($resource);
                //获取对应游戏海报二维码位置
                $qrcode_pos = $pos;//$pos = [x,y]
                //获取对应游戏海报二维码规格
                $qrcode_spec = $spec;//$spec = [w,h]
                $resource = smart_resize_image('qrcode_copy.png', $qrcode_spec[0], $qrcode_spec[1], true);
                imagepng($resource,'qrcode_'.$gameType.'.png');//存储改变大小后的透明二维码
                $qrcode = 'qrcode_'.$gameType.'.png';
                //将透明背景的二维码贴到海报图指定位置
                $poster_with_qrcode_or_invitedcode_url = waterImage($posterUrl, $qrcode, $qrcode_pos[0], $qrcode_pos[1]);
                @imagedestroy($resource);
                @unlink('qrcode_copy.png');
                @unlink($qrcode);

以上调用的方法如下:

Winston AI
Winston AI

强大的AI内容检测解决方案

下载
/**
     * 调整图片大小并返回图片资源
     * @param $file
     * @param int $width
     * @param int $height
     * @param bool $proportional
     * @return bool|resource
     * @author zsb
     */
    function smart_resize_image($file, $width = 0, $height = 0, $proportional = false)
    {
        if ( $height <= 0 && $width <= 0 ) {
            return false;
        }
        $info = getimagesize($file);
        $image = '';
        $final_width = 0;
        $final_height = 0;
        list($width_old, $height_old) = $info;
        if ($proportional) {
            if ($width == 0) {
                $factor = $height/$height_old;
            }elseif ($height == 0) {
                $factor = $width/$width_old;
            }else {
                $factor = min ( $width / $width_old, $height / $height_old);
            }
            $final_width = round ($width_old * $factor);
            $final_height = round ($height_old * $factor);
        }else {
            $final_width = ( $width <= 0 ) ? $width_old : $width;
            $final_height = ( $height <= 0 ) ? $height_old : $height;
        }
        switch ($info[2]) {
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif($file);
                break;
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg($file);
                break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng($file);
                break;
            default:
                return false;
        }
        $image_resized = imagecreatetruecolor( $final_width, $final_height );
        if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
            $trnprt_indx = imagecolortransparent($image);
            // If we have a specific transparent color
            if ($trnprt_indx >= 0) {
                // Get the original image's transparent color's RGB values
                $trnprt_color  = imagecolorsforindex($image, $trnprt_indx);
                // Allocate the same color in the new image resource
                $trnprt_indx  = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                // Completely fill the background of the new image with allocated color.
                imagefill($image_resized, 0, 0, $trnprt_indx);
                // Set the background color for new image to transparent
                imagecolortransparent($image_resized, $trnprt_indx);
            }
            // Always make a transparent background color for PNGs that don't have one allocated already
            elseif ($info[2] == IMAGETYPE_PNG) {
                // Turn off transparency blending (temporarily)
                imagealphablending($image_resized, false);
                // Create a new transparent color for image
                $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
                // Completely fill the background of the new image with allocated color.
                imagefill($image_resized, 0, 0, $color);
                // Restore transparency blending
                imagesavealpha($image_resized, true);
            }
        }
 
        imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
 
        return $image_resized;
    }
 
    /**
     * 获取图片信息
     * @param $filename 传过来的参数是文件名字符串
     * @return mixed 返回值是一个数组,包含图片宽度、高度、创建和输出的字符串以及扩展名
     *  @author zsb
     */
    function getImageInfo($filename){
        if(@!$info = getimagesize($filename)){//getimagesize()函数可以得到文件信息,
            //还可以判断图片是否为真实的图片类型,详细功能见PHP手册
            exit('文件不是真实图片');
        }
        $fileInfo['width'] = $info[0];
        $fileInfo['height'] = $info[1];
        $mime = image_type_to_mime_type($info[2]);//info[2]这个是图片类型对应的数字,此函数可以根据该数字返回出文件对应的MIME类型,详细见手册
        $createFun = str_replace('/', 'createfrom', $mime);//将$mime中的'/'替换成'createfrom',
        //因为后边要用到imagecreatefromjpeg/jpg/png 这个函数,这样就可以动态使用不同的函数了
        $outFun = str_replace('/', '', $mime);
        $fileInfo['createFun'] = $createFun;
        $fileInfo['outFun'] = $outFun;
        $fileInfo['ext'] = strtolower(image_type_to_extension($info[2]));//image_type_to_extension()是得到文件后缀名函数
        return $fileInfo;//返回文件信息
    }
 
    /**
     * 给图片加水印并返回新图片地址
     * @param $dstName
     * @param $srcName
     * @param int $x 图片水印的位置  (0,0)代表左上角
     * @param int $y 图片水印的位置  (0,0)代表左上角
     * @param string $dest 默认保存的位置
     * @param string $pre  默认文件名前缀
     * @param int $pct  图片水印的透明度
     * @return string
     * @author zsb
     */
    function waterImage($dstName, $srcName, $x = 0, $y = 0, $dest = 'images/waterImage', $pre = 'waterImage_', $pct = 100){
        //获取图片信息
        $dstInfo = getImageInfo($dstName);
        $srcInfo = getImageInfo($srcName);
        //创建画布
        $dst_im = $dstInfo['createFun']($dstName);
        $src_im = $srcInfo['createFun']($srcName);
        $src_width = $srcInfo['width'];
        $src_height = $srcInfo['height'];
        $dst_width = $dstInfo['width'];
        $dst_height = $dstInfo['height'];
 
        imagecopymerge($dst_im, $src_im, $x, $y, 0, 0, $src_width, $src_height, $pct);
 
        if($dest && !file_exists($dest)){
            mkdir($dest,0777,true);
        }
 
        //$randNum = mt_rand(100000,999999);
        $dstName = "$pre".$dstInfo['ext'];
        $destination = $dest ? $dest.'/'.$dstName : $dstName;
        $dstInfo['outFun']($dst_im,$destination);
        imagedestroy($src_im);
        imagedestroy($dst_im);
        return $destination;
    }

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
javascript void运算符
javascript void运算符

void是一元运算符,执行右侧表达式但始终返回undefined;用于丢弃返回值、阻止a标签跳转、IIFE忽略结果、动态导入不取Promise、安全获取undefined。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1

2025.12.29

vscode的界面字体大小调整
vscode的界面字体大小调整

调整VSCode界面字体大小可通过设置编辑器或整体UI缩放实现;2.修改"Editor:FontSize"改变代码字体;3.设置"Window:ZoomLevel"调整整体界面字体;4.使用Ctrl+滚轮快捷键临时缩放。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1

2025.12.29

VSCode的注释快捷键
VSCode的注释快捷键

单行注释快捷键为Ctrl+/(Windows/Linux)或Cmd+/(macOS),块注释使用Shift+Alt+A(Windows/Linux)或Shift+Option+A(macOS),VSCode会根据语言类型自动匹配语法,如JavaScript用//,Python用#,C++用//,若快捷键无效需检查语言扩展或插件冲突。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1

2025.12.29

Golang 命令行工具(CLI)开发实战
Golang 命令行工具(CLI)开发实战

本专题系统讲解 Golang 在命令行工具(CLI)开发中的实战应用,内容涵盖参数解析、子命令设计、配置文件读取、日志输出、错误处理、跨平台编译以及常用CLI库(如 Cobra、Viper)的使用方法。通过完整案例,帮助学习者掌握 使用 Go 构建专业级命令行工具与开发辅助程序的能力。

4

2025.12.29

ip地址修改教程大全
ip地址修改教程大全

本专题整合了ip地址修改教程大全,阅读下面的文章自行寻找合适的解决教程。

165

2025.12.26

压缩文件加密教程汇总
压缩文件加密教程汇总

本专题整合了压缩文件加密教程,阅读专题下面的文章了解更多详细教程。

56

2025.12.26

wifi无ip分配
wifi无ip分配

本专题整合了wifi无ip分配相关教程,阅读专题下面的文章了解更多详细教程。

108

2025.12.26

漫蛙漫画入口网址
漫蛙漫画入口网址

本专题整合了漫蛙入口网址大全,阅读下面的文章领取更多入口。

356

2025.12.26

b站看视频入口合集
b站看视频入口合集

本专题整合了b站哔哩哔哩相关入口合集,阅读下面的文章查看更多入口。

703

2025.12.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 8万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号