Home Backend Development PHP Tutorial PHP realizes picture merging in WeChat-Jiugongge pictures

PHP realizes picture merging in WeChat-Jiugongge pictures

Jul 29, 2016 am 09:16 AM
array intval start

<?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;   
    } 
?>
Copy after login

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 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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 &gt; /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

What to do if node start reports an error What to do if node start reports an error Dec 29, 2022 pm 01:55 PM

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Introduction to how to use the PHP array_change_key_case() function Introduction to how to use the PHP array_change_key_case() function Jun 27, 2023 am 10:43 AM

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke

What are the common causes of ArrayIndexOutOfBoundsException in Java? What are the common causes of ArrayIndexOutOfBoundsException in Java? Jun 24, 2023 pm 10:39 PM

Java is a very powerful programming language that is widely used in various development fields. However, during Java programming, developers often encounter ArrayIndexOutOfBoundsException exceptions. So, what are the common causes of this anomaly? ArrayIndexOutOfBoundsException is a common runtime exception in Java. It means that when accessing data, the array subscript exceeds the range of the array. Common reasons include

See all articles