PHP - 使用来自用户的表单数据的GD库使用$ _POST对图像进行着色

时间:2016-12-29 05:41:57

标签: php post gd

我的图片看起来像这样:

Image

其中包含basehair.png与base.php合并,以便创建具有头发的模型的单个图像。目标是改变模型上头发的颜色以适合用户对头发颜色的选择。为此,我有一个html格式的文件:

的index.php

<p>Choose your hair color:</p><form method="post" action="generate_illu.php">
<input type="radio" name="radio" value="blonde">Blonde
<input type="radio" name="radio" value="brown">Brown
<input type="radio" name="radio" value="black">Black
<input type="radio" name="radio" value="red">Red
<input style="border-style: solid;"type="submit" value="SUBMIT" name="submitBtn"></form>

带有以下代码的php文件 (GENERATE_ILLU.PHP)

<?php
if (isset($_POST['submitBtn']))
{
   $haircolor = $_POST['radio'];
   header ("Content-type: image/png");


if ($haircolor == "blonde")
{
    $source = imagecreatefrompng("images/hairbase.png"); // The base hair is the source
    $destination = imagecreatefrompng("images/base.png"); // The base model is the destination
    imagealphablending($source, true);
    imagesavealpha($source, true);
    imagefilter($source, IMG_FILTER_COLORIZE, 0, 150, 100, 100); //php function used to apply a tint to "change" the colour of the hair. Works fine on its own
    $width_source = imagesx($source);
    $height_source = imagesy($source);
    $width_destination = imagesx($destination);
    $height_destination = imagesy($destination);

    imagecopy($destination, $source, 53, 0, 0, 0, $width_source, $height_source);
    imagepng($destination);

}

elseif ($haircolor == "brown")
{
    $source = imagecreatefrompng("images/hairbase.png"); 
    $destination = imagecreatefrompng("images/base.png"); 
    imagealphablending($source, true);
    imagesavealpha($source, true);
    imagefilter($source, IMG_FILTER_COLORIZE, 0, 0, 200, 200);
    $width_source = imagesx($source);
    $height_source = imagesy($source);
    $width_destination = imagesx($destination);
    $height_destination = imagesy($destination);

    imagecopy($destination, $source, 53, 0, 0, 0, $width_source, $height_source);
    imagepng($destination);

}

elseif ($haircolor == "black")
{
    $source = imagecreatefrompng("images/hairbase.png"); // The base hair is the source
    $destination = imagecreatefrompng("images/base.png"); // La photo est la destination
    imagealphablending($source, true);
    imagesavealpha($source, true);
    imagefilter($source, IMG_FILTER_COLORIZE, 0, 150, 100, 100);
    $width_source = imagesx($source);
    $height_source = imagesy($source);
    $width_destination = imagesx($destination);
    $height_destination = imagesy($destination);

    imagecopy($destination, $source, 53, 0, 0, 0, $width_source, $height_source);
    imagepng($destination);
}

elseif ($haircolor == "red")
{
    $source = imagecreatefrompng("images/hairbase.png"); // The base hair is the source
    $destination = imagecreatefrompng("images/base.png"); // La photo est la destination
    imagealphablending($source, true);
    imagesavealpha($source, true);
    imagefilter($source, IMG_FILTER_COLORIZE, 0, 250, 200, 100);
    $width_source = imagesx($source);
    $height_source = imagesy($source);
    $width_destination = imagesx($destination);
    $height_destination = imagesy($destination);

    imagecopy($destination, $source, 53, 0, 0, 0, $width_source, $height_source);
    imagepng($destination);
}
else
{
    echo "Please choose one of the proposed hair colors.";
}}?>

而&#34; else&#34;这部分条件工作正常,其他部分根本不会改变颜色。我怀疑这可能与imagefilter有关($ source,IMG_FILTER_COLORIZE,0,150,100,100); ,但是当我单独使用它时,在if / else语句之外,它运行正常。

感谢您的帮助!

0 个答案:

没有答案