PHP implements image merging function (generates group logo images based on group user avatars)


/* Image merge (generate group logo image)
        $pic_arr = array(
            'https://www.test.com/1.png',
            'https://www.test.com/2.png',
            'https: //www.test.com/3.png',
            'https://www.test.com/4.png',
        );
*/
function meshImg($pic_arr=[],$output_pic="./Uploads/ clublogo/club_id.png"){     if(empty($pic_arr)) return false;

    $pic_list    = array_slice($pic_arr, 0, 6); // 只操作前9个图片
    if($pic_list[0] == ''){
        $pic_list[0] = './Uploads/clublogo/club_logo_background.png';
        unset($pic_list[1]);
        unset($pic_list[2]);
        unset($pic_list[3]);
        unset($pic_list[4]);
        unset($pic_list[5]);
    }else{
        if($pic_list[1] != ''){
            if($pic_list[2] == ''){
                $pic_list[2] = './Uploads/clublogo/club_logo_background.png';
            }
            if($pic_list[3] == ''){
                $pic_list[3] = './Uploads/clublogo/club_logo_background.png';
            }
            if($pic_list[4] == ''){                 $pic_list[4] = './Uploads/clublogo/club_logo_background.png';             }             if($pic_list[5] == ''){                 $pic_list[5] = './Uploads/clublogo/club_logo_background.png';             }         }else{             unset($pic_list[1]);             unset($pic_list[2]);             unset($pic_list[3]);             unset($pic_list[4] ]);             unset($pic_list[5]);         }     }     $bg_w = ​​150; // background image width     $bg_h = 150; // background image height














    $background = imagecreatetruecolor($bg_w,$bg_h); // Background image
    $color = imagecolorallocate($background, 202, 201, 201); // Create a white background for the true color canvas, and set it to be transparent
    imagefill($background, 0, 0, $color);
    imageColorTransparent($background, $color);

    $pic_count  = count($pic_list);
    foreach( $pic_list as $k=>$pic_path ) {
        if($pic_count != 1) {
            if ($k == 0) {
                $start_x = 1;
                $start_y = 1;
                $pic_w = 98;
                $pic_h = 98;
            }
            if ($k == 1) {
                $start_x = 101;
                $start_y = 1;
                $pic_w = 48;
                $pic_h = 48;
            }
            if ($k == 2) {
                $start_x = 101;
                $start_y = 51;
                $pic_w = 48;
                $pic_h = 48;
            }
            if ($k == 3) {
                $start_x = 1;
                $start_y = 101;
                $pic_w = 48;
                $pic_h = 48;
            }
            if ($k == 4) {
                $start_x = 51;
                $start_y = 101;
                $pic_w = 48;
                $pic_h = 48;
            }
            if ($k == 5) {
                $start_x = 101;
                $start_y = 101;
                $pic_w = 48;
                $pic_h = 48;
            }
        }else{
            $start_x = 0;
            $start_y = 0;
            $pic_w = 150;
            $pic_h = 150;
        }
        $pathInfo    = pathinfo($pic_path);
        switch( strtolower($pathInfo['extension']) ) {
            case 'jpg':
            case 'jpeg':
                $imagecreatefromjpeg    = 'imagecreatefromjpeg';
                break;
            case 'png':
                $imagecreatefromjpeg    = 'imagecreatefrompng';
                break;
            case 'gif':
            default:
                $imagecreatefromjpeg    = 'imagecreatefromstring';
                $pic_path    = file_get_contents($pic_path);
                break;
        }
        $resource = $imagecreatefromjpeg($pic_path);
        // $start_x, $start_y position of the copied image in the background
        // 0,0 position of the copied image
        // $pic_w, $pic_h height and width after copying
        imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource)); // The last two parameters are the original image width and height, The width and height of the picture when the last two parameters are copy
    }

    header("Content-type: image/jpg");
    imagejpeg($background);
    imagegif($background, $output_pic);

}

Guess you like

Origin blog.csdn.net/happyzhlb/article/details/126680291