溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php把二維碼透明的方法

發(fā)布時(shí)間:2020-09-18 10:58:42 來(lái)源:億速云 閱讀:343 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹php把二維碼透明的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

直接把二維碼圖片背景變透明  

//二維碼內(nèi)容
		$qrcodeContent = '此處存鏈接的話,參數(shù)不宜過長(zhǎng),否則導(dǎo)致生成二維碼時(shí)間太長(zhǎng)!??!';
		//容錯(cuò)級(jí)別
		$errorCorrectionLevel = 'L';
		//生成圖片大小
		$matrixPointSize = 6;
		//生成二維碼圖片
		\QRcode::png($qrcodeContent, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
		$QR = 'qrcode.png';//已經(jīng)生成的原始二維碼圖
		//將二維碼背景變透明
		$resource = imagecreatefrompng($QR);
		@unlink($QR);
		$bgcolor = imagecolorallocate($resource, 255, 255, 255);
		imagefill($resource, 0, 0, $bgcolor);
		imagecolortransparent($resource, $bgcolor);
		imagepng($resource,'qrcode_copy.png');	/*先處理成透明圖再進(jìn)行縮放就不會(huì)出現(xiàn)白黑點(diǎn)情況了?。。。ㄖ辽傩Ч枚嗔耍冗M(jìn)行縮放再處理背景透明就會(huì)出現(xiàn)很多白黑點(diǎn)?。?/
		@imagedestroy($resource);
                //獲取對(duì)應(yīng)游戲海報(bào)二維碼位置
                $qrcode_pos = $pos;//$pos = [x,y]
                //獲取對(duì)應(yīng)游戲海報(bào)二維碼規(guī)格
                $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');//存儲(chǔ)改變大小后的透明二維碼
                $qrcode = 'qrcode_'.$gameType.'.png';
                //將透明背景的二維碼貼到海報(bào)圖指定位置
                $poster_with_qrcode_or_invitedcode_url = waterImage($posterUrl, $qrcode, $qrcode_pos[0], $qrcode_pos[1]);
                @imagedestroy($resource);
                @unlink('qrcode_copy.png');
                @unlink($qrcode);

以上調(diào)用的方法如下:

/**
     * 調(diào)整圖片大小并返回圖片資源
     * @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 傳過來(lái)的參數(shù)是文件名字符串
     * @return mixed 返回值是一個(gè)數(shù)組,包含圖片寬度、高度、創(chuàng)建和輸出的字符串以及擴(kuò)展名
     *  @author zsb
     */
    function getImageInfo($filename){
        if(@!$info = getimagesize($filename)){//getimagesize()函數(shù)可以得到文件信息,
            //還可以判斷圖片是否為真實(shí)的圖片類型,詳細(xì)功能見PHP手冊(cè)
            exit('文件不是真實(shí)圖片');
        }
        $fileInfo['width'] = $info[0];
        $fileInfo['height'] = $info[1];
        $mime = image_type_to_mime_type($info[2]);//info[2]這個(gè)是圖片類型對(duì)應(yīng)的數(shù)字,此函數(shù)可以根據(jù)該數(shù)字返回出文件對(duì)應(yīng)的MIME類型,詳細(xì)見手冊(cè)
        $createFun = str_replace('/', 'createfrom', $mime);//將$mime中的'/'替換成'createfrom',
        //因?yàn)楹筮呉玫絠magecreatefromjpeg/jpg/png 這個(gè)函數(shù),這樣就可以動(dòng)態(tài)使用不同的函數(shù)了
        $outFun = str_replace('/', '', $mime);
        $fileInfo['createFun'] = $createFun;
        $fileInfo['outFun'] = $outFun;
        $fileInfo['ext'] = strtolower(image_type_to_extension($info[2]));//image_type_to_extension()是得到文件后綴名函數(shù)
        return $fileInfo;//返回文件信息
    }
 
    /**
     * 給圖片加水印并返回新圖片地址
     * @param $dstName
     * @param $srcName
     * @param int $x 圖片水印的位置  (0,0)代表左上角
     * @param int $y 圖片水印的位置  (0,0)代表左上角
     * @param string $dest 默認(rèn)保存的位置
     * @param string $pre  默認(rèn)文件名前綴
     * @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);
        //創(chuàng)建畫布
        $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把二維碼透明的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI