phpimagefor-loopimagecreatefrompng

PHP creating multiple images from single image using imagecreatefrompng function in a loop


I am creating multiple partial images from a single image to use for a map overlay. The issue I am having is that the first image is created successfully, but the rest are black. I suspect that the imagecreatefrompng is the culprit and it is not able to run multiple times with out clearing it and so my images are black because the image is nil. Please help me find a suitable solution to my code below. Thanks in advance.

Everything in my code works, except the image creation. As I said before it creates the image successfully once. The output of my loop says everything that it should say.

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

Solution

  • If you're trying to split the big image into smaller images, i suggest you just call imagecreatefrompng() once, then create images with imagecreatetruecolor() and use imagecopyresampled() for each of the bits copying in new image objects (or use same one and save the parts after you extract them)

    Here is how i would do it:

    $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