2009-12-11 77 views
0

我在這個星期寫代碼來動態地改變圖像的顏色。只要圖像是方形或矩形,下面的代碼完美地工作。然而,我有一個圓角的圖像(填充專色,角落外面的背景是白色的。)使用PHP GD庫轉換圖像的顏色/色調。抓住?它有圓角

有人可以告訴我,是否有簡單的方法使用imagecolorset對具有圓角的圖像着色函數(或任何其他的PHP方法)?

我只希望圖像的非白色區域被着色(如果你想知道,我不允許將白色應用於圖像)。

請注意,也許更好的方法是使用具有透明背景的PNG圖像(而不是我的gif圖像)。如果您認爲這是一種更好的方法,請告知。

下面是我的工作的功能...

function set_theme_color_header($hex) 
{ 
    $info = hexToRGB($hex); //calls a helper function which translates the hex to RGB 
    $img = imagecreatefromgif('header-template.gif'); //again, this could be a PNG image, but we always start with this image, then create a color copy 
    $color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]); 
    imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]); 
    imagegif($img, 'header.gif'); //only problem is that the imagecolorset function creates a messy fill at the corners 
} 

回答

0

GD不處理gif的很好。我建議用圓角創建一個png圖像,然後使用GD的imagefilter($ img,IMG_FILTER_COLORIZE,$ info [「red」],$ info [「green」],$ info [「blue」],$ alpha)函數。然後將圖像保存爲gif。

或者,使用php的imagick庫來字面地繪製圖像。 imagick比GD好上十億倍。記錄不完善,但...基本的東西不是太難。請參閱下面的tut鏈接。

搜索php.net for「imagick」查看所有功能。

imagick tutorial

0

我真的不不是你爲什麼這樣做,但也許看看phpTumb(及其演示here)。