1

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.

bur
  • 604
  • 5
  • 20
  • Is $url[$drawn] correct? Or do you need something like $url[$i]? – djot Jan 11 '12 at 03:23
  • to see the actual error that you are getting, go to File>Save Page As then open the "image" up in notepad. Read the error, and post it here. – Different55 Jan 11 '12 at 03:23
  • yup, $url[$drawn] is correct :) I see a load of random characters, but no error message. – bur Jan 11 '12 at 03:35
  • I added if($finalbg != null) imagedestroy($finalbg); $finalbg = $newbg; imagedestroy($newbg); imagedestroy($addbg); in the loop, and imagedestroy($finalbg); after the loop. Unless I comment the one for $newbg, I get the error: Warning: imagesy(): 11 is not a valid Image resource in... (lines containing finalbg) I'm at a total loss here... – bur Jan 11 '12 at 04:58
  • Even simply header("Content-type: image/jpeg"); $addbg = imagecreatefromjpeg('images/left/'.$url_mo[0]); imagejpeg($addbg); imagedestroy($addbg); doesnt work ;( – bur Jan 11 '12 at 16:48

1 Answers1

1

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:

imagejpeg($finalbg, 'images/left/bg.jpg');

and set it as the background in CSS. And now it works!

bur
  • 604
  • 5
  • 20