2014-04-11 108 views
0

我從單個圖像創建多個部分圖像以用於地圖疊加。我遇到的問題是第一個圖像創建成功,但其餘的都是黑色的。我懷疑imagecreatefrompng是罪魁禍首,它無法運行多次,清除它,所以我的圖像是黑色的,因爲圖像是零。請幫我找到適合我的代碼的解決方案。提前致謝。PHP在單個圖像中使用imagecreatefrompng函數創建多個圖像

我的代碼中的所有東西都起作用,除了圖像創建。正如我之前說過的,它一次成功創建圖像。我的循環輸出說明了它應該說的一切。

for($x = 0; $x <= $loop; $x++) 
{ 
    //Check for direcotory for X 
    $x_directory = 'generated_images/' . $dbn . '/' . $zoom . '/' . $x; 
    if (!file_exists($x_directory)) 
    { 
     mkdir($x_directory, 0777, true); 
    }//end if 

    //Set Starting Y Copy Values 
    $startY = 0; 
    $endY = 1; 

    for($y = 0; $y <= $loop; $y++) 
    { 
     //Do not need to check for Y images, we will replace any exsisting images 
     //Set a time limit for each Y image 
     set_time_limit(15); 
     $time = time(); 

     $src_image = imagecreatefrompng('generated_images/pre'. $dbn .'.png') or die('Problem with source'); 
     $y_image = imagecreatetruecolor($image_w,$image_h) or die('Problem In Creating image'); 

     if(($x >= 2 && $x <= 5) && ($y >= 5 && $y <= 6)) { 
      ?> 
      <p> 
      Start X: <?=$startX?> <br/> 
      Start Y: <?=$startY?> <br/> 
      End X: <?=$endX?> <br/> 
      End Y: <?=$endY?> <br/> 
      <? 

      //imagecopyresized($y_image, $src_image, 0, 0, $startX, $startY, $image_w, $image_h, 1024, 1024); 

      //Set the blending mode for an image 
      imagealphablending($y_image, false); 
      imagesavealpha($y_image, true); 

      // scan image pixels 
      for ($pix_y = ($startY * $multiplier); $pix_y < ($endY * $multiplier) ; $pix_y++) { 

       for ($pix_x = ($startX *$multiplier); $pix_x < ($endX * $multiplier) ; $pix_x++) { 

        $out_pix = imagecolorat($src_image,$pix_x,$pix_y); 
        $colors = imagecolorsforindex($y_image, $out_pix); 
        //$src_pix_array = rgb_to_array($src_pix); 

        if($colors['red'] == 0 && $colors['green'] == 0 && $colors['blue'] == 0) $alpha = 127; 
        else $alpha = 80; 

        imagesetpixel($y_image, $pix_x, $pix_y, imagecolorallocatealpha($y_image, $colors['red'], $colors['green'], $colors['blue'], $alpha)); 


       }//end for 

      }//end for 

      $startY++; 
      $endY++; 

      imagepng($y_image,$x_directory . '/' . $y . '.png') or die('Problem saving image: ' . $x_directory . '/' . $y . '.png'); 
      imagedestroy($y_image); 
      //imagedestroy($src_image); 

     }//end if 
     else 
     { 
      $black = imagecolorallocate($y_image, 0, 0, 0); 
      // Make the background transparent 
      imagecolortransparent($y_image, $black); 

      imagepng($y_image,$x_directory . '/' . $y . '.png') or die('Problem saving image: ' . $x_directory . '/' . $y . '.png'); 
      imagedestroy($y_image); 

     } 

     $time = time() - $time; 
     ?> 
     Image <?=$x?>,<?=$y?> time: <?=$time?> Seconds <br/> 
     <? 

    } 
    //end for y 

    if(($x >= 2 && $x <= 5)) { 
    $startX++; 
    $endX++; 
    } 

}//end for x 

回答

1

如果你想大圖像分割成更小的圖像,我建議你只需要調用imagecreatefrompng()一次,然後創建imagecreatetruecolor(圖像),並使用imagecopyresampled()爲每個複製位新的圖像對象(或使用同一個並保存部分你解壓後他們)

下面是我會做:

$main_image = imagecreatefrompng($path); //assuming you know the path 
//do your for's and every time your loop runs, you use an $part_image like this 

//LOOP START 
$part_image = imagecreatetruecolor(you know your parameters, atleast you should); 
//do the imagecopyresampled with your coordinates 
imagejpeg($part_image, $part_path); //save the image, make sure each time you do a different filename in order not to overwrite the first part - maybe increment a $counter variable and append it to the filename like "part1.jpg" and so on 
//or imagepng, see php docs for these functions 
imagedestroy($part_image); 
//LOOP END 
+0

你能只使用imagecreatefrompng()一次? – mcphersonjr

+0

你也可以提供一個簡單的例子嗎? – mcphersonjr

+0

回覆一個簡短的例子,如果你很聰明,你會發現它很快。 P.S. Daca e mai vb maine,in functie de ce timp am – ied3vil