470,833 Members | 1,375 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Can GD load an image from another php script?

Hi everybody!!!
I have a script (foo.php) which uses GD to create an image and sends it
to the browser:

<?php
$img = imagecreatefrompng("mypng.png");

/* other stuff */

header("Content-type: image/png");
imagepng($img);

imagedestroy($img);
?>

I also have another php script: goofy.php.
How can I use imagecreatefrompng to load the result of foo.php (without
saving it on the hard disk first)?

I tried with: imagecreatefrompng("foo.php") but doesn't work...
Can anyone help me, please?

I'm sorry for my bad english...

--
Quando noi ignoranti eravamo di pi, tutto era pi bello...
(A. Celentano)

Per rispondermi in privato:
s e p r a n o(punto)a n t o n i o(chiocciola)l i b e r o(punto)i t
Jan 3 '06 #1
6 1898
Darkbyte [Work] said the following on 03/01/2006 19:57:
Hi everybody!!!
I have a script (foo.php) which uses GD to create an image and sends it
to the browser:

<?php
$img = imagecreatefrompng("mypng.png");

/* other stuff */

header("Content-type: image/png");
imagepng($img);

imagedestroy($img);
?>

I also have another php script: goofy.php.
How can I use imagecreatefrompng to load the result of foo.php (without
saving it on the hard disk first)?

I tried with: imagecreatefrompng("foo.php") but doesn't work...
Can anyone help me, please?


Well, you could do it with the absolute HTTP URL, i.e.:

imagcreatefrompng("http://example.com/blah/foo.php");

(assuming allow_url_fopen is enabled).

However, doing a complete HTTP transaction just to get an image from
yourself is overkill.

A more sensible, programmatic way of doing this is to put the
image-creation stuff from foo.php into a function in an include() file,
and then call it from foo.php and goofy.php.

--
Oli
Jan 3 '06 #2
Darkbyte [Work] wrote:
Hi everybody!!!
I have a script (foo.php) which uses GD to create an image and sends it
to the browser:

<?php
$img = imagecreatefrompng("mypng.png");

/* other stuff */

header("Content-type: image/png");
imagepng($img);

imagedestroy($img);
?>

I also have another php script: goofy.php.
How can I use imagecreatefrompng to load the result of foo.php (without
saving it on the hard disk first)?

I tried with: imagecreatefrompng("foo.php") but doesn't work...
Can anyone help me, please?

Try this:

$img = imagecreatefromstring(file_get_contents("http://.../foo.php"));

However, *do* use http:// in the name so that the function loads the
page and not the text file.
--

- lpher
---------------------------------------------
"Man sieht nur das, was man wei" (Goethe)
Jan 3 '06 #3
> Well, you could do it with the absolute HTTP URL, i.e.:

imagcreatefrompng("http://example.com/blah/foo.php");
Oh yes, I already was doing it... I just was looking for another way to
force imagecreatefrompng() to "execute" my script rather then read it...

[cut]
A more sensible, programmatic way of doing this is to put the
image-creation stuff from foo.php into a function in an include() file,
and then call it from foo.php and goofy.php.


Aehm... I'm using my foo.php script in the html code:

<img src="/blah/foo.php?width=320&height=100" border="0" />

Assuming I'm going to put the stuff into a function, and assuming I'm
going to create a php script that just calls that function so I can load
the image directly from html code, how can it help me with
imagecreatefrompng()?
How could I use the function with imagecreatefrompng()?

--
Quando noi ignoranti eravamo di pi, tutto era pi bello...
(A. Celentano)

Per rispondermi in privato:
s e p r a n o(punto)a n t o n i o(chiocciola)l i b e r o(punto)i t
Jan 4 '06 #4
> Assuming I'm going to put the stuff into a function, and assuming I'm
going to create a php script that just calls that function so I can load
the image directly from html code, how can it help me with
imagecreatefrompng()?
How could I use the function with imagecreatefrompng()?

DONE!!! DONE!!! AUHAUHAUHAUHAUHAUHA LOOOOOOLLLLL :)

function mystuff($width=1, $height=1)
{
$img = imagecreatetruecolor($width, $height);
/* stuff here */
imagepng($img);
}

$img = imagecreatefromstring(mystuff(320, 200));
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);

That's cool!!! :)

Thanks everybodyyyyyyyy!!!
:) :) :) :)

--
Quando noi ignoranti eravamo di pi, tutto era pi bello...
(A. Celentano)

Per rispondermi in privato:
s e p r a n o(punto)a n t o n i o(chiocciola)l i b e r o(punto)i t
Jan 4 '06 #5
Darkbyte [Work] said the following on 04/01/2006 09:20:
Assuming I'm going to put the stuff into a function, and assuming I'm
going to create a php script that just calls that function so I can
load the image directly from html code, how can it help me with
imagecreatefrompng()?
How could I use the function with imagecreatefrompng()?


DONE!!! DONE!!! AUHAUHAUHAUHAUHAUHA LOOOOOOLLLLL :)

function mystuff($width=1, $height=1)
{
$img = imagecreatetruecolor($width, $height);
/* stuff here */
imagepng($img);
}

$img = imagecreatefromstring(mystuff(320, 200));
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);


What??? This code is nonsensical!

mystuff() uses imagepng() to output an image to the browser, and has no
return value. Therefore you're effectively calling
imagecreatefromstring(NULL), which just throws warnings. This means that
$img == FALSE, so imagepng() and imagedestroy() fail and throw warnings
as well. Comment out the header() line and you'll see what I mean.

The only reason this appears to work is the combination of mystuff()
outputting (*not* returning) PNG data, coupled with the Content-Type header.
--
Oli
Jan 4 '06 #6
> What??? This code is nonsensical!

Ja!!! It's true, but I changed the code to some other:

function create_my_image($w=1, $h=1)
{
$img = @imagecreatetruecolor($w, $h);
if ($img)
{
/* Other stuff here */
}
return $img;
}

and I use it as:

<?php
include("inc/mod_image.php");

if ($my_img = create_my_image(320, 200))
{
/* Other stuff to the image */

header("Content-type: image/png");
imagepng($my_img);

imagedestroy($my_img);
}
?>

This should work better... or shouldn't?

--
When ignorants were more, everything was better...
(A. Celentano)

Per rispondermi in privato:
s e p r a n o(punto)a n t o n i o(chiocciola)l i b e r o(punto)i t
Jan 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Charlie | last post: by
1 post views Thread by Christophe Candas | last post: by
6 posts views Thread by Seth Illgard | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.