问题
I need to create something like this with PHP and GD:

The three visible faces will be three parts of the same image, of which I know the coordinates.
I suppose that this can be done with image transformations and some maths.
The rotation of the cube will be always the same.
I don't need "stroke" edges and face lighting like in that picture, I just need a "shadeless" cube.
Finally, the result should be an alpha-transparent PNG.
P.S. I only have GD on my host, I don't have access to ImageMagick.
回答1:
you are searching for something like this?
<?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" >
image result:

回答2:
Starting from kraysak's answer, do the following:
In order to use images instead of colors, please replace these lines:
$blue = imagecolorallocate($im, 0, 0, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
and
imagefilledpolygon($im, $first_poligon, 4, $blue);
imagefilledpolygon($im, $second_poligon, 4, $white);
imagefilledpolygon($im, $third_poligon, 4, $red);
With something like this (this example is only for one of the three faces):
For 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');
Instead of:
$blue = imagecolorallocate($im, 0, 0, 255)
You will use:
$cube_face_1 = imagepng($img);
And instead of:
imagefilledpolygon($im, $first_poligon, 4, $blue);
You will use:
imagefilledpolygon($im, $first_poligon, 4, $cube_face_1);
Source: http://php.net/manual/en/book.image.php
On the above link, you can find details regarding gif/jpeg images. My demonstration is for png images.
回答3:
i can´t let that this problem defeat me... (also i want to help you, but not that much :P) so, i made this litle code that resolve almost 70% of your problem:
<?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" >
and, the result: at right is the image made by previous code.

来源:https://stackoverflow.com/questions/25888253/php-isometric-cube-with-gd