php 男女转为0 1,通过php将“0”和“1”的字符串转换为图像

这是我的示例代码,希望这可以帮助你^^

$size = 50; /*the square size*/

$map = "0101\n1010\n0101\n1010"; /*your image map*/

$totalRow = 0;

$totalCol = 0;

$rows = explode("\n", $map); /* split map by '\n' as the requirement stated*/

$totalRow = count($rows); /* get total row */

/**

* Convert map to array make it loopable

*/

$map = [];

foreach ($rows as $row)

{

$cols = str_split($row); /* use str_split to split each characters */

$totalCol = $totalCol < count($cols) ? count($cols) : $totalCol; /* get biggest cols user to calculate image width*/

$map[] = $cols; /* push to map*/

}

/**

* Create image

*/

$width = $size * $totalCol; /* width = size * biggest cols*/

$height = $size * $totalRow; /* height = size * total rows*/

$im = imagecreatetruecolor($width, $height); /* create image */

/**

* Colors

*/

$white = imagecolorallocate($im, 255, 255, 255);

$black = imagecolorallocate($im, 0, 0, 0);

/**

* Build square

*/

$x = 0; /* x pointer*/

$y = 0; /* y pointer*/

foreach ($map as $mapRow) /* rows loop*/

{

foreach ($mapRow as $square) /* cols loop */

{

$color = $square == 1 ? $black : $white; /* black if the code is '1'*/

// create square

imagefilledrectangle($im, $x, $y, $x + $size, $y + $size, $color);

$x += $size; /* increase x pointer*/

}

$x = 0; /* reset x pointer */

$y += $size; /* increase y pointer*/

}

/* png header */

header('Content-Type: image/png');

/* show image */

imagepng($im);

imagedestroy($im);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值