PHP realizes picture merging in WeChat-Jiugongge pictures

WBOY
Release: 2016-07-29 09:16:00
Original
2018 people have browsed it
<?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.

Related labels:
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