Home > Backend Development > PHP Problem > How to make QR code transparent in php

How to make QR code transparent in php

(*-*)浩
Release: 2023-02-27 06:30:02
Original
3430 people have browsed it

How to make QR code transparent in php

Directly make the background of the QR code image transparent (Recommended learning: PHP video tutorial)

//二维码内容
		$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);
Copy after login

The above calling method is as follows:

/**
     * 调整图片大小并返回图片资源
     * @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 = &#39;&#39;;
        $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&#39;s transparent color&#39;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[&#39;red&#39;], $trnprt_color[&#39;green&#39;], $trnprt_color[&#39;blue&#39;]);
                // 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&#39;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(&#39;文件不是真实图片&#39;);
        }
        $fileInfo[&#39;width&#39;] = $info[0];
        $fileInfo[&#39;height&#39;] = $info[1];
        $mime = image_type_to_mime_type($info[2]);//info[2]这个是图片类型对应的数字,此函数可以根据该数字返回出文件对应的MIME类型,详细见手册
        $createFun = str_replace(&#39;/&#39;, &#39;createfrom&#39;, $mime);//将$mime中的&#39;/&#39;替换成&#39;createfrom&#39;,
        //因为后边要用到imagecreatefromjpeg/jpg/png 这个函数,这样就可以动态使用不同的函数了
        $outFun = str_replace(&#39;/&#39;, &#39;&#39;, $mime);
        $fileInfo[&#39;createFun&#39;] = $createFun;
        $fileInfo[&#39;outFun&#39;] = $outFun;
        $fileInfo[&#39;ext&#39;] = 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 = &#39;images/waterImage&#39;, $pre = &#39;waterImage_&#39;, $pct = 100){
        //获取图片信息
        $dstInfo = getImageInfo($dstName);
        $srcInfo = getImageInfo($srcName);
        //创建画布
        $dst_im = $dstInfo[&#39;createFun&#39;]($dstName);
        $src_im = $srcInfo[&#39;createFun&#39;]($srcName);
        $src_width = $srcInfo[&#39;width&#39;];
        $src_height = $srcInfo[&#39;height&#39;];
        $dst_width = $dstInfo[&#39;width&#39;];
        $dst_height = $dstInfo[&#39;height&#39;];
 
        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[&#39;ext&#39;];
        $destination = $dest ? $dest.&#39;/&#39;.$dstName : $dstName;
        $dstInfo[&#39;outFun&#39;]($dst_im,$destination);
        imagedestroy($src_im);
        imagedestroy($dst_im);
        return $destination;
    }
Copy after login

The above is the detailed content of How to make QR code transparent in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template