search
HomeBackend DevelopmentPHP TutorialPHP realizes picture merging in WeChat-Jiugongge pictures

<?php /**
     * 根据图片数组  拼接成九宫格式拼图
     * @param  array   $pic_list [带拼成的图片数组]
     * @param  integer $bg_w     [背景图片宽度]
     * @param  integer $bg_h     [背景图片高度]
     * @param  string  $format   [阿里云图片获取参数]
     * @return [type]            [返回一个拼接好的图片(路径)]
     */
    function mosaicGroupAvatar($pic_list = array(),$bg_w = 500,$bg_h=500,$format="@0e_320w_320h_0c_0i_1o_90Q_1x.jpg"){

        $pic_list       = array(
            &#39;http://image.microinvestment.cn/avatar/ios_13409C7D-0499-40D7-8EF8-242636833E1C_1432009249.534474.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/1d11f7cf-d345-4b66-809c-df4f9fb44900.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/241e049f-791b-49e0-a041-7b95720a4eb2.jpg&#39;,
            &#39;http://image.microinvestment.cn/avatar/6737e039-167e-4c4f-8915-616d0fe6c471.jpg&#39;,
            &#39;http://image.microinvestment.cn/avatar/1d11f7cf-d345-4b66-809c-df4f9fb44900.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/ios_192DA9F1-47C2-4593-8390-731DD8A6E4A6_1432038578.595966.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/ios_0C9B935A-8ACA-43A7-A5F9-70FCE8032499_1434365804.095018.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/default.png&#39;,
            &#39;http://image.microinvestment.cn/avatar/14f7ebcb-f522-4cb0-b9c2-adca25e536f2.jpg&#39;,
        );
   
        $pic_list    = array_slice($pic_list, 0, 9); // 只操作前9个图片       

        $background = imagecreatetruecolor($bg_w,$bg_h); // 背景图片  

        //int imagecolorallocate ( resource $image , int $red , int $green , int $blue ) 为一幅图像分配颜色
        $color   = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
        imagefill($background, 0, 0, $color);           //区域填充  
        imageColorTransparent($background, $color);     // 将某个颜色定义为透明色

        $pic_count  = count($pic_list);
        $lineArr    = array();  // 需要换行的位置
        $space_x    = 3;
        $space_y    = 3;
        $line_x  = 0;
        switch($pic_count) {
            case 1: // 正中间
                $start_x = intval($bg_w/4);  // 开始位置X
                $start_y = intval($bg_h/4);  // 开始位置Y
                $pic_w   = intval($bg_w/2); // 宽度
                $pic_h   = intval($bg_h/2); // 高度
                break;
            case 2: // 中间位置并排
                $start_x = 2;
                $start_y = intval($bg_h/4) + 3;
                $pic_w   = intval($bg_w/2) - 5;
                $pic_h   = intval($bg_h/2) - 5;
                $space_x = 5;
                break;
            case 3:
                $start_x = 124;   // 开始位置X
                $start_y = 5;    // 开始位置Y
                $pic_w   = intval($bg_w/2) - 5; // 宽度
                $pic_h   = intval($bg_h/2) - 5; // 高度
                $lineArr = array(2);
                $line_x  = 4;
                break;
            case 4:
                $start_x = 4;    // 开始位置X
                $start_y = 5;    // 开始位置Y
                $pic_w   = intval($bg_w/2) - 5; // 宽度
                $pic_h   = intval($bg_h/2) - 5; // 高度
                $lineArr = array(3);
                $line_x  = 4;
                break;
            case 5:
                $start_x = 85.5;   // 开始位置X
                $start_y = 85.5;   // 开始位置Y
                $pic_w   = intval($bg_w/3) - 5; // 宽度
                $pic_h   = intval($bg_h/3) - 5; // 高度
                $lineArr = array(3);
                $line_x  = 5;
                break;
            case 6:
                $start_x = 5;    // 开始位置X
                $start_y = 85.5;   // 开始位置Y
                $pic_w   = intval($bg_w/3) - 5; // 宽度
                $pic_h   = intval($bg_h/3) - 5; // 高度
                $lineArr = array(4);
                $line_x  = 5;
                break;
            case 7:
                $start_x = 166.5;   // 开始位置X
                $start_y = 5;    // 开始位置Y
                $pic_w   = intval($bg_w/3) - 5; // 宽度
                $pic_h   = intval($bg_h/3) - 5; // 高度
                $lineArr = array(2,5);
                $line_x  = 5;
                break;
            case 8:
                $start_x = 80.5;   // 开始位置X
                $start_y = 5;    // 开始位置Y
                $pic_w   = intval($bg_w/3) - 5; // 宽度
                $pic_h   = intval($bg_h/3) - 5; // 高度
                $lineArr = array(3,6);
                $line_x  = 5;
                break;
            case 9:
                $start_x = 5;    // 开始位置X
                $start_y = 5;    // 开始位置Y
                $pic_w   = intval($bg_w/3) - 5; // 宽度
                $pic_h   = intval($bg_h/3) - 5; // 高度
                $lineArr = array(4,7);
                $line_x  = 5;
                break;
        }

        foreach( $pic_list as $k=>$pic_path ) {
            $kk = $k + 1;
            if ( in_array($kk, $lineArr) ) {
                $start_x    = $line_x;
                $start_y    = $start_y + $pic_h + $space_y;
            }

            $resource   = imagecreatefromjpeg($pic_path.$format);

            // $start_x,$start_y copy图片在背景中的位置
            // 0,0 被copy图片的位置   $pic_w,$pic_h copy后的高度和宽度
            imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource)); // 最后两个参数为原始图片宽度和高度,倒数两个参数为copy时的图片宽度和高度
            $start_x    = $start_x + $pic_w + $space_x;
        }

        $fname= date('YmdHis').rand(100,999);
  
        $imagePath = './Public/Temp/'.$fname.'.jpg';
   
        // header("Content-type: image/jpg");
        // imagejpeg($background);die;

        // 保存图像为 $imagePath.'$fname'.'.jpg'
        $res = imagejpeg($background,$imagePath);  // imagejpeg($background,'./public/$uid_.$group.jpg');
        if (false === $res) {
            return false;
        } 

        // 释放内存
        imagedestroy($background);
        
        return $imagePath;   
    } 
?>

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the PHP implementation of image merging in WeChat - Jiugongge pictures, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools