Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Saving A Php Generated Image To The Server, Help Needed Please
vujsa
post Sep 19 2005, 11:38 AM
Post #1


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43



I'm trying to create a script that will generate an image and send it back to the browser while saving the same image to the server. I'm doing this because the image should be dynamically generated but because of the load that would place on a server if the image happened to be requested frequently, I've decided to build in a means to serve the static version of the image most of the time.
What isn't included here is the function that will check the age of the file in cache directory and serve the static image if it is less than an hour old. This isn't really needed to get this part of the script working so to prevent making the code more complicated, I left it out. I'm having trouble saving the generated image to the server. I have finally gotten it to save the file but it is saving the data as text and not as a PNG image file.

Here is the script as it is currently written:
CODE
<?php
// Designate a background image
 $background = imagecreatefrompng("background001.png");

// Designate a foreground image
 $insert = imagecreatefrompng("token001.png");

// Select the origin pixel of the foregrounf image as the transparent color
 imagecolortransparent($insert,imagecolorat($insert,0,0));

// Get dimisions of the foreground image
 $insert_x = imagesx($insert);
 $insert_y = imagesy($insert);

// Place the foreground image on the background image
 imagecopymerge($background,$insert,345,72,0,0,$insert_x,$insert_y,100);

// Insert Text into the image
 $text = $_GET['action'];
 $red =  imagecolorallocate($background, 255, 0, 0);
 $font = "verdana.ttf";
 imagettftext($background, 14, 0, 238, 90, $red, $font, $text);
 imagettftext($background, 8, 0, 238, 125, $red, $font, mktime());

// Start the content buffer to hold information prior to saving
 ob_start();

// Set the header information of the image
 header ("Content-type: image/png");
 header('Content-transfer-encoding: binary');

// Output a png image
 imagepng($background,'',100);

//Assign a variable to hold the buffer information and clear/close buffer
 $output = ob_get_clean();

// Clear computer memory of image data
 imagedestroy($background);

// Output the buffer information to browser
 echo $output;

// Open or create a file and write buffer information to it.
 $cachefile = "cache/test001.png";
 $handle = fopen($cachefile, "w+b");
 fwrite($handle, $output);

// Close file
 fclose($handle);

?>


Here is the output shown of test001.png:
CODE
PNG



IHDR
$!,.¢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Ρf.S'@eG-эL']O=%S
:bX]d@4
:@4: ? ȝ
sO__cLCnwC5j+y-ٚ9M
P]{(ɛU
,>L`Lso&c$tY5OCa0sG^
~0MeZ#P4YʜXxWa86v=m\&z}*z}Va#A=KZZ!S+LppO^6h H-=퓡
lPHl 9Hӥ ٣ %yn.hV}X S%V弴~:-'%a)p
DҙYȤ32kUg`[
,;W*2C5({_7j?z2{Dz4XR6+FtU{MBŋq76>R1ި'MuVVр.}2hʌ =ѐZ`X©@ha*oQ-2dtIh؜֊JV-]
/|2\9n@"F 6Xu8
AU|H)*gZ!) J]$eԎcTsOIN/ 
l+Ql˳3Ru)%1;Ӣ5(ƳՄ =Y}MB~&GD'j35TT+G
xv׳x$*B|IFa,!ك ߣb|VO ݃żV<;VVxR:G{ /ȴ[Ӝn"SAPď4KJ~3)5iXkc!xxƎ%YT(<8=@c:Jrg&yao+9oh$Y=Av%e*Q3ptIYl*;.VYzrxM p0!Fn$h+Xf4N)^ɯcDi31>.=i$i]
msN9j HV~n'D!'*OҨO(= Ev9BqKXl0!D盚8O Ml ș@֢ud˫5+T2as3GXrr #-HL ?P@$j
u:[0C!O{:.1*ma㑗c I1&~g .YP`k$2
" qhZh&Qg,eQQ#_Hr|()U#&=X^7, g2LA<Ռ{C(HWsfT9 j\&]sjdI(!hP/cgH'F97ARtl^L朏}C,{:r?{mFPWEq)VLLT'o?2t83M:9!̹rqleT\Y_}y<0OKr 5ա
Gf7*VRT{И tԃM/FtR` B[͊Gz#QӇh4!ubKb$n
i EsaDn݀'&{k}3k7
^ǔWr`
8W|i"D!OP-C D!"ęCf"D3r=D!$D d!B8kc+



Anyone know how to successfully generate and save an image file using PHP?

Thanks for any help you can give. cool.gif

vujsa
Go to the top of the page
 
+Quote Post
mastercomputers
post Sep 19 2005, 11:57 AM
Post #2


PESTICIDAL MANIAC
Group Icon

Group: Members
Posts: 626
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27



Hey vujsa,

Simple error, this line:

$handle = fopen($cachefile, "w+b");

Should be:

$handle = fopen($cachefile, 'wb');

That should be it.

Other than that, I would probably have to test it out myself.


Cheers,


MC
Go to the top of the page
 
+Quote Post
vujsa
post Sep 19 2005, 12:21 PM
Post #3


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43



Well, that didn't fix the bug. I'm still getting trash instead of the image.

Any ideas?

vujsa

Go to the top of the page
 
+Quote Post
vizskywalker
post Sep 19 2005, 08:55 PM
Post #4


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



Instead of using fwrite() to create the png, you need t use imagepng as what you are actually saving is an image resource, not the png image. Simply use this format: imagepng($image, $filename) where $image is the png image resource and $filename is the name of the file you want to save it to.

~Viz
Go to the top of the page
 
+Quote Post
vujsa
post Sep 19 2005, 11:10 PM
Post #5


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43



Okay everyone, false alarm.

The problem wasn't in the code but instead was in a .htaccess two directories above the image to be shown. I forgot to delete the .htaccess that redirected .png file extensions to the PHP engine after I replaced that set up. I now use a diferent means to mask urls.

Everything is working fine now with out the need to use any file system functions ot object buffering.

Basically, to print to file use:
imagepng($image, 'path/to/file.png');

and to print to browser use:
imagepng($image, '');

hope this helps everyone.

vujsa
Go to the top of the page
 
+Quote Post
vizskywalker
post Sep 20 2005, 12:12 AM
Post #6


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



That's what I said vujsa! Glad it's working now. If anymore problems arise just post back here.

~Viz
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Random Image / Random Header(5)
  2. Help With Some Php Or Maybe Asp(5)
  3. Mini Apache Server W/ Php(5)
  4. Site Counters - Help Needed(13)
  5. Server-friendly Php Scripts(5)
  6. Forum Signature-image With Php(8)
  7. Php: Write Random Text As Image(3)
  8. Include Directives And Efficancey(4)
  9. Is It Possible To Create A String Image In Chinese(0)
  10. How Do I Make PHP Based Image Gallery Like This?(20)
  11. Uploading Image Via Admin Menu?(2)
  12. Viper Guestbook(5)
  13. How Do I Resize An Image On The Fly?(8)
  14. How To Reset The Server Variable Php_auth_user(9)
  15. User Authentication Session Handling Problems(14)
  1. Script For Viewing A Random Image Needed(3)
  2. Php :: Adding Image Over Image(5)
  3. Image Works With Php And Gd(3)
  4. Php :: Image Pixel Per Inch And Conversion(1)
  5. Basics Of Php For Beginners - Suggestion(5)
  6. Using Php With A Mail Server(2)
  7. Coders Needed To Help On A Bulletin Board System (BBS)(4)
  8. Need Help With Background Image...(12)
  9. Image Popup On Mouseover(23)
  10. Dynamic Gd Image(2)
  11. Dynamic Php Image And Better Php Code Question(10)
  12. View Php In Another Server(4)


 



- Lo-Fi Version Time is now: 5th December 2008 - 12:37 PM