php - 使用 PHP 将 3 张 jpg 图像相互叠加
问题描述
这个问题已经问过了,但这并不能解决我的问题。
我想使用 with 将combine
3JPG
张图像相互叠加PHP
。
我试过了PNG
,效果很好。但是当我使用JPG
图像时,我得到了这样的黑屏
这是我的代码:
<?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;
注意:我的代码仅适用于
PNG
,我想使用所有图像格式,例如JPG
. 我参考这个网址 链接
解决方案
推荐阅读
- regex - PowerShell -match 与 -simplematch
- angular - 使用 Postcss 进行 Webpack 编译失败,因为在 node_modules 的库中找不到 scss 文件
- c - 从文件转换文本
- html - 即使跨度和 li 标签的高度相同,我的 li 标签内的 span 标签也没有正确对齐
- oledb - 我如何使用加入?
- android - VideoView 错误“访问被拒绝查找属性”
- javascript - 如何在节点js中将JSON对象转换为excel文件时将Javascript数组元素存储在excel文件中?
- php - 在 wordpress 主题下开发的滑块不起作用
- swift - 为什么 UIScreen.main.bounds 是反转的而不是静态的?
- qt - 为什么在 Qt Designer 中将小部件添加到从右到左排序的水平布局中?