2011-07-09 34 views
0

这是我想要做的。我有一个我已经创建的类,但我只希望该类的某些部分显示如果在数据库中设置了某些值。该课程所做的是为基础图像着色,然后将另一图像置于顶部。有时虽然在数据库中设置了多个图层,所以类必须进行调整才能适应。如何根据数据库字段值在类中添加新值?

有谁知道如何做出任何建议或如何做到这一点?

因此,例如,该类允许基础图像是彩色的,另一个将要缩放并放置在顶部:

public function layers2 ($target, $art, $newcopy, $red, $blue, $green) { 

    $artLayer = imagecreatefrompng($art); // Art Layer 
    $base = imagecreatefrompng($target); // Base Product 
    $base_location = "base"; 

    $img = imagecreatefrompng($base); 

    $width3 = imagesx($artLayer); // artLayer 
    $height3 = imagesy($artLayer); // artLayer 

    //COLOR THE IMAGE 
    imagefilter($base, IMG_FILTER_COLORIZE, $red, $green, $blue, 1); //the product 

    imagecopyresampled($base,$artLayer,350, 150, 0, 0, 300, 300, imagesx($artLayer), imagesy($artLayer));  // rotate image 

    // save the alpha 
    imagesavealpha($base,true); 
    // Output final product 
    imagepng($base, $newcopy); //OUTPUT IMAGE 

}

我想要做的添加取决于号的另一个价值是什么用于在数据库表中设置的基本图像的图层。这是因为有图像具有多个颜色层。

所以是这样的:

public function layer_3($target, $NEWLAYER, $art, $newcopy, $r, $b, $g) { 

    $artLayer = imagecreatefrompng($art); // Art Layer  
    $colorLayer1 = imagecreatefrompng($NEWLAYER); // NEW LAYER  
    $base = imagecreatefrompng($target); // Base Product 
    $base_location = "base"; 

    $img = imagecreatefrompng($base); 

    // NEW LAYER 
    $width = imagesx($colorLayer1); // colorLayer1 
    $height = imagesy($colorLayer1); // colorLayer1 

    $width3 = imagesx($artLayer); // artLayer 
    $height3 = imagesy($artLayer); // artLayer 

    $img=imagecreatetruecolor($width, $height); // NEW LAYER 

    imagealphablending($img, true); // NEW LAYER 


    $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127); 
    imagefill($img, 0, 0, $transparent); 

    //COLOR THE IMAGE 
    imagefilter($base, IMG_FILTER_COLORIZE, $r, $b, $g, 1); //the base 
    imagecopyresampled($img,$base,1,1,0,0, 1000, 1000, imagesx($base), imagesy($base));    
    imagecopyresampled($img,$colorLayer1,1,1,0,0, 1000, 1000, imagesx($colorLayer1), imagesy($colorLayer1)); //NEW LAYER  
    imagecopyresampled($img,$artLayer,300, 200, 0, 0, 350, 350, imagesx($artLayer), imagesy($artLayer)); 


    imagealphablending($img, false); 
    imagesavealpha($img,true); 
    imagepng($img, $newcopy); 

}

回答

0

据我所看到的,最简单的方法是使用图层作为参数数组,因此该方法的签名是:

public function my_layers_func($target, $NEWLAYERS = array(), $art, $newcopy, $r, $b, $g) 

而在my_layers_func的正文中,您应该迭代$ NEWLAYERS数组,应用与您在layer_3函数中的$ NEWLAYER上所做的相同的转换。

这是你如何可以重构你的函数的例子:

public function my_layers_func($target, $newlayers = array(), $art, $newcopy, $r, $b, $g) 
     $artLayer = imagecreatefrompng($art); // Art Layer 
    $colorLayers = array(); 
    foreach($newlayers as $newlayer){ 
     $colorLayers[] = imagecreatefrompng($newlayer); // NEW LAYER  
    } 
     .... 

让我知道如果你需要更多的解释!

+0

非常感谢你,但我不得不承认我不太清楚如何去做你刚刚提到的事情。有没有办法可以详细说明? – GGcupie

+0

我用一个重构的例子编辑了我的答案... –

+0

谢谢Fabrizio! – GGcupie