使用 PHP GD 平铺图像
我正在尝试平铺多个图像,即将一个图像直接放在另一个图像下面。它们都具有相同的宽度 (120px) 和不同的高度。
这就是我所拥有的:
$finalbg = null;
for($i=0; $i<7; $i++) {
$addbg = imagecreatefromjpeg('images/left/'.$url[$drawn]);
$addsize = imagesy($addbg);
if($finalbg != null) $basesize = imagesy($finalbg); else $basesize = 0;
$newsize = $addsize+$basesize;
$newbg = imagecreatetruecolor(120, $newsize);
if($finalbg != null) imagecopy($newbg, $finalbg, 0, 0, 0, 0, 120, $basesize);
imagecopy($newbg, $addbg, 0, $basesize, 0, 0, 120, $addsize);
$finalbg = $newbg;
}
header( "Content-type: image/jpeg" );
imagejpeg($finalbg);
尺寸输出正确,但它一直告诉图像包含错误,我不知道为什么:(如果我尝试输出 addbg 或 newbg 也是如此。
谢谢。
I'm trying to tile multiple images, i.e. put one directly underneath another. They all have the same width (120px) and differing heights.
This is what I have:
$finalbg = null;
for($i=0; $i<7; $i++) {
$addbg = imagecreatefromjpeg('images/left/'.$url[$drawn]);
$addsize = imagesy($addbg);
if($finalbg != null) $basesize = imagesy($finalbg); else $basesize = 0;
$newsize = $addsize+$basesize;
$newbg = imagecreatetruecolor(120, $newsize);
if($finalbg != null) imagecopy($newbg, $finalbg, 0, 0, 0, 0, 120, $basesize);
imagecopy($newbg, $addbg, 0, $basesize, 0, 0, 120, $addsize);
$finalbg = $newbg;
}
header( "Content-type: image/jpeg" );
imagejpeg($finalbg);
The sizes are outputting correctly, but it keeps telling the image contains errors, and I have no idea why :( Same thing if I try to output addbg or newbg.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,显然问题是页面上有应该渲染的 HTML,但事实证明这与 GD 图像结合是不可能的。
所以我采取了不同的方法。我将渲染的图像保存为文件,如下所示:
并将其设置为 CSS 中的背景。现在可以了!
Okay, apparently the problem was that there was HTML on the page that was supposed to be rendered, which turns out not to be possible in combination with a GD image.
So I took a different approach. I saved the rendered image as a file, like so:
and set it as the background in CSS. And now it works!