PHP GIF Images: More than 1?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

PHP GIF Images: More than 1?

Post by IceMetalPunk »

I know my mistake is probably in my almost non-existant knowledge of the GIF format, but if anyone can help me, please do. I have 2 images: jeq.gif and IceMo.gif. I am trying to create a PHP file to put them together as 1 image (I need this in PHP so I can learn how to do this with other images, not just these 2). I thought this would work:

Code: Select all

<?php
$all=file_get_contents("jeq.gif");
$all2=file_get_contents("IceMo.gif");
header("Content-type: image/gif");
echo $all;
echo $all2;
?>
However, it only showed the first image (jeq.gif), and not the second. When I use echo $all.$all2 to try to put them on one line, nothing shows up at all.

I have a feeling that my problem lies in the fact that the end of the 1st gif image is telling the program that the image is over, but I can't fix it. I tried getting rid of the last line of the jeq.gif file, and it did absolutely nothing (it still only showed the 1st image).

So, can someone please help me if you know how?

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

What do you mean by "put them together as 1 image". Do you mean as a two frame animated gif? Or side by side in a bigger image? Or merged together like two layers in Photoshop? Or something else..
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Oh, sorry, I should have been more clear. I meant side-by-side. For example, assume that jeq.gif looks like this: /\ and that IceMo.gif looks like this: (). Then the outputted image would look like this:

/\()

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Right. That's easy enough.

Code: Select all

<?php

$gif1 = imagecreatefromgif("xyz.gif");
$gif2 = imagecreatefromgif("uvw.gif");

$outputgif = imagecreate(imagesx($gif1)+imagesx($gif2),imagesy($gif1));

imagecopy($outputgif,$gif1,0,0,0,0,imagesx($gif1),imagesy($gif1));
imagecopy($outputgif,$gif2,imagesx($gif1),0,0,0,imagesx($gif2),imagesy($gif2));

header("Content-type: image/gif");
imagegif($outputgif);

?>
I've not run that, or checked it very throughly, or anything. Basically it opens both gif files, creates a big image that's the same height as gif1 and the width of both added together, then copies gif1 into it. Then it copies gif2 into it to the right of gif1, and finally it outputs the whole thing.

Things to bear in mind:

0. It requires the GD library with GIF support (either GD 1.6 or a very recent version of 2.0)
1. It assumes both gifs are the same height.
2. It ignores palette differences between the two gifs (if there's more than 256 colours between them it'll look odd.
3. I might have the arguments for imagecopy wrong.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I'm affraid you don't understand how jpg/gif encoding works..

You can't simply concatenate them.. http://www.php.net/gd wil give you access to functions that allow you to create such an image ;)
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Thanks... but I am not hosting the files on my own computer, so I can't get the GD Library. Is there NO other way to do this?

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

IceMetalPunk wrote:Thanks... but I am not hosting the files on my own computer, so I can't get the GD Library. Is there NO other way to do this?

-IMP ;) :)
The GD library might still be available. Lots of hosts have it. If you don't though, then it's pretty close to impossible. You could write your own gif encoder, but it'd be a massive undertaking, and a bit of a waste of time really.

By the way, I checked my code and it works fine. :)
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Why not save the files (temporarly), create a (temporaray) php file like

Code: Select all

<?php
echo '<img src="file1.gif"><img src="file2.gif">';
?>
and then call this page with

Code: Select all

header('temp.php');
djot
-
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

It still wouldn't be the same image, it would just be an HTML page that DISPLAYS the images.

-IMP ;) :)
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

True, but I did not understand your question this way...
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

That's okay :) But apparently, my host (Ripway.com) does not have the GD Library on their servers, because when I use the code Onion2k gave me, it just goes to the page, but it never loads anything. There can't be only one way to do this, can there?

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Looking at ripway.com's site they seem to be a free/cheap hosting service .. There's pretty much no chance of getting anything that can do image based stuff on one of them.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Strange.... I just made a script of my own (getting the functions from php.net) and uploaded it to my ripway account. The strange part is that the first error it returns is that imagegif is an unknown function. Yet imagecreatefromgif, imagecreate, and imagecopy are called before it, and they don't give any errors, which makes me think that Ripway DOES have the GD library, but something else is wrong.... I need imagegif to output it to the browser....

Any idea of what's going on?

-IMP ;) :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Some versions do not have the ability to create GIF images because of the copyright issues surrounding that format. You're likely going to have to save as a JPG then.

See if you can run phpinfo() and see exactly what the setup is. It should say in the GD section whether GIF creation is supported or not.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

IceMetalPunk wrote:Strange.... I just made a script of my own (getting the functions from php.net) and uploaded it to my ripway account. The strange part is that the first error it returns is that imagegif is an unknown function. Yet imagecreatefromgif, imagecreate, and imagecopy are called before it, and they don't give any errors, which makes me think that Ripway DOES have the GD library, but something else is wrong.... I need imagegif to output it to the browser....
Replace:

Code: Select all

header("Content-type: image/gif");
imagegif($outputgif);
with

Code: Select all

header("Content-type: image/png");
imagepng($outputgif);
It'll output as a PNG file instead of a GIF.
Post Reply