|
|
|
|
![]() ![]() |
Sep 19 2005, 11:38 AM
Post
#1
|
|
|
Absolute Newbie 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 $!,.¢ BR}E[Uk[J֥oZbEE ٳB2 Yf23ǝs h'9<s=[`cÉV`cƐe/q01 K1}c 1;01@fv#1acǰ_ 1PEE/TT1[X!yVA U'&sWV(tYsD]1G`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%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㑗cI1&~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<0OKr5ա Gf7*VRT{И tԃM/FtR`B[͊Gz#QӇh4!ubKb$n i EsaDn݀'&{k}3k7 ^ǔWr` 8W|i"D!OP-CD!"ę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. vujsa |
|
|
|
Sep 19 2005, 11:57 AM
Post
#2
|
|
|
PESTICIDAL MANIAC 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 |
|
|
|
Sep 19 2005, 12:21 PM
Post
#3
|
|
|
Absolute Newbie 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 |
|
|
|
Sep 19 2005, 08:55 PM
Post
#4
|
|
|
Techno-Necromancer 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 |
|
|
|
Sep 19 2005, 11:10 PM
Post
#5
|
|
|
Absolute Newbie 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 |
|
|
|
Sep 20 2005, 12:12 AM
Post
#6
|
|
|
Techno-Necromancer 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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th December 2008 - 12:37 PM |