gpt4 book ai didi

PHP GD 库如何从颜色数组生成带有 imagesetpixel 的矩阵

转载 作者:行者123 更新时间:2023-12-04 23:33:45 26 4
gpt4 key购买 nike

我想用 php gd 库生成图片,但我有一些错误,有时有效,有时无效。不会生成完整图像,只会生成部分图像。有时在对角线上。

我有一个字符串,例如“color_color_color”等,我将其转换为数组以创建像素矩阵,然后使用循环解析该数组并创建具有设置像素颜色的图片。然后我生成一个透明颜色,保存在 png 中。

有时可以,但不会生成第一行和第一列。

<?php
//tell at the browser that is a picture
header("Content-Type: image/png");

$size = $_POST['size'];

//size of the picture
$image = imagecreatetruecolor($size, $size);


//color background example : "grayOne_black_grayOne_white_" ect...
$first_array = explode("_", $_POST['field']);


$white = imagecolorallocate($image, 255, 255, 255);
$grayOne = imagecolorallocate($image, 225, 225, 225);
$grayTwo = imagecolorallocate($image, 200, 200, 200);
$grayThree = imagecolorallocate($image, 175, 175, 175);
$grayFour = imagecolorallocate($image, 150, 150,150);
$grayFive = imagecolorallocate($image, 125, 125, 125);
$graySix = imagecolorallocate($image, 100, 100, 100);
$graySeven = imagecolorallocate($image, 75, 75, 75);
$grayHeight = imagecolorallocate($image, 50, 50, 50);
$grayNine = imagecolorallocate($image, 25, 25, 25);
$black = imagecolorallocate($image, 0, 0, 0);

// a variable to parse the array
$a = 0;

// a loop ImageSetPixel($image, $y, $x, $color);

for($x = 0; $x < $size; $x++){
for($y = 0; $y < $size; $y++){

switch($first_array[$a]){
case "white" : $color = $white;
break;
case "grayOne" : $color = $grayOne;
break;
case "grayTwo" : $color = $grayTwo;
break;
case "grayThree" : $color = $grayThree;
break;
case "grayFour" : $color = $grayFour;
break;
case "grayFive" : $color = $grayFive;
break;
case "graySix" : $color = $graySix;
break;
case "graySeven" : $color = $graySeven;
break;
case "grayHeight" : $color = $grayHeight;
break;
case "grayNine" : $color = $grayNine;
break;
case "black" : $color = $black ;
break;
}
$a++;
ImageSetPixel($image, $y, $x, $color);
}
}

//set black as transparent
ImageColorTransparent($image, $black);

//save the picture with a number
ImagePng($image, "../image/pictureb".$_POST['numb'].".png");

imagedestroy($image);
?>

最佳答案

关于PHP GD 库如何从颜色数组生成带有 imagesetpixel 的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34063026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com