Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a folder with 5 images that I would like to compile through PHP into a vertical image sprite. I've looped through all the items getting the data and saved it into an array:

Array
(
[0] => Array
    (
        [location] => uploads/test1/android.png
        [height] => 175
        [width] => 175
        [vertical] => 0
    )

[1] => Array
    (
        [location] => uploads/test1/autocad.png
        [height] => 225
        [width] => 225
        [vertical] => 175
    )

[2] => Array
    (
        [location] => uploads/test1/betting.png
        [height] => 512
        [width] => 512
        [vertical] => 400
    )

[3] => Array
    (
        [location] => uploads/test1/calculator.png
        [height] => 200
        [width] => 200
        [vertical] => 912
    )

[4] => Array
    (
        [location] => uploads/test1/ccleaner.png
        [height] => 256
        [width] => 256
        [vertical] => 1112
    )

)

With vertical being the starting y coordinate for that image. Here's my current PHP:

$background = imagecreatetruecolor($horizontal, $vertical);
$newimg = "sprite.jpg";

foreach($images as $i)
{
  $tmp = imagecreatefromjpeg($i['location']);
  imagecopy($background, $tmp, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
  imagedestroy($tmp);
}

imagejpeg($background, $newimg);

$images is the array above. When I'm creating the array I calculate the horizontal and vertical variables (I've verified they are correct). I've tried a few things with the PHP and at one point managed to create a black image of the correct size (512x1368), but that's about as close as I've come. Any thoughts why this isn't working?


Here is the updated code:

$background = imagecreatetruecolor($horizontal, $vertical);
$newimg = "sprite.png";

foreach($images as $i)
{
  $tmp = imagecreatefrompng($i['location']);
  imagecopy($background, $tmp, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
  imagedestroy($tmp);
}

imagepng($background, $newimg);

I've even tried commenting out the "imagedestroy" to see if that was the culprit. The code does not generate anything. Zero. Folder and image permissions are all 777. At this point I'm not real sure even which direction to try next. Any thoughts?


Also tried "imagecreatefromstring", which also yielded no result (literally no file is being created).

$frame = imagecreatefromstring(file_get_contents($i['location']));
imagecopy($background, $frame, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
imagedestroy($frame);
share|improve this question
2  
At a cursory glance, you're using imagecreatefromjpeg when all your files are .pngs? – dKen May 30 at 15:56
I am not sure what you mean "This isn't working". Maybe I missed it, but you don't say which part isn't working. – Ryan Naddy May 30 at 16:08
He explained that he got the sprite created to the correct dimensions, but it was all black, indicating there's a problem with the image files themselves. I think that PHP is trying to interpret them as JPEGs from the function he's using, not PNGs – dKen May 30 at 16:09
I don't think that should matter, I believe I was able to pull a png image using imagecreatefromjpeg, but let me verify that... – Ryan Naddy May 30 at 16:11
Okay added the updated code and it still fails to generate anything (I've looked through all the folders just to make sure). Thoughts? – cfox May 30 at 16:45

1 Answer

up vote 0 down vote accepted

Wow... So yes, I had the permissions set correctly on the "uploads" folder, but $newimg was attempting to save in the root directly (not 777), which is why it appeared to be failing.

Course I did manage to break it again shortly after getting it to work (trying to make the background white instead of black, not going so well strangely), but I'm sure I'll get it working eventually!

Thanks for all the help!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.