想象黑条

时间:2017-09-11 18:39:08

标签: php imagick

我最近将服务器上的ImageMagick和centos更新为7.0.7-0和cantos7。我在PHP Imagick脚本中遇到了一个非常独特的问题,其中黑条显示在所创建的图像上。

Here is a live example of the issue。这只狮子通常会重新着色到用户输入,并且似乎仍然可以正确地重新着色,但这些黑色线条是新的,显然非常具有破坏性。这是imagemagick的某种配置问题还是与新版本的冲突,但在服务器升级之前相同的代码工作正常。

图像生成器

public function drawPet($species, $baseHex, $eyeHex, $age, $gender, $genderHex, $markings, $layer1, $layer2, $layer3, $layer4)
{
    // Call pet data
    $pet = DataPet::where('name', $species)->first();

    // Instantiate adult image objects
    if ($age == 'adult')
    {
        $base = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/base_color.png");
        $eye = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/eye_color.png");
        $lines = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/lines.png");
        $shading = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/shading.png");
        $lights = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/lights.png");
        if ($pet->name == 'snake') {
            $scales = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/snake/scales.png");
        }

        // Checks for gendered attribute
        if ($pet->gendered == true )
        {
            if ($gender == $pet->gendered_value)
            {
                $genderBase = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->gendered_attribute . "_color.png");
                $genderLines = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->gendered_attribute . "_lines.png");
                $genderShading = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->gendered_attribute . "_shading.png");
                $genderLights = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->gendered_attribute . "_lights.png");

                // Recolors attribute if recolorable
                if ($pet->gendered_recolor == true)
                {
                    $genderBase->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                    $genderBase->setImageBackgroundColor($genderHex);
                    $genderBase->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                }
            }
        }
    }

    // Instantiate baby image objects
    else {
        $base = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/base_color.png");
        $eye = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/eye_color.png");
        $lines = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/lines.png");
        $shading = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/shading.png");
        $lights = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/lights.png");
        if ($pet->name == 'snake') {
            $scales = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/snake/baby/scales.png");
        }
    }

    // Recolors base
    $base->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
    $base->setImageBackgroundColor($baseHex);
    $base->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);

    // Recolors eyes
    $eye->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
    $eye->setImageBackgroundColor($eyeHex);
    $eye->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);

    // Begin composite
    // If extra layers: under markings
    if ($pet->layer_name1)
    {
        if ($pet->layer_pos1 == 1)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name1 . ".png");
            }

            else {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name1 . ".png");
            }

            if ($pet->layer_recolor1 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer1);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name2)
    {
        if ($pet->layer_pos2 == 1)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name2 . ".png");
            }

            else {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name2 . ".png");
            }

            if ($pet->layer_recolor2 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer2);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name3)
    {
        if ($pet->layer_pos3 == 1)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name3 . ".png");
            }

            else {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name3 . ".png");
            }

            if ($pet->layer_recolor3 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer3);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name4)
    {
        if ($pet->layer_pos4 == 1)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick("./img/pets/" . $species . "/" . $pet->layer_name4 . ".png");
            }

            else {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name4 . ".png");
            }

            if ($pet->layer_recolor4 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer4);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    // Layer markings
    if ($age == 'adult')
    {
        foreach ($markings as $marking => $color)
        {
         // Weeds empty markings
            if ($marking != '') {
                // Ensures pet has region
                $mData = DataMarking::where('name', $marking)->first();

                if ($mData) {
                    if ($mData->region == 'leg' && $pet->legs == 1) {
                        $marking = new \Imagick("./img/pets/" . $species . "/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'tail' && $pet->tail == 1) {
                        $marking = new \Imagick("./img/pets/" . $species . "/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'ear' && $pet->ears == 1) {
                        $marking = new \Imagick("./img/pets/" . $species . "/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'wing' && $pet->wings == 1) {
                        $marking = new \Imagick("./img/pets/" . $species . "/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region != 'leg' && $mData->region != 'tail' && $mData->region != 'ear' && $mData->region != 'wing') {
                        $marking = new \Imagick("./img/pets/" . $species . "/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }
                }
            }
        }
    }

    else
    {
        foreach ($markings as $marking => $color)
        {
            // Weeds empty markings
            if ($marking != '') {
                // Ensures pet has region
                $mData = DataMarking::where('name', $marking)->first();

                if ($mData) {
                    if ($mData->region == 'leg' && $pet->legs == 1) {
                        $marking = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'tail' && $pet->tail == 1) {
                        $marking = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'ear' && $pet->ears == 1) {
                        $marking = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region == 'wing' && $pet->wings == 1) {
                        $marking = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }

                    if ($mData->region != 'leg' && $mData->region != 'tail' && $mData->region != 'ear' && $mData->region != 'wing') {
                        $marking = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/markings/" . $marking . ".png");
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                        $marking->setImageBackgroundColor($color);
                        $marking->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
                        $base->compositeImage($marking, imagick::COMPOSITE_DEFAULT, 0, 0);
                    }
                }
            }
        }
    }

    // If extra layers: over markings
    if ($pet->layer_name1)
    {
        if ($pet->layer_pos1 == 0)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name1 . ".png");
            } else
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name1 . ".png");
            }

            if ($pet->layer_recolor1 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer1);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name2)
    {
        if ($pet->layer_pos2 == 0)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name2 . ".png");
            }

            else
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name2 . ".png");
            }

            if ($pet->layer_recolor2 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer2);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name3)
    {
        if ($pet->layer_pos3 == 0)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name3 . ".png");
            }

            else
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name3 . ".png");
            }

            if ($pet->layer_recolor3 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer3);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    if ($pet->layer_name4)
    {
        if ($pet->layer_pos4 == 0)
        {
            if ($age == 'adult')
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/" . $pet->layer_name4 . ".png");
            }

            else
            {
                $layer = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/" . $species . "/baby/" . $pet->layer_name4 . ".png");
            }

            if ($pet->layer_recolor4 == 1)
            {
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
                $layer->setImageBackgroundColor($layer4);
                $layer->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
            }

            $base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 0, 0);
        }
    }

    // Lion tail tuft
    if ($pet->name == 'lion')
    {
        if ($age == 'adult') {
            $tuft = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/lion/tailtuft.png");
        } else {
            $tuft = new \Imagick(dirname(dirname(dirname(dirname(__FILE__)))) . "/public_html/img/pets/lion/baby/tailtuft.png");
        }

        $tuft->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
        $tuft->setImageBackgroundColor($genderHex);
        $tuft->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);

        $base->compositeImage($tuft, imagick::COMPOSITE_DEFAULT, 0, 0);
    }

    $base->compositeImage($eye, imagick::COMPOSITE_DEFAULT, 0, 0);
    $base->compositeImage($shading, imagick::COMPOSITE_MULTIPLY, 0,0);
    $base->compositeImage($lights, imagick::COMPOSITE_OVERLAY, 0, 0);
    if ($pet->name == 'snake') {
        $base->compositeImage($scales, imagick::COMPOSITE_OVERLAY, 0, 0);
    }
    $base->compositeImage($lines, imagick::COMPOSITE_DEFAULT, 0, 0);

    if (isset($genderBase))
    {
        $base->compositeImage($genderBase, imagick::COMPOSITE_DEFAULT, 0, 0);
        $base->compositeImage($genderShading, imagick::COMPOSITE_MULTIPLY, 0,0);
        $base->compositeImage($genderLights, imagick::COMPOSITE_OVERLAY, 0, 0);
        $base->compositeImage($genderLines, imagick::COMPOSITE_DEFAULT, 0,0);
    }

    // Outputs final result
    $base->setImageColorSpace(imagick::COLORSPACE_SRGB);
    return $base;
}

0 个答案:

没有答案