使用PHP将3个jpg图像组合在一起

This question already asked but that not solve my issue.

I want to combine 3 JPG images top of each other using with PHP.

I tried this in PNG that's working fine. But when i using JPG image i got black screen like this

enter image description here

This is my code:

<?php
ini_set('max_execution_time', 300);

function get_file($file, $from) {
    if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
}
get_file("background-layer-111.jpg", "https://img.youtube.com/vi/CQElIZzzlpc/hqdefault.jpg");
get_file("icon-layer-222.jpg", "https://img.youtube.com/vi/OK_JCtrrv-c&t=1732s/hqdefault.jpg");
get_file("stars-layer-333.jpg", "https://img.youtube.com/vi/1vYMqjNafww/hqdefault.jpg");

$bgFile = __DIR__ . "/background-layer-111.jpg"; 
$imageFile = __DIR__ . "/icon-layer-222.jpg"; 
$watermarkFile = __DIR__ . "/stars-layer-333.jpg"; 


$x = $y = 76;


$final_img = imagecreatetruecolor($x, $y);


$image_1 = imagecreatefromjpeg($bgFile);
$image_2 = imagecreatefromjpeg($imageFile);
$image_3 = imagecreatefromjpeg($watermarkFile);


imagealphablending($final_img, true);
imagesavealpha($final_img, true);


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();
imagejpeg($final_img);
$watermarkedImg = ob_get_contents(); 
ob_end_clean(); 

header('Content-Type: image/jpeg');
echo $watermarkedImg; 

Note: My code working for only PNG, i want to work with all image format like JPG. I refer this url Link

dongtangze6393
dongtangze6393 我可以使用此代码更改jpeg函数。它可以工作。
一年多之前 回复
drpph80800
drpph80800 与PNG不同,JPG没有alpha通道:JPG中没有任何内容是透明的。您可以使用imagecolortransparent将JPG转换为可混合的东西。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问