2014-04-08 50 views
0

此功能的目標是創建一個目錄中所有.png的精靈。我一直在考慮這一段時間試圖讓它工作。我檢查了所有的文件夾權限,並確保啓用了gd庫。有什麼建議麼? ?Php Gd Sprite創建

<?php 
    function spriter($dir = 'thumbs/*.png', $dest = 'thumbs/sprite.png', $spacing = 1) { 

     // define icons sizes 
     $icon_width = 32; 
     $icon_height = 32; 

     // start height of my sprite canvas 
     $height = 100; 

     // select all the icons and read theri height to build our canvas size. 
     foreach (glob($dir) as $file) { 
      list($w, $h) = getimagesize($file); 
      // make sure out icon is a 32px sq icon 
      if ($h == $icon_height) 
       $height += ($h + $spacing); 
     } 

     // double our canvas height to allow for a gray-scale versions. 
     $height = ($height * 2); 

     // create our canvas 
     $img = imagecreatetruecolor($icon_width, $height); 
     $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 (glob($dir) as $file) { 
      $tmp = imagecreatefrompng($file); 
      if (imagesy($tmp) == $icon_height) { 
       imagecopy($img, $tmp, 0, $pos, 0, 0, $icon_width, $icon_height); 
       $pos += ($icon_height + $spacing); 
      } 
      imagedestroy($tmp); 
     } 

     // place all of our icons on again, but this time convert them to gray-scale 
     foreach (glob($dir) as $file) { 
      $tmp = imagecreatefrompng($file); 
      if (imagesy($tmp) == $icon_height) { 
       imagefilter($tmp, IMG_FILTER_GRAYSCALE); 
       imagecopy($img, $tmp, 0, $pos, 0, 0, $icon_width, $icon_height); 
       $pos += ($icon_height + $spacing); 
      } 
      imagedestroy($tmp); 
     } 

     // create our final output image. 
     imagepng($img, $dest); 
    } 

>

+0

它不起作用?你會得到什麼錯誤? –

+0

它沒有給出任何錯誤,它只是不輸出PNG。 – user2392940

回答