Shiny_Charizard Posted June 20, 2008 Share Posted June 20, 2008 Is there any way I could display this page as an image? Link: http://norberto.freehostia.com/EA/Display_Avatar.php?AvID=1 Note: The sprites used are not mine I am using the for testing purposes. Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 You need to read the image file, and before that set the header to the correct content-type (i.e. image/png) Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 20, 2008 Author Share Posted June 20, 2008 But it's not one image. It is a lot of images on top of each other. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 It sure would be possible to merge the images with PHP (using GD). I actually feel like writing an example I'll post it later. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 20, 2008 Author Share Posted June 20, 2008 I would be very thankful if you thought me how to do that. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 Here goes! <?php // function merge_png takes 2 or more URLs to PNG files as arguments, and merges the PNGs function merge_png() { //check and store arguments if (func_num_args() < 2) { echo 'Error: Function merge_png takes 2 or more arguments.'; return false; } $args = func_get_args(); //set image width and height $w = 80; $h = 100; //create blank, fully transparent canvas $im = imagecreatetruecolor($w, $h); imagesavealpha($im, true); $trans_color = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans_color); //merge PNGs foreach($args as $url) { $dest_im = @imagecreatefrompng(str_replace(' ', '%20', $url)); @imagecopy($im, $dest_im, 0, 0, 0, 0, $w, $h); } //output final image header('Content-type: image/png'); imagepng($im); imagedestroy($im); return true; } //usage //image credits goes to the creator merge_png('http://www.anitaria.com/images/avatars/equips/Mid Tone.png', 'http://www.anitaria.com/images/avatars/equips/1192900734.png'); ?> The PNGs should have the same dimensions (in your and my example they're all 80x100px), in order for the placements to work. And they should be transparent of course. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 I tried it and I get this error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/www/norberto.freehostia.com/EA/Display_Avatar.php on line 16 Warning: Cannot modify header information - headers already sent by (output started at /home/www/norberto.freehostia.com/EA/Connect.php:3) in /home/www/norberto.freehostia.com/EA/Display_Avatar.php on line 51 ‰PNG ��� IHDR���P���d���ú·���6IDATxœíÁ1��� õOm/ �������������������������������~}d�[5AÙ����IEND®B`‚ Punk_Rocker277, Level 1 Here is the code: <?php #Display_Avatar.php #Require Connect File Require_Once('Connect.php'); #Get AvID $AvID = $_GET["AvID"]; #Select Rows From Members Database $Results = mysql_query("SELECT * FROM `Members` WHERE `Member_ID` = '$AvID'"); #Fetch Rows From Members Database $Rows = mysql_fetch_array($Results); $Count_Rows = mysql_num_rows($Rows); if(empty($AvID)) { echo "<center>Please choose an avatar to view!</center>"; } elseif($Count_Rows = "0") { echo "<center>The user you have chosen does not exist!</center>"; } else { // function merge_png takes 2 or more URLs to PNG files as arguments, and merges the PNGs function merge_png() { //check and store arguments if (func_num_args() < 2) { echo 'Error: Function merge_png takes 2 or more arguments.'; return false; } $args = func_get_args(); //set image width and height $w = 80; $h = 100; //create blank, fully transparent canvas $im = imagecreatetruecolor($w, $h); imagesavealpha($im, true); $trans_color = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans_color); //merge PNGs foreach($args as $url) { $dest_im = @imagecreatefrompng(str_replace(' ', '%20', $url)); @imagecopy($im, $dest_im, 0, 0, 0, 0, $w, $h); } //output final image header('Content-type: image/png'); imagepng($im); imagedestroy($im); return true; } //usage //image credits goes to the creator merge_png('$Rows[body]', '$Rows[Equip_Hair]', '$Rows[Equip_Eyes]', '$Rows[Equip_Mouth]', '$Rows[Equip_Face]', '$Rows[Equip_Torso]', '$Rows[Equip_Back]', '$Rows[Equip_LArm]', '$Rows[Equip_LHand]', '$Rows[Equip_RArm]', '$Rows[Equip_RHand]', '$Rows[Equip_Legs]', '$Rows[Equip_Feet]'); echo "<br /><br /><br /><br /><small>$Rows[userName], Level $Rows[Level]</small>"; } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 You need to put that function in its own PHP file and call it like, merge.php. Then, do: <img src="merge.php?userid=7" /> Or something like that, which will let the script get the right images and use the merge_png function to put it all together and output it. You need to code the "get the right images" part. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 Can you explain it a bit more? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Make a separate file named merge.php. Have this page use a passed GET parameter for the User ID or something to pull the correct images from the database, and have the function code inside that script too. Then use like merge_png($image1, $image2, $image3), etc. Now, on that other page you had, where you actually want to display it, you set the src attribute of the img tag as merge.php?userid=6 (except it would be dynamically set, obviously) and the script sends the proper headers and displays the image. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 21, 2008 Share Posted June 21, 2008 DarkWater is right (the image can't display alongside other content that way), but the error you're getting has to do with your mysql_num_rows(), which you are 'feeding' with the wrong variable $Rows. Should be $Results instead. When you run merge_png, you can't put the variables inside single quotes (treats them as strings). Either use double quotes or no quotes at all. Code for the page you want to display the image on (run it like Display_Avatar.php?AvID=[AvID_here]): <?php #Display_Avatar.php #Require Connect File Require_Once('Connect.php'); #Get AvID $AvID = $_GET["AvID"]; #Select Rows From Members Database $Results = mysql_query("SELECT * FROM `Members` WHERE `Member_ID` = '" . mysql_real_escape_string($AvID) . "'"); #Count rows $Count_Rows = mysql_num_rows($Results); #Fetch Rows From Members Database $Rows = mysql_fetch_array($Results); if(empty($AvID) && $AvID != '0') { echo "<center>Please choose an avatar to view!</center>"; } elseif($Count_Rows = "0") { echo "<center>The user you have chosen does not exist!</center>"; } else { echo '<img src="merge_png.php?AvID=$AvID" width="80" height="100" alt="" /><br /><br /><br /><br /><small>{$Rows["UserName"]}, Level {$Rows["Level"]}</small>'; } ?> merge_png.php: <?php #merge_png.php // function merge_png takes 2 or more URLs to PNG files as arguments, and merges the PNGs function merge_png() { //check and store arguments if (func_num_args() < 2) { echo 'Error: Function merge_png takes 2 or more arguments.'; return false; } $args = func_get_args(); //set image width and height $w = 80; $h = 100; //create blank, fully transparent canvas $im = imagecreatetruecolor($w, $h); imagesavealpha($im, true); $trans_color = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans_color); //merge PNGs foreach($args as $url) { $dest_im = @imagecreatefrompng(str_replace(' ', '%20', $url)); @imagecopy($im, $dest_im, 0, 0, 0, 0, $w, $h); } //output final image header('Content-type: image/png'); imagepng($im); imagedestroy($im); return true; } #Require Connect File Require_Once('Connect.php'); #Get AvID $AvID = $_GET["AvID"]; #Select Rows From Members Database $Results = mysql_query("SELECT * FROM `Members` WHERE `Member_ID` = '" . mysql_real_escape_string($AvID) . "'"); #Count rows $Count_Rows = mysql_num_rows($Results); #Fetch Rows From Members Database $Rows = mysql_fetch_array($Results); if(empty($AvID) && $AvID != '0') { echo "<center>Please choose an avatar to view!</center>"; } elseif($Count_Rows = "0") { echo "<center>The user you have chosen does not exist!</center>"; } else { merge_png( $Rows['Body'], $Rows['Equip_Hair'], $Rows['Equip_Eyes'], $Rows['Equip_Mouth'], $Rows['Equip_Face'], $Rows['Equip_Torso'], $Rows['Equip_Back'], $Rows['Equip_LArm'], $Rows['Equip_LHand'], $Rows['Equip_RArm'], $Rows['Equip_RHand'], $Rows['Equip_Legs'], $Rows['Equip_Feet'] ); } ?> Doing it this way, you connect to the database in both scripts, which is a bit inefficient. You could store the $Rows array in a session in the first script, and then use the array in the image script, but then you wouldn't be able to run the image script by itself. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 On the merge_png.php file I get this error: Warning: Cannot modify header information - headers already sent by (output started at /home/www/norberto.freehostia.com/EA/Connect.php:3) in /home/www/norberto.freehostia.com/EA/Merge_Avatar.php on line 26 Then it displays the image's code. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 21, 2008 Share Posted June 21, 2008 As it says in the error, Connect.php is outputting something, which we can't have. Please post the code of the file (Connect.php). Remember to remove your login details Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 Here it is: <?php mysql_connect("-------", "----------", "------------") or die(mysql_error("<font color='red'>Error:</font> Couldn't Connect to MySQL.")); echo"<br />"; mysql_select_db("---------") or die(mysql_error("<font color='red'>Error:</font> Database not found.")); ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 21, 2008 Share Posted June 21, 2008 Why do you output that HTML line break? Just remove it: <?php mysql_connect("-------", "----------", "------------") or die(mysql_error("<font color='red'>Error:</font> Couldn't Connect to MySQL.")); mysql_select_db("---------") or die(mysql_error("<font color='red'>Error:</font> Database not found.")); ?> Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 Thanks a lot, it worked but is there anyway I could save the image into a file called "$AvID.png" because most forums don't let users put dynamic images on there signatures. ($AvID being the avatars ID) Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 21, 2008 Share Posted June 21, 2008 No problem To save the file instead of outputting it, firstly put the following line just inside the merge_png function: global $AvID; Then change this (in the merge_png function): //output final image header('Content-type: image/png'); imagepng($im); to: //save final image imagepng($im, "$AvID.png"); The PNG should then be saved in the same directory as the function is run in. Obviously you'll need to have right to write to that particular directory. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 I get this error: Parse error: syntax error, unexpected T_GLOBAL in /home/eaexoph/public_html/Merge_Avatar.php on line 57 Here is the code: <?php #Merge_Avatar.php // function merge_png takes 2 or more URLs to PNG files as arguments, and merges the PNGs function merge_png() { //check and store arguments if (func_num_args() < 2) { echo 'Error: Function merge_png takes 2 or more arguments.'; return false; } $args = func_get_args(); //set image width and height $w = 80; $h = 100; //create blank, fully transparent canvas $im = imagecreatetruecolor($w, $h); imagesavealpha($im, true); $trans_color = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans_color); //merge PNGs foreach($args as $url) { $dest_im = @imagecreatefrompng(str_replace(' ', '%20', $url)); @imagecopy($im, $dest_im, 0, 0, 0, 0, $w, $h); } //save final image imagepng($im, "$AvID.png"); imagedestroy($im); return true; } #Require Connect File Require_Once('Connect.php'); #Get AvID $AvID = $_GET["AvID"]; #Select Rows From Members Database $Results = mysql_query("SELECT * FROM `Members` WHERE `Member_ID` = '" . mysql_real_escape_string($AvID) . "'"); #Count rows $Count_Rows = mysql_num_rows($Results); #Fetch Rows From Members Database $Rows = mysql_fetch_array($Results); if(empty($AvID) && $AvID != '0') { echo "<center>Please choose an avatar to view!</center>"; } elseif($Count_Rows = "0") { echo "<center>The user you have chosen does not exist!</center>"; } else { merge_png( global $AvID; $Rows['Body'], $Rows['Equip_Hair'], $Rows['Equip_Eyes'], $Rows['Equip_Mouth'], $Rows['Equip_Face'], $Rows['Equip_Torso'], $Rows['Equip_Back'], $Rows['Equip_LArm'], $Rows['Equip_LHand'], $Rows['Equip_RArm'], $Rows['Equip_RHand'], $Rows['Equip_Legs'], $Rows['Equip_Feet'] ); } ?> Sorry for bothering you so much but is there a way I can save it in another directory? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 global $AvID; goes inside the function. Put it right beneath the { of the function declaration. And you can change that path through that call to imagepng(). =P Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 I get this error: Warning: imagepng() [function.imagepng]: Unable to open '1.png' for writing: Permission denied in /home/eaexoph/public_html/Merge_Avatar.php on line 29 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Make the directory 755 so that you can write. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 I did and it still says that. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 The directory that Merge_Avatar.php is in is 755'd? Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted June 21, 2008 Author Share Posted June 21, 2008 I uploaded the Merge_Avatar.php file to the public_html directory and that directory's permissions are 0755. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Are you saving it to that directory or a different one? Like, do you have it as "avatars/$AvID.png" in the imagepng() call? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.