我需要用PHP和GD创建这样的东西:
三个可见面将是同一图像的三个部分,其中我知道坐标。
我认为这可以通过图像转换和一些数学来完成。
立方体的旋转将始终相同。
我不需要"中风"像那张照片中的边缘和面部照明,我只需要一个无阴影的"立方体。
最后,结果应该是一个alpha透明的PNG。
P.S。我的主机上只有GD,我无法访问ImageMagick。
答案 0 :(得分:2)
<?php
$size=100;
$first_poligon = array(
0, $size/4, // Point 1 (x, y) ----> 0,20
$size/2, $size/2, // Point 2 (x, y) ----> 50,50
$size/2, $size, // Point 3 (x, y) ----> 50,100
0, ($size/4)*3, // Point 4 (x, y) ----> 0,60
);
$second_poligon = array(
0, $size/4, // Point 1 (x, y) ----> 0,33
$size/2, 0, // Point 2 (x, y) ----> 50,0
$size, $size/4, // Point 3 (x, y) ----> 100,20
$size/2, $size/2, // Point 4 (x, y) ----> 50,50
);
$third_poligon = array(
$size, $size/4, // Point 1 (x, y) ----> 100,20
$size/2, $size/2, // Point 2 (x, y) ----> 50,50
$size/2, $size, // Point 3 (x, y) ----> 50,100
$size, ($size/4)*3, // Point 4 (x, y) ----> 100,60
);
$im = imagecreatetruecolor($size, $size);
$fondo = imagecolorallocate($im, 51, 0, 0);
imagefilledrectangle($im, 0, 0, $size, $size, $fondo);
$blue = imagecolorallocate($im, 0, 0, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledpolygon($im, $first_poligon, 4, $blue);
imagefilledpolygon($im, $second_poligon, 4, $white);
imagefilledpolygon($im, $third_poligon, 4, $red);
imagepng($im, './image.png');
imagedestroy($im);
?>
<img src="image.png" >
图片结果:
答案 1 :(得分:1)
从 kraysak 的回答开始,执行以下操作:
为了使用图像而不是颜色,请替换这些行:
$blue = imagecolorallocate($im, 0, 0, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
和
imagefilledpolygon($im, $first_poligon, 4, $blue);
imagefilledpolygon($im, $second_poligon, 4, $white);
imagefilledpolygon($im, $third_poligon, 4, $red);
使用类似的东西(此示例仅适用于三个面中的一个):
对于PNG:
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('bogus.image');
而不是:
$blue = imagecolorallocate($im, 0, 0, 255)
您将使用:
$cube_face_1 = imagepng($img);
而不是:
imagefilledpolygon($im, $first_poligon, 4, $blue);
您将使用:
imagefilledpolygon($im, $first_poligon, 4, $cube_face_1);
来源:http://php.net/manual/en/book.image.php
在上面的链接中,您可以找到有关gif / jpeg图像的详细信息。我的演示是针对png图像的。
答案 2 :(得分:0)
我不能让这个问题打败我......(我也想帮助你,但不是那么多:P) 所以,我制作了这个小问题,解决了近70%的问题:
<?php
$size=200;
$im= imagecreatetruecolor($size,$size);
$orige = imagecreatefrompng("test2.png");
$orige2 = imagecreatefrompng("tesst3.png");
$limit=0;
$newlimit=0;
for($y=$size/4;$y<=$size;$y++){
for($x=0;$x<=$size/2;$x++){
if($y>($size/4)*3){
if( $x>=$newlimit){
copy_pixel($im,$orige,$x,$y,NULL); //copy left image
copy_pixel($im,$orige2,$x,$y,$size-$x); //copy rightimage
}
}
else{
copy_pixel($im,$orige,$x,$y,NULL); //copy left image
copy_pixel($im,$orige2,$x,$y,$size-$x); //copy rightimage
}
if($x==$limit and $limit<= $size/2){ $limit=$limit+2; break;}
}
if($y>=($size/4)*3)$newlimit=$newlimit+2;
}
imagepng($im, "n.png");
function copy_pixel($im,&$orige,$x,$y,$newx){
if($newx==NULL) $newx=$x;
$rgb = imagecolorat($orige, $x, $y);
$color = imagecolorsforindex($orige, $rgb);
$red=preg_replace("/[^0-9]/","",$color["red"]);
$green=preg_replace("/[^0-9]/","",$color["green"]);
$blue=preg_replace("/[^0-9]/","",$color["blue"]);
$color_to_paste = imagecolorallocate($im, $red, $green, $blue);
imagesetpixel($im,$newx, $y, $color_to_paste);
}
?>
<img src="n.png" >
,结果: 右边是前面代码生成的图像。