0

我正在从单个图像创建多个部分图像以用于地图叠加。我遇到的问题是第一个图像创建成功,但其余的都是黑色的。我怀疑 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
4

1 回答 1

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
于 2014-04-11T17:03:09.873 回答