imagecolorallocate() - gd函数(图像处理)
imagecolorallocate()
(PHP 4, PHP 5, PHP 7)
为一幅图像分配颜色
说明
imagecolorallocate(resource $image,int $red,int $green,int $blue): intimagecolorallocate()返回一个标识符,代表了由给定的RGB成分组成的颜色。$red,$green和$blue分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate()必须被调用以创建每一种用在$image所代表的图像中的颜色。
Note:第一次对imagecolorallocate()的调用会给基于调色板的图像填充背景色,即用imagecreate()建立的图像。
imagecolorallocate()例子
如果分配失败则返回-1。
参见imagecolorallocatealpha()和imagecolordeallocate()。
Note that you can only assign 255 colors to any image palette. If you try assigning more, imagecolorallocate() will fail. If, for example, you are randomly allocating colors, it will be wise to check if you have used up all of the colors possible. You can use imagecolorclosest() to get the closest assigned color: Also, imagecolorallocate() will assign a new color EVERY time the function is called, even if the color already exists in the palette: So here, imagecolorexact() is useful: And, for nerdy-ness sake, we can put the two ideas together: Or as a function:
this might help someone, how to allocate an color from an html color-definition:
When working on an existant GIF images, if the number of different colours has reached the limits of the GIF format, imagecolorallocate will not use to the colour you ask her within the parameters, she will apply black ! That's a problem when generating images "on-the-fly" with many manipulations, from a GIF image. To go round the problem, you have to convert the GIF image into a PNG one, and then you can work on the PNG and everything will be ok. For example :
Also, when you need more then 256 colors, use imagecreatetruecolor function. With this function you can use unlimited number of colors.
Lots of hsv2rgb commentary but no working example, so here's mine:
This works! A Black-Image with vertical centered white Aliased Arial-Text and same left and right margin - used for Menu-Buttons.
If you even in a situation where it's not allocating the color you want it could be because of your images color allocation table. GIF and 8-bit PNG images are very susceptible to this. If your using an GIF and PNG try dropping a color from the table, should let you allocate another.
Another solution to color limitation issues when creating gradients. This file takes width (px) and left and right colors (hex) and makes a gradient while only allocating 250 colors. example: gradient.php?width=640&left=000000&right=FF0000 Makes a 640px-wide image that fades from black to red.
Actually, you can't allocate more than 256 colours for an paletted image (ImageCreate). Use ImageCreateTrueColor instead. For it to work, you need libgd version 2 support in php though.
Another more general variation on the theme using the same naming conventions as the hexdec and dechex built-in functions ... Prototype: array hexrgb ( string hex_string ) Returns: An associative array of the RGB components specified in hex_string. hexrgb() example: Output is: Array ( [red] => 170 [green] => 187 [blue] => 204 ) Implementation: The output of hexdec can then be passed to imagecolorallocate and manipulated as required.
Here's a very simple and very effective code to change a HEX color to RGB.
This nifty function will produce the negative of a given image!
hsl to RGB (not yet optimized but it function)
you have VERY limited space for color indexes (255 indexes on my system, with over 10 GB ram available, cli, no memory limit), when there is no more indexes available, imagecolorallocate will return false. when you create 2x indexes with the same r/g/b, you waste this very limited space. the function below should never fail, AND never waste any color index space. if there's already an index with the rgb, it will return the existing index, else it will try allocate 1, in the event that allocation fail (presumably because all index space is used up already), it will return the closest match to what you requested, and warn you via $couldFindExact. function myimagecolorallocate($gd,int $red,int $green,int $blue,bool &$couldFindExact=null): int{ $ret=imagecolorexact($gd, $red, $green, $blue); if($ret===-1){ $ret=imagecolorallocate($gd, $red, $green, $blue); if($ret===false){ $couldFindExact=false;//out of color indexes (255 index by default..wish i knew why) $ret=imagecolorclosest($gd, $red, $green, $blue); } else { $couldFindExact=true; } } else { $couldFindExact=true; } return $ret; }
When you are using truecolor images, you can also use bitwise operations to generate the color: This is identical to the imagecolorallocate() function, in truecolor images!
This will let you tint an image to any specific color. The blacks of the source image become your specified color, and the whites remain white. Works best for colorizing greyscale images.
2 functions to convert from HSV colorspace (hue/saturation/brightness) to RGB (red/green/blue) colorspace and back. [1] - EDITOR NOTE: THIS IS A FIX FROM "hc at hob(removethis)soft dot net".
Some of you maybe want to use HSV color model for drawing color selectors and circles:
I was unable to get any of the posted methods for converting colour to grayscale to work. The problem appears to be the way gd creates images from jpeg inconsistently over various versions. Eventually I wrote my own that works for me - this approach allocates the 256 color pallete first. You can also play with separate $r, $g, $b variables before using imagecolorallocate in order to tone or tint the image. go on using $im as your image, it is now grayscale ....
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)