2011-07-09 30 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