Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual gridding/resizing of image collage in PHP

I'm looking for a finshed solution or possibly some math/algorithms for creating a big image collage out of smaller product pictures? I know how to do it in a square fashion, from same sized pictures with gd/imagemagick but I'd like to have some variation built in.

For example, some of the pictures might be slightly taller, and if all are the same size and square - i might want 1 of them take up more space, just to mix up the design. keeping it interesting.

the more i think about this the harder it seems to do with a formula. having predefined "templates" for all the possible scenarios isn't going to work since the amount of pictures could vary from just 1 (no work needed) to 10+.

i'm not looking for any rotation or special effects, just the images in a grid with maybe some spacing inbetween and no overlapping.

any ideas how to accomplish this, and is there really nothing ready to go out there?

like image 686
Christoffer Bubach Avatar asked Oct 16 '12 11:10

Christoffer Bubach


1 Answers

Implement transparency

First on the __construct method, replace :

    $white = imagecolorallocate($this->image, 255, 255, 255);
    imagefill($this->image, 0, 0, $white);

By :

    $transparent = imagecolorallocate($this->image, 255, 0, 255);
    imagefill($this->image, 0, 0, $transparent);
    imagecolortransparent($this->image, $transparent);

Then, on the resizePreservingAspectRatio method, add just after :

    $targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

The following lines :

    $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
    imagefill($targetImg, 0, 0, $targetTransparent);
    imagecolortransparent($targetImg, $targetTransparent);

And here we go.

enter image description here

like image 132
Alain Tiemblo Avatar answered Oct 20 '22 03:10

Alain Tiemblo