Jump to content

[SOLVED] Displaying A Page As An Image


Shiny_Charizard

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.