-6
A
回答
3
创建使用正常的GD图像功能在PHP梯度。该函数使用HTML十六进制代码作为颜色值,然后将它们转换为具有RGB值的数组。
function image_gradientrect($img,$x,$y,$x1,$y1,$start,$end) {
if($x > $x1 || $y > $y1) {
return false;
}
$s = array(
hexdec(substr($start,0,2)),
hexdec(substr($start,2,2)),
hexdec(substr($start,4,2))
);
$e = array(
hexdec(substr($end,0,2)),
hexdec(substr($end,2,2)),
hexdec(substr($end,4,2))
);
$steps = $y1 - $y;
for($i = 0; $i < $steps; $i++) {
$r = $s[0] - ((($s[0]-$e[0])/$steps)*$i);
$g = $s[1] - ((($s[1]-$e[1])/$steps)*$i);
$b = $s[2] - ((($s[2]-$e[2])/$steps)*$i);
$color = imagecolorallocate($img,$r,$g,$b);
imagefilledrectangle($img,$x,$y+$i,$x1,$y+$i+1,$color);
}
return true;
}
$imgWidth = 300;
$imgHeight = 150;
$img = imagecreatetruecolor($imgWidth,$imgHeight);
image_gradientrect($img,0,0,$imgWidth,$imgHeight,'ff0000','0000ff');
/* Show In Browser as Image */
header('Content-Type: image/png');
imagepng($img);
/* Save as a File */
imagepng($img,'save.png');
/* Some Cleanup */
imagedestroy($img);
只要改变高度& widht和颜色在上面的代码中,它会产生矩形图像。
-1
bool imagefilledrectangle (resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color)
+0
这将很难画出一个渐变。 –
1
此源码ref。到Christoper Kramer的PHP文档页面。 试试吧。 PHP中没有“内置”函数来绘制它。
function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) {
/*
Generates a gradient image
Author: Christopher Kramer
Parameters:
w: width in px
h: height in px
c: color-array with 4 elements:
$c[0]: top left color
$c[1]: top right color
$c[2]: bottom left color
$c[3]: bottom right color
if $hex is true (default), colors are hex-strings like '#FFFFFF' (NOT '#FFF')
if $hex is false, a color is an array of 3 elements which are the rgb-values, e.g.:
$c[0]=array(0,255,255);
*/
$im=imagecreatetruecolor($w,$h);
if($hex) { // convert hex-values to rgb
for($i=0;$i<=3;$i++) {
$c[$i]=hex2rgb($c[$i]);
}
}
$rgb=$c[0]; // start with top left color
for($x=0;$x<=$w;$x++) { // loop columns
for($y=0;$y<=$h;$y++) { // loop rows
// set pixel color
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($im,$x-1,$y-1,$col);
// calculate new color
for($i=0;$i<=2;$i++) {
$rgb[$i]=
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
$c[1][$i]*($x *($h-$y)/($w*$h)) +
$c[2][$i]*(($w-$x)*$y /($w*$h)) +
$c[3][$i]*($x *$y /($w*$h));
}
}
}
return $im;
}
function hex2rgb($hex)
{
$rgb[0]=hexdec(substr($hex,1,2));
$rgb[1]=hexdec(substr($hex,3,2));
$rgb[2]=hexdec(substr($hex,5,2));
return($rgb);
}
// usage example
$image=gradient(300, 300, array('#000000', '#FFFFFF', '#FF0000', '#0000FF'));
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
0
如果我理解正确,您想在网页上绘制渐变。 PHP是一个服务器端脚本,与Presetation无关。所以HTML和JS可以帮助你。创建一个画布,然后
HTML
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
的Javascript
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var grd=ctx.createLinearGradient(0,0,170,0);
grd.addColorStop(0,"black");
grd.addColorStop(1,"white");
ctx.fillStyle=grd;
ctx.fillRect(20,20,150,100);
+0
谢谢,但我真的想用PHP绘制图像。 – tholu
相关问题
- 1. 如何绘制圆形渐变?
- 2. 矩形渐变
- 3. iphone在多边形内绘制渐变
- 4. 在画布上绘制渐变形状
- 5. 渐变色圆形矩形
- 6. 如何在Paper.js中绘制矩形?
- 7. 如何在gwt-openlayers中绘制矩形?
- 8. 如何在JPanel中绘制矩形
- 9. 如何在python中绘制矩形?
- 10. 如何在flex 4.5.1中绘制矩形?
- 11. 画布绘制用线性渐变填充圆角矩形
- 12. 绘制渐变矩形的有效方法
- 13. 绘制渐变
- 14. 如何绘制矩形?
- 15. 如何绘制矩形?
- 16. 在iOS中绘制矩形
- 17. 在MKMapView中绘制矩形
- 18. 在Java中绘制矩形
- 19. 绘制矩形在Java中
- 20. 在c中绘制矩形#
- 21. 在.NET中绘制矩形
- 22. 在ViewPager中绘制矩形
- 23. 在图框上绘制矩形 - 如何限制矩形区域?
- 24. 我将如何渐变几个单独绘制的矩形的颜色?
- 25. iPhone SDK:如何使用半透明渐变和阴影绘制圆角矩形
- 26. 如何绘制渐变iOS中
- 27. 绘制矩形
- 28. 在IOS中同一视图上绘制2个带有渐变的矩形(swift)
- 29. 如何在Android中使用渐变绘制渐进环?
- 30. 矩形渐变填充
Stack Overflow不是Google。请尝试先做一些自己的研究。坦率地说,现在有一位有几百名代表的人应该知道这一点。 – GordonM