2011-04-17 54 views
0

我正在创建一个简单的PHP sprite图像生成器,我能够计算出sprite图像的可能宽度和高度,但我被困在创建透明画布并将图像进入它。PHP-将单个图像放入透明画布中

现在让我们说精灵图像的宽度是200x500px(我创建垂直精灵)。并且要放置在精灵图像中的图像如下所示:

array('logo.png','delete.png','open.png'); 

如何将这些图像放入精灵?

+0

使用图像功能的多种可能性(http://us.php.net/manual/en/ref.image.php) – 2011-04-17 07:38:04

回答

0
 $path = '/img/'; 
     $imgs = array('logo.png','delete.png','save.png');//images needed to be put in the sprite 
     $canvasWidth= 0; 
     $canvasHeight = 0; 

     //calculate the posible canvas width and height 
     foreach($imgs as $val) 
     { 
      $info = getimagesize($path.basename($val)); 
      $width=$info[0]; 
      $height=$info[1]; 
      $canvasHeight +=$height; 
      if($canvasWidth < $width){ 
       $canvasWidth = $width; 
      } 
     } 

     // create our canvas 
     $img = imagecreatetruecolor($canvasWidth, $canvasHeight); 
     $background = imagecolorallocatealpha($img, 255, 255, 255, 127); 
     imagefill($img, 0, 0, $background); 
     imagealphablending($img, false); 
     imagesavealpha($img, true); 

     // start placing our icons from the top down. 
     $pos = 0; 
     foreach($imgs as $val){ 
      $tmp = imagecreatefrompng($path.basename($val)); 
       $w=imagesx($tmp); 
       $h=imagesy($tmp); 
       imagecopy($img, $tmp, 0, $pos, 0, 0, $w, $h); 
       $pos += $h; 
      imagedestroy($tmp); 
     } 

     // create our final output image. 
     imagepng($img, $path.'sprite.png');