使用PHP创建带有<form>数据的图像,并输出为<img src=""/>(GD)

时间:2017-03-07 20:31:52

标签: php html post gd

我有这个项目,用户可以根据肤色和头发颜色自定义头像的外观。

为了从用户那里获取数据,我使用的是html格式:

的index.php:

&#13;
&#13;
<p>Choose your hair colour:</p>
<form method="post" action="generate.php">
	<input type="radio" name="hair" value="blonde">Blonde
	<input type="radio" name="hair" value="brown">Brown
	<input type="radio" name="hair" value="black">Black
	<input type="radio" name="hair" value="red">Red

<p>Choose your skin color:</p>
	<input type="radio" name="skin" value="pale">White
	<input type="radio" name="skin" value="tan">White(Tanned)
	<input type="radio" name="skin" value="asian">Asian
	<input type="radio" name="skin" value="brown">Brown
	<input type="radio" name="skin" value="black">Black
	<p><input style="border-style: solid;"type="submit" value="SUBMIT" name="submitBtn"></p>
</form>

<img src="generate.php" alt="" />
&#13;
&#13;
&#13;

这是generate.php文件:

&#13;
&#13;
<?php

if(isset($_POST['submitBtn']))
{
	$haircolor = $_POST['hair'];
	$skincolor = $_POST['skin'];
	
	//Hair color assignment of variables
	if($haircolor == "blonde")
	{
		$h_r = 150;
		$h_g = 0;
		$h_b = -150;
		$h_op = 0;		
	}
	elseif($haircolor == "brown")
	{
		$h_r = -110;
		$h_g = -140;
		$h_b = -150;
		$h_op = 0;
	}
	elseif($haircolor == "black")
	{
		$h_r = -170;
		$h_g = -170;
		$h_b = -170;
		$h_op = 0;
	}
	elseif($haircolor == "red")
	{
		$h_r = 150;
		$h_g = -140;
		$h_b = -170;
		$h_op = 0;
	}
	else
	{
		echo "Please choose correct values (make sure you didn't miss anything)!";
	}




	//Skin Color assignment of variables
	if($skincolor == "pale")
	{
		$s_r = 50;
		$s_g = -2;
		$s_b = -10;
		$s_op = 0;		
	}
	elseif($skincolor == "tan")
	{
		$s_r = 70;
		$s_g = -30;
		$s_b = -55;
		$s_op = 0;
	}
	elseif($skincolor == "asian")
	{
		$s_r = 70;
		$s_g = -25;
		$s_b = -85;
		$s_op = 0;
	}
	elseif($skincolor == "brown")
	{
		$s_r = -50;
		$s_g = -120;
		$s_b = -250;
		$s_op = 0;
	}
	elseif($skincolor == "black")
	{
		$s_r = -135;
		$s_g = -180;
		$s_b = -205;
		$s_op = 0;
	}
	else
	{
		echo "Please choose correct values (make sure you didn't miss anything)!";
	}




//Creating the image
header ("Content-type: image/png");

$source = imagecreatefrompng("images/hair.png");
$source2 = imagecreatefrompng("images/line.png");
$source3 = imagecreatefrompng("images/clothing.png");

$destination = imagecreatefrompng("images/skin.png"); 

imagealphablending($source, true);
imagesavealpha($source, true);


$largeur_source = imagesx($source);
$hauteur_source = imagesy($source);

$largeur_source2 = imagesx($source2);
$hauteur_source2 = imagesy($source2);

$largeur_source3 = imagesx($source3);
$hauteur_source3 = imagesy($source3);

$largeur_destination = imagesx($destination);
$hauteur_destination = imagesy($destination);

imagefilter($destination, IMG_FILTER_COLORIZE, $s_r, $s_g, $s_b, $s_op);
imagefilter($source, IMG_FILTER_COLORIZE, $h_r, $h_g, $h_b, $h_op);
imagefilter($source3, IMG_FILTER_COLORIZE, 0, 0, 0, 0);
imagefilter($source2, IMG_FILTER_COLORIZE, 0, 0, 0, 0);


imagecopy($destination, $source, 0, 0, 0, 0, $largeur_source, $hauteur_source);
imagecopy($destination, $source2, 0, 0, 0, 0, $largeur_source2, $hauteur_source2);
imagecopy($destination, $source3, 0, 0, 0, 0, $largeur_source3, $hauteur_source3);

imagepng($destination);

	
}


?>
&#13;
&#13;
&#13;

图像生成本身运行良好,但它在另一页(www.mysite.com/generate.php)上生成它。

如何使它在index.php上生成自己?我添加了  在index.php中它似乎没有用。

谢谢!

0 个答案:

没有答案