百科狗-知识改变命运!
--

imagerectangle() - gd函数(图像处理)

百变鹏仔11个月前 (11-21)阅读数 9#技术干货
文章标签坐标

imagerectangle()

(PHP 4, PHP 5, PHP 7)

imagerectangle() - gd函数(图像处理)

画一个矩形

说明

imagerectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $col): bool

imagerectangle()用$col颜色在$image图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。

Please pay attention if you want to draw pixel perfect rectangles: Since this function uses absolute values for the second coordinate points (instead of width and height), you might face a logical problem. PHP counts from 0. But a pixel at position 0,0 occupies already a 1x1 space. In the example above you have the following line:
imagerectangle($canvas, 50, 50, 150, 150, $pink);
If you don't pay attention, you might thing that the difference between the two coordinates is exactly 100 and assume that the drawn rectangle would have the dimension of 100 x 100 pixels too. But it would be 101 x 101, because PHP counts from 0 and imagerectangle() uses absolute coordinates for the second point too. A smaller example: A rectangle with coordinates 0,0 and 5,5 means 0,1,2,3,4,5 which are 6 pixels, not 5.
In addition to Corey's note, this is the kind of code he means. Note that I always draw an outer grid border, so drawing lines will always take 
1 + ceil((rows+cols)/2) actions. For a 20X20 grid, this means 21 actions, a 10X25 grid takes 19 Actions

have fun ;)
If you want an empty rectangle, I mean, just the borders, fill it first with the ImageFilledRectangle function with the background color and then draw it with this function.
Oh I don't know. He was on the right track.. 
More functions at http://www.sphoera.com
Matt, 
I agree that drawing 100 boxes for a 10x10 square is ludicrous. However, if we're going to talk about the best way to draw it in GD, you're still off. 
Since a rectangle will draw two vertical lines in one draw, we should use it to our advantage. You can draw 5 rectangles that have the tops and bottoms outside of the image, and there you have your ten rows. Draw 5 more who's sides are out of the image and you have your columns. We just drew a 10x10 (you could do 11x11) grid in 10 draw operations. 
:)
Lets not do it Mr Benson's way OK!
I'm sure if I had to draw a 10x10 grid on paper I wouldn't do it by drawing 100 individual squares, redrawing nearly half of the lines twice.
I'd probably do it by drawing 11 vertical lines and 11 horizontal lines.
function ImageGrid2(&$im,$startx,$starty,$width,$height,$xcols,$yrows,&$color) {
  $endy = $starty + $height * $yrows;
  for ( $x=0; $x 
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)