Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jul 19, 2006, 02:47   #1
ikeo
SitePoint Guru
 
ikeo's Avatar
 
Join Date: Oct 2004
Location: Austin Texas
Posts: 666
gd functions! Is there anyway to produce and image display an image withou saving it?

Right now this is what I'm doing ....

PHP Code:

 #Load the orginal image
$source_image = imagecreatefrompng('../images/captcha.png');
#Get a color and allocate to the image pallet
$color = imagecolorallocate($source_image, 100, 100, 100);
# The text to draw
$text = randomkeys(4);
$no_cache = randomkeys(8);
# Replace path by your own font path
$font = 'georgiab.ttf';
#Add the text
imagettftext($source_image, 20, 0, 35, 30, $color, $font, $text);
#Write image to file
$true = imagepng($source_image, '../images/tester.png');
#clean out the memory reserved for the image
if($source_image)
{
    
imagedestroy($source_image);
}
... but I want to know if there is a way to display the image on an html page
without first saving it to a file?

Last edited by ikeo; Jul 19, 2006 at 03:52.
ikeo is offline   Reply With Quote
Old Jul 19, 2006, 03:57   #2
vedeney
SitePoint Member
 
vedeney's Avatar
 
Join Date: Jul 2006
Location: Ukraine
Posts: 3
Quote:
header('Content-Type: image/png');
imagepng($source_image);
Hm... why are you saving it?
vedeney is offline   Reply With Quote
Old Jul 19, 2006, 04:15   #3
ikeo
SitePoint Guru
 
ikeo's Avatar
 
Join Date: Oct 2004
Location: Austin Texas
Posts: 666
I've tried that before and it won't work if I put it in an html page.
It just spits out gobbledy gook ..
ikeo is offline   Reply With Quote
Old Jul 19, 2006, 04:18   #4
onion2k
SitePoint Enthusiast
 
onion2k's Avatar
 
Join Date: Dec 2005
Location: UK
Posts: 83
Quote:
Originally Posted by ikeo
I've tried that before and it won't work if I put it in an html page.
It just spits out gobbledy gook ..
Have you tried opening a PNG file, copying the binary data, and putting that in an HTML page? Coz that's effectively what you're doing when you call imagepng() in the middle of HTML output.

What you need to do is create a script that ONLY outputs an image, and then call it using <img src="myimage.php"> in your HTML.
onion2k is offline   Reply With Quote
Old Jul 19, 2006, 04:29   #5
ikeo
SitePoint Guru
 
ikeo's Avatar
 
Join Date: Oct 2004
Location: Austin Texas
Posts: 666
thanks onion2k,
The approach in the code I posted works for me, but I was hoping I could just assign the image I created to .... say
Quote:
$image
and then use that as a surrogate image file.

so maybe what I want isn't possible ...
Thanks for the help.
ikeo is offline   Reply With Quote
Old Jul 19, 2006, 06:24   #6
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
No, doesn't make sense in the context of how webpages work. You either need to write it to a file and write out an <img> tag pointing to that new file, or write an <img> tag pointing to a PHP script which outputs the new image.

SitePoint has a tutorial on CAPTCHA scripts too. A search for "security image" may bring it up.
Dan Grossman is online now   Reply With Quote
Old Jul 19, 2006, 07:36   #7
siteguru
SitePoint Wizard
 
siteguru's Avatar
 
Join Date: Oct 2002
Location: Scotland
Posts: 2,936
The fundamental issue here is as Dan pointed out - an image is just a collection of binary data. When you put an image in a web page, you don't actually put the image there, you put a link/pointer to the image then the browser follows that link, fetches the image and displays it.

If PHP is to create the image then the process is no different - you put the link/pointer to the PHP file in the web page. The only difference is that instead of an actual physical image being fetched by the browser, it fetches the output of that PHP file (with the appropriate header() info).
siteguru is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:27.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved