dpzjl68484 2019-01-29 04:33
浏览 80

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况