我正在尝试制作一个函数,它接受一个 JPG 文件,添加一个网格覆盖,并在每个单元格中写入文本 A1、A2、A3 等等。
当前代码(下方)仅绘制网格,具有静态列/行大小。
问题 1) 如何在每个单元格中添加坐标作为文本?例如,行是字母,列是数字。所以第一行是 A1、A2、A3 ...,下一行是 B1、B2、B3。
问题 2)如何修改它,以便指定我想要的行数和列数,并且它会自动相应地调整列/行的大小以适应输入图像的尺寸?
function draw_grid(&$img, $x0, $y0, $width, $height, $cols, $rows, $color) {
imagesetthickness($img, 5);
//draw outer border
imagerectangle($img, $x0, $y0, $x0+$width*$cols, $y0+$height*$rows, $color);
//first draw horizontal
$x1 = $x0;
$x2 = $x0 + $cols*$width;
for ($n=0; $n<ceil($rows/2); $n++) {
$y1 = $y0 + 2*$n*$height;
$y2 = $y0 + (2*$n+1)*$height;
imagerectangle($img, $x1,$y1,$x2,$y2, $color);
}
//then draw vertical
$y1 = $y0;
$y2 = $y0 + $rows*$height;
for ($n=0; $n<ceil($cols/2); $n++) {
$x1 = $x0 + 2*$n*$width;
$x2 = $x0 + (2*$n+1)*$width;
imagerectangle($img, $x1,$y1,$x2,$y2, $color);
}
}
$imgpath = "foto/306/306.jpg";
$img = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$width = $size[0];
$height = $size[1];
$red = imagecolorallocate($img, 255, 0, 0);
draw_grid($img, 0,0, $width /10 , $height /10 ,20,10,$red);
header("Content-type: image/jpg");
imagejpeg($img);
imagedestroy($img);
回首忆惘然
叮当猫咪