开发者

How to add a new value in a class based on database field value?

Here is what I want to do. I have a class that I have created but I only want the certain parts of the class to show IF certain values are set in the database. What the class does is that it colors the base image and then places another image on top. Sometimes though there are multiple layers set in the database so the class has to adjust to accommodate.

DOES anyone know how or make any suggestions on how I can do this??

SO for example this class allow a base image to be colored and another to be scaled and placed on top:

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

    $artLayer = imagecreatefrompng($art); // Art Layer  
    $base = imagecreatefrompng($target); // Base Product
    $base_location = "bas开发者_开发问答e";

    $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

}

What I want to do add another value depending on the number of layers for the base image that is set in a database table. This is because there are images that have more than one layer of color.

so something like this:

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);

}


As far I can see, the simplest way would be to use an array of layers as parameters, so the method signature will be:

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

And in your my_layers_func's body, you should iterate on the $NEWLAYERS array applying the same transformations as you did on $NEWLAYER in layer_3 function.

This is a sample of how you could refactor your function:

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      
    }
        ....

Let me know if you need more explanations!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜