Page 1 of 1

imagecopymerge problems?

Posted: Tue Feb 24, 2009 10:18 pm
by psychotomus
I'm trying to take an image and extract every 32x32 image from it. heres what I got. every image i upload is divisible by 32 so I know thats not the problem. ever image I uploaded gets created as preview.png so i know thats not the problem either.

Code: Select all

            mkdir("../tiles/$names", 0777);
            $file_typ = array();
            $file_typ =  explode('.',strtolower($_FILES["file"]["name"]));
            $file_type = $file_typ[count($file_typ)-1];
            if($file_type == "png")
            {
                move_uploaded_file($_FILES["file"]["tmp_name"], "../tiles/$names/preview.png");
                $src = imagecreatefrompng("../tiles/$names/preview.png");
                
                $w = imagesx($src) / 32; 
                $h = imagesy($src) / 32;
                if( (is_int($w)) && (is_int($h)) )
                {
 
                     /* Create a blank image */
                    for($x=0; $x < $h - 32; $x=$x+32)
                    {
                        for($y=0; $y < $w - 32; $y=$y+32)
                        {
                            $im  = imagecreate(32, 32);
                            imagecopymerge($im $src, 0, 0, $x,$y, $x+32, $y + 32, 0);
                            imagepng($im ,"../tiles/$names/$x-$y.png"); 
                        }
                    }
                }
            }

Re: imagecopymerge problems?

Posted: Tue Feb 24, 2009 10:37 pm
by requinix
psychotomus wrote:I'm trying to take an image and extract every 32x32 image from it. heres what I got. every image i upload is divisible by 32 so I know thats not the problem. ever image I uploaded gets created as preview.png so i know thats not the problem either.
Great. So what is the problem?

Anyways,

Code: Select all

imagecopymerge($im $src, 0, 0, $x,$y, 32, 32, 0);
Those two arguments near the end are width and height, not end X and end Y.

Re: imagecopymerge problems?

Posted: Tue Feb 24, 2009 10:39 pm
by psychotomus
i changed imagecopymerge to imagecopy but its still not working. its not saving none of the 32x32 png files.

Re: imagecopymerge problems?

Posted: Tue Feb 24, 2009 10:45 pm
by requinix
Hmm. Thought I could get the edit in fast enough. Look up, I added something.

Are there no images at all or are they just bad images?

Re: imagecopymerge problems?

Posted: Tue Feb 24, 2009 10:59 pm
by psychotomus

Code: Select all

            if($file_type == "png")
            {
                move_uploaded_file($_FILES["file"]["tmp_name"], "../tiles/$names/preview.png");
                $src = imagecreatefrompng("../tiles/$names/preview.png");
                
                $w = imagesx($src) / 32; 
                $h = imagesy($src) / 32;
                if( (is_int($w)) && (is_int($h)) )
                {
                    $im  = imagecreate(32, 32);
                    
                     /* Create a blank image */
                    for($x=0; $x < $h - 32; $x=$x+32)
                    {
                        for($y=0; $y < $w - 32; $y=$y+32)
                        {
                            imagecopy($im, $src, 0, 0, $x,$y, 32, 32);
                            imagepng($im ,"../tiles/$names/$x-$y.png"); 
                        }
                    }
                }
            }
its not creating no images at all.

Re: imagecopymerge problems?

Posted: Wed Feb 25, 2009 12:04 am
by requinix
psychotomus wrote:its not creating no images at all.
So it is creating images then? Good. No problems.

...Unless you meant "it's not creating any images at all", which is the exact opposite of what you said.

Code: Select all

if($file_type == "png")
{
    echo "moving file: ";
    var_dump(move_uploaded_file($_FILES["file"]["tmp_name"], "../tiles/$names/preview.png"));
    echo "loading image: ";
    var_dump($src = imagecreatefrompng("../tiles/$names/preview.png"));
   
    echo "width=", $w = imagesx($src) / 32;
    echo "height=", $h = imagesy($src) / 32;
    if( (is_int($w)) && (is_int($h)) )
    {
        echo "creating blank image";
        var_dump($im  = imagecreate(32, 32));
       
        /* Create a blank image */
        for($x=0; $x < $h - 32; $x=$x+32)
        {
            for($y=0; $y < $w - 32; $y=$y+32)
            {
                echo "copying: ";
                var_dump(imagecopy($im, $src, 0, 0, $x,$y, 32, 32));
                echo "saving: ";
                var_dump(imagepng($im ,"../tiles/$names/$x-$y.png"));
            }
        }
    }
}
 
What do you get from running that version?

Re: imagecopymerge problems?

Posted: Wed Feb 25, 2009 12:16 am
by psychotomus
moving file: bool(true) loading image: resource(8) of type (gd) width=19height=19creating blank imageresource(9) of type (gd) Map Image height and width must be divisible by 32.


got it working.. ;]

Re: imagecopymerge problems?

Posted: Wed Feb 25, 2009 12:45 am
by requinix
psychotomus wrote:got it working.. ;]
So you see what you did there then?

To everybody who doesn't see it: there are two bits of code you need to pay close attention to.

Code: Select all

echo "width=", $w = imagesx($src) / 32;
echo "height=", $h = imagesy($src) / 32;

Code: Select all

for($x=0; $x < $h - 32; $x=$x+32)
{
    for($y=0; $y < $w - 32; $y=$y+32)
    {
In the first part, $w and $h correspond to the number of 32-pixel blocks in the width and height respectively, but in the second part they are used as actual pixel locations.
From the output he posted you see that width=19 and height=19. Subtract 32 and you get -13, which is less than 0 and the two conditions in the loop fail. With a much larger image (1056x1056 at the very least) the behavior would be different since $w = 1056/32 = 33, minus 32 = 1, is greater than 0.