0

I am trying to create one very large image out of many small ones in PHP using WideImage library.

If the image becomes too big, the server obviously runs out of memory.

Is there anyway to solve this problem by executing this process in batches, or writing directly to disk, for example?

The code looks something like this:

        $blank = WideImage::load('../paintings/blank.png'); //this is a blank image file
        $new = $blank -> resize($fullWidth,$fullHeight,'fill'); //resize to full image size

        for ($x = 0; $x < $fullWidth ; $x = $x + $smallPictureWidth)
        {
            for ($y = 0; $y < $fullHeight ; $y = $y + $smallPictureHeight)
            {
                $fileName = //code to get filename based on $x and $y

                $img = WideImage::load($fileName);
                $new = $new -> merge($img,$x,$y,100);
            }
        }
        $fullImageName = //code to determine file name
        $new -> saveToFile($fullImageName);

Thanks!

Alex
  • 1
  • 2
  • Images are by their nature quite memory intensive. You could try batching (for example, build each row in a single process and then stitch the rows together afterwards in a different process) but if the image you're trying to make is truly enormous then it just might not be possible to fit it in available memory. For example a truecolour 4K image takes 23 megabytes (3840 x 2160 x 3 = 24883200 bytes, assuming no alpha channel). – GordonM Oct 07 '14 at 15:54
  • you can try something like `ini_set('memory_limit', '128M');` – cmorrissey Oct 07 '14 at 15:57
  • Unless `WideImage` offers the option to write the small image directly to a file (the big image), you will always need to store the uncompressed big image in memory. So you should start with checking the options they offer. – jeroen Oct 07 '14 at 15:59
  • 1
    don't save to a jpg for your intermediate images. you'll degrade the target image by repeatedly opening/resaving it. use a temporary lossless format, e.g. `.bmp`, instead until you're done assembling, THEN convert that to a .jpg – Marc B Oct 07 '14 at 16:00

0 Answers0