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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/ 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) Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570621 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570622 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570625 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570626 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570638 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570739 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570743 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? Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570765 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570770 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570838 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570940 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 Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570941 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.")); ?> Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570943 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.")); ?> Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570948 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) Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-570953 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571023 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? Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571062 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 Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571066 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 Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571071 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571078 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571084 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? Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571086 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. Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571093 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? Link to comment https://forums.phpfreaks.com/topic/111177-solved-displaying-a-page-as-an-image/#findComment-571095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.