簡體   English   中英

使用PHP GD庫轉換圖像顏色/色相。 趕上? 它有圓角

[英]Convert an image color/hue using PHP GD library. The Catch? It has rounded corners

我本周正在編寫代碼以動態更改圖像的顏色。 只要圖像是正方形或矩形,下面的代碼就可以正常工作。 但是,我有一個帶有圓角的圖像(填充有專色,而拐角外部的背景為白色。)

有人可以告訴我是否有一種簡單的方法可以使用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
}

我真的不不是你為什么這樣做,但也許看看phpTumb (和它的演示在這里 )。

GD不能很好地處理gif。 我建議創建帶有圓角的png圖像,然后使用GD的imagefilter($ img,IMG_FILTER_COLORIZE,$ info [“ red”],$ info [“ green”],$ info [“ blue”],$ alpha)函數。 然后將圖像另存為gif。

或者,使用php的imagick庫從字面上繪制圖像。 imagick比GD好十億倍。 記錄不足,但是...基本內容並不難。 請參閱下面的tut鏈接。

在php.net上搜索“ imagick”以查看所有功能。

imagick教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM