Подтвердить что ты не робот

Объедините 2-3 прозрачных изображения PNG поверх друг друга с помощью PHP

Я работаю над пользовательской системой аватаров для проекта, но я никогда не делал многого с частью изображения PHP. Я предполагаю, что мне нужно каким-то образом использовать GD, но я не знаю, с чего начать.

В принципе, есть куча готовых прозрачных изображений PNG. Пользователи могут выбрать 2-3 из них, чтобы настроить их аватар, и я хочу, чтобы иметь возможность принимать эти изображения и сделать из них одно изображение, которое будет храниться в папке.

4b9b3361

Ответ 1

$image_1 = imagecreatefrompng('image_1.png');
$image_2 = imagecreatefrompng('image_2.png');
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);
imagepng($image_1, 'image_3.png');

Ответ 2

Это помогло мне создать PNG-изображение из трех других PNG файлов, чтобы создать водяной знак с фоном. Надеюсь, это поможет кому-то другому.


$bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93

Background Layer

$imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76

Icon Image Layer

$watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133 (Белые звезды)
Watermark

<?php
// Download the image files if we don't have them
function get_file($file, $from) {
    if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
}
get_file("background-layer-1.png", "http://i.imgur.com/6pgf3WK.png");
get_file("icon-layer-2.png", "http://i.imgur.com/0sJt52z.png");
get_file("stars-layer-3.png", "http://i.imgur.com/1Tvlokk.png");

$bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93
$imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76
$watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133

// We want our final image to be 76x76 size
$x = $y = 76;

// dimensions of the final image
$final_img = imagecreatetruecolor($x, $y);

// Create our image resources from the files
$image_1 = imagecreatefrompng($bgFile);
$image_2 = imagecreatefrompng($imageFile);
$image_3 = imagecreatefrompng($watermarkFile);

// Enable blend mode and save full alpha channel
imagealphablending($final_img, true);
imagesavealpha($final_img, true);

// Copy our image onto our $final_img
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_3, 0, 0, 0, 0, $x, $y);

ob_start();
imagepng($final_img);
$watermarkedImg = ob_get_contents(); // Capture the output
ob_end_clean(); // Clear the output buffer

header('Content-Type: image/png');
echo $watermarkedImg; // outputs: `http://i.imgur.com/f7UWKA8.png`

Выходы:

Result

Ответ 3

Определенное использование библиотеки GD.

<?php

$final_img = imagecreate($x, $y); // where x and y are the dimensions of the final image

$image_1 = imagecreatefrompng('image_1.png');
$image_2 = imagecreatefrompng('image_2.png');
$image_3 = imagecreatefrompng('image_3.png');
imagecopy($image_1, $final_img, 0, 0, 0, 0, $x, $y);
imagecopy($image_2, $final_img, 0, 0, 0, 0, $x, $y);
imagecopy($image_3, $final_img, 0, 0, 0, 0, $x, $y);

imagealphablending($final_img, false);
imagesavealpha($final_img, true);
if($output_to_browser){

header('Content-Type: image/png');
imagepng($final_img);

}else{
// output to file

imagepng($final_img, 'final_img.png');

}

?>

Ответ 4

Также может быть сделано таким образом. Надеюсь, это будет полезно для будущих посетителей.

$base = imagecreatefrompng('your base image path');
//logo is transparent: in this case stackoverflow logo
$logo = imagecreatefrompng("path for image with transparent background");

//Adjust paramerters according to your image
imagecopymerge_alpha($base, $logo, 60, 60, 0, 0, 300, 200, 100);

header('Content-Type: image/png');
imagepng($base);

//@see: http://php.net/manual/en/function.imagecopymerge.php for below function in first comment
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
        // creating a cut resource 
        $cut = imagecreatetruecolor($src_w, $src_h); 

        // copying relevant section from background to the cut resource 
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

        // copying relevant section from watermark to the cut resource 
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 

        // insert cut resource to destination image 
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
    } 

Рабочий пример: Это фоновое изображение введите описание изображения здесь
Это логотип stackoverflow.
введите описание изображения здесь
Это комбинированный результат.
введите описание изображения здесь

Ответ 5

Что вы хотите использовать, это утилиты PHP ImageMagick.

В частности, команда CombineImages.