2014-04-11 2 views
0

지도 오버레이에 사용할 단일 이미지에서 여러 개의 부분 이미지를 만듭니다. 내가 가지고있는 문제는 첫 번째 이미지가 성공적으로 만들어졌지만 나머지 이미지는 검은 색이라는 것입니다. 나는 imagecreatefrompng이 범인이라고 의심하고 그것을 지우지 않고 여러 번 실행할 수 없으므로 이미지가 없기 때문에 이미지가 검은 색이됩니다. 아래 내 코드에 적합한 솔루션을 찾도록 도와주십시오. 미리 감사드립니다.루프에서 imagecreatefrompng 함수를 사용하여 단일 이미지에서 여러 이미지를 만드는 PHP

내 코드의 모든 것이 이미지 생성을 제외하고 작동합니다. 전에 말했듯이 그것은 한 번 성공적으로 이미지를 만듭니다. 내 루프 출력은 말해야 할 모든 것을 말합니다. 당신이 작은 이미지로 큰 이미지를 분할하려는 경우

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

, 내가 한 번, 다음 imagecreatetruecolor (와 이미지를 생성)과에 복사 각 비트에 대한) (imagecopyresampled 용) 그냥 imagecreatefrompng을 (전화 제안

$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

간단한 예를 들어 답하면 똑똑하다면 빨리 이해할 수 있습니다. 오후 8시 30 분 P.S. Daca e mai vb maine, functie de ce timp am – ied3vil

관련 문제