原文在:

http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/

使用范例如下,基本函数封装在gd-gradient-fill.php中,

require_once('/path/to/gd-gradient-fill.php');

$image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);

gd_gradient_fill有5个必要参数和一个可选参数:

integer $width

Width of the image

integer $height

Height of the image

string $direction

The shape or direction of the gradient, which can be any of : vertical, horizontal, ellipse, ellipse2, circle, circle2, rectangle, diamond.

string $startcolor

Gradient start color, 3 or 6 digit hexadecimal ("#ff0" or "#dd4578")

string $endcolor

Gradient end color

Optional : $step

Breaks the gradient smooth blending

下面是一些实例,颜色从#101040渐变到#a1a1ff, 渐变方向不同, 鼠标移动到图片上可看到提示信息.

另外, 这个函数处理非矩形图片一样可以.

附函数源码如下:

/*

Script Name: GD Gradient Fill

Script URI: http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/

Description: Creates a gradient fill of any shape (rectangle, ellipse, vertical, horizontal, diamond)

Author: Ozh

Version: 1.1

Author URI: http://planetozh.com/

*/

/* Release history :

* 1.1

*- changed : more nicely packaged as a class

*- fixed : not displaying proper gradient colors with image dimension greater than 255 (because of a limitation in imagecolorallocate)

*- added : optional parameter 'step', more options for 'direction'

* 1.0

*- initial release

*/

/* Usage :

*

* require_once('/path/to/gd-gradient-fill.php');

* $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);

*

* Parameters :

*- width and height : integers, dimesions of your image.

*- direction : string, shape of the gradient.

* Can be : vertical, horizontal, rectangle (or square), ellipse, ellipse2, circle, circle2, diamond.

*- startcolor : string, start color in 3 or 6 digits hexadecimal.

*- endcolor : string, end color in 3 or 6 digits hexadecimal.

*- step : integer, optional, default to 0. Step that breaks the smooth blending effect.

* Returns a resource identifier.

*

* Examples :

*

* 1.

* require_once('/home/ozh/www/includes/gd-gradient-fill.php');

* $image = new gd_gradient_fill(200,200,'horizontal','#fff','#f00');

*

* 2.

* require_once('c:/iis/inet/include/gd-gradient-fill.php');

* $myimg = new gd_gradient_fill(80,20,'diamond','#ff0010','#303060');

*

*/

// Test it :

// $image = new gd_gradient_fill(400,200,'ellipse','#f00','#000',0);

class gd_gradient_fill {

// Constructor. Creates, fills and returns an image

function gd_gradient_fill($w,$h,$d,$s,$e,$step=0) {

$this->width = $w;

$this->height = $h;

$this->direction = $d;

$this->startcolor = $s;

$this->endcolor = $e;

$this->step = intval(abs($step));

// Attempt to create a blank image in true colors, or a new palette based image if this fails

if (function_exists('imagecreatetruecolor')) {

$this->image = imagecreatetruecolor($this->width,$this->height);

} elseif (function_exists('imagecreate')) {

$this->image = imagecreate($this->width,$this->height);

} else {

die('Unable to create an image');

}

// Fill it

$this->fill($this->image,$this->direction,$this->startcolor,$this->endcolor);

// Show it

$this->display($this->image);

// Return it

return $this->image;

}

// Displays the image with a portable function that works with any file type

// depending on your server software configuration

function display ($im) {

if (function_exists("imagepng")) {

header("Content-type: image/png");

imagepng($im);

}

elseif (function_exists("imagegif")) {

header("Content-type: image/gif");

imagegif($im);

}

elseif (function_exists("imagejpeg")) {

header("Content-type: image/jpeg");

imagejpeg($im, "", 0.5);

}

elseif (function_exists("imagewbmp")) {

header("Content-type: image/vnd.wap.wbmp");

imagewbmp($im);

} else {

die("Doh ! No graphical functions on this server ?");

}

return true;

}

// The main function that draws the gradient

function fill($im,$direction,$start,$end) {

switch($direction) {

case 'horizontal':

$line_numbers = imagesx($im);

$line_width = imagesy($im);

list($r1,$g1,$b1) = $this->hex2rgb($start);

list($r2,$g2,$b2) = $this->hex2rgb($end);

break;

case 'vertical':

$line_numbers = imagesy($im);

$line_width = imagesx($im);

list($r1,$g1,$b1) = $this->hex2rgb($start);

list($r2,$g2,$b2) = $this->hex2rgb($end);

break;

case 'ellipse':

$width = imagesx($im);

$height = imagesy($im);

$rh=$height>$width?1:$width/$height;

$rw=$width>$height?1:$height/$width;

$line_numbers = min($width,$height);

$center_x = $width/2;

$center_y = $height/2;

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));

break;

case 'ellipse2':

$width = imagesx($im);

$height = imagesy($im);

$rh=$height>$width?1:$width/$height;

$rw=$width>$height?1:$height/$width;

$line_numbers = sqrt(pow($width,2)+pow($height,2));

$center_x = $width/2;

$center_y = $height/2;

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

break;

case 'circle':

$width = imagesx($im);

$height = imagesy($im);

$line_numbers = sqrt(pow($width,2)+pow($height,2));

$center_x = $width/2;

$center_y = $height/2;

$rh = $rw = 1;

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

break;

case 'circle2':

$width = imagesx($im);

$height = imagesy($im);

$line_numbers = min($width,$height);

$center_x = $width/2;

$center_y = $height/2;

$rh = $rw = 1;

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));

break;

case 'square':

case 'rectangle':

$width = imagesx($im);

$height = imagesy($im);

$line_numbers = max($width,$height)/2;

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

break;

case 'diamond':

list($r1,$g1,$b1) = $this->hex2rgb($end);

list($r2,$g2,$b2) = $this->hex2rgb($start);

$width = imagesx($im);

$height = imagesy($im);

$rh=$height>$width?1:$width/$height;

$rw=$width>$height?1:$height/$width;

$line_numbers = min($width,$height);

break;

default:

}

for ( $i = 0; $i < $line_numbers; $i=$i+1+$this->step ) {

// old values :

$old_r=$r;

$old_g=$g;

$old_b=$b;

// new values :

$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;

$g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;

$b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;

// if new values are really new ones, allocate a new color, otherwise reuse previous color.

// There's a "feature" in imagecolorallocate that makes this function

// always returns '-1' after 255 colors have been allocated in an image that was created with

// imagecreate (everything works fine with imagecreatetruecolor)

if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")

$fill = imagecolorallocate( $im, $r, $g, $b );

switch($direction) {

case 'vertical':

imagefilledrectangle($im, 0, $i, $line_width, $i+$this->step, $fill);

break;

case 'horizontal':

imagefilledrectangle( $im, $i, 0, $i+$this->step, $line_width, $fill );

break;

case 'ellipse':

case 'ellipse2':

case 'circle':

case 'circle2':

imagefilledellipse ($im,$center_x, $center_y, ($line_numbers-$i)*$rh, ($line_numbers-$i)*$rw,$fill);

break;

case 'square':

case 'rectangle':

imagefilledrectangle ($im,$i*$width/$height,$i*$height/$width,$width-($i*$width/$height), $height-($i*$height/$width),$fill);

break;

case 'diamond':

imagefilledpolygon($im, array (

$width/2, $i*$rw-0.5*$height,

$i*$rh-0.5*$width, $height/2,

$width/2,1.5*$height-$i*$rw,

1.5*$width-$i*$rh, $height/2 ), 4, $fill);

break;

default:

}

}

}

// #ff00ff -> array(255,0,255) or #f0f -> array(255,0,255)

function hex2rgb($color) {

$color = str_replace('#','',$color);

$s = strlen($color) / 3;

$rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));

$rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));

$rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));

return $rgb;

}

}

?>

iefreer

php渐变,PHP绘制渐变颜色图片相关推荐

  1. gradient设置上下渐变_iOS 绘制渐变·实例篇

    原标题:iOS 绘制渐变·实例篇 作者丨QiShare https://www.jianshu.com/p/3fbea6b31dc6 1.渐变色进度条 渐变色进度条实现效果 渐变色进度条实现代码 // ...

  2. H5canvas(渐变,绘制图片和视频,画布变换,制作马赛克)

    绘制渐变 前言 绘制一条线段 让这个线段的宽度是10 颜色是蓝色 var canvas = document.getElementById('myCanvas'); var cxt = canvas. ...

  3. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  4. SVG绘制渐变颜色文字

    H5提供了很多有意思的新特性,工作中用的比较少,但是这仍然不能阻碍我们对它的兴趣,先给大家上个图,如果要用CSS来写这个效果,如何才能实现呢? 怎么样?这个文字效果不好实现吧,以上这个效果其实是用H5 ...

  5. AI(角度渐变icon绘制步骤)

    AI角度渐变icon绘制步骤(作者欧日鑫 撰写时间:2019年1月19日) 1.找到椭圆工具绘制椭圆,打开显示智能参考线和对齐点,准确找到中心点,直接给图片导入到IA里面给吸管工具吸颜色,外面的椭圆有 ...

  6. Canvas 渐变 图像组合效果 颜色翻转

    // canvas 简单例子 var canvas = document.getElementById('canvas'); if (canvas.getContext) {var context = ...

  7. iOS:quartz2D绘图(绘制渐变图形)

    quartzD可以用来绘制渐变图形,即图形向外或向内发散,会变得越来越模糊. 渐变分为线性渐变和径向渐变,所谓线性渐变,就是图形以线的方式发散,发散后一般呈现出矩形的样子:而径向渐变,就是以半径的大小 ...

  8. android canvas画渐变背景,View绘制系列(13)-Canvas渐变属性绘制

    Canvas渐变属性绘制 五颜六色,七彩缤纷.有时候我们的UI设计稿也极尽色彩之能,比如下图这样: 这种渐变效果我们能画吗?不得不说,Android系统的基础构架还是很强大的,我们可以使用Linear ...

  9. CSS 背景色 背景图片 渐变背景 - 径向渐变 background-image:radial-gradient()

    radial-gradient() 用来生成径向渐变的图片 基础语法 background-image: radial-gradient(shape extent at positionX posit ...

最新文章

  1. 在阿里写了8年代码后,我才明白这些道理
  2. 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
  3. 操作系统--系统调用
  4. RabbitMQ消息手动应答生产者
  5. python 中的__getattr__和__setattr__
  6. 华为电视鸿蒙系统好用吗,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可 !【手机吧】_百度贴吧...
  7. java 堆大小_适当的Java堆大小的5个技巧
  8. python调用系统命令_linux里面python调用系统命令问题
  9. 「ECharts」交互 API (echarts、echartsInstance)
  10. java购买同一件商品时加锁_java中CAS的ABA问题思考和整理(不看后悔系列)
  11. 敏捷开发“松结对编程”系列之七:问题集之一
  12. OpenAI 发布模型实现自动定理证明,妈妈再也不用担心我的数学?
  13. 公众服务常用电话号码大全
  14. 用友超客:社交化业务就是要化繁为简
  15. 设计模式系列--Singleton
  16. 【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之一
  17. 博弈的意思_身处博弈时代,我们更要读些历史
  18. FAQ01【Hadoop】:Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
  19. wechat sdk java_使用java集成微信支付sdk。
  20. 随机变量分布函数:相关习题解答

热门文章

  1. iPhone12隔空投送功能怎么开启和关闭
  2. 常用的MRP元素(MRP elements)缩写如下:
  3. html5编写圆柱,three.js实现圆柱体
  4. 计算机科学与技术分类号查询,计算机科学与技术专业论文
  5. mysql sd5加密语句_CAS原理与配置
  6. NLP 语义匹配:业务场景、数据集及比赛
  7. 网球服装的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  8. gitlab服务器安装及汉化配置
  9. 教你打造股市晴雨表——通过LSTM神经网络预测股市
  10. 努比亚Z7 mini刷机教程_recovery卡刷机教程