我希望使用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);
?>
答案 0 :(得分:1)