DomingoSL
DomingoSL

Reputation: 15494

How to make a single picture (PNG/JPG) from a know number of pictures on PHP?

i have an array in php who contains pictures url (jpg pics). When you execute the php script this array is filled with url of pictures (for this example lets say 4 pics). At this point i want to equally distribute the pics in the space and generate a single one, like: alt text

Upvotes: 1

Views: 257

Answers (1)

nikc.org
nikc.org

Reputation: 16962

  1. Grab all the pictures in the array
  2. Find the two largest dimensions
  3. Make a canvas that can fit 4 images covering an area of the product of the two largest dimensions, i.e. Height = MaxY * 2 and Width = MaxX * 2.
  4. Place images on canvas, you can easily use the max dimensions as a guide here:
    1. x= MaxX / 2 + Image1 Width / -2, y= MaxY / 2 + Image1 Height / -2
    2. x= MaxX + (MaxX / 2 + Image2 Width / -2), y= MaxY / 2 + Image2 Height / -2
    3. x= MaxX / 2 + Image3 Width / -2, y= MaxY + (MaxY / 2 + Image3 Height / -2)
    4. x= MaxX + (MaxX / 2 + Image4 Width / -2), y= MaxY + (MaxY / 2 + Image4 Height / -2)
  5. Save to disk

You may want to scale down to a set maximum, in which case you need to introduce a scale multiplier into the equation. You might also want to crop to a square size to reduce empty space induced from having pictures of different aspect ratios.

Keywords: getimagesize, imagecreatetruecolor, imagecopyresampled.

Upvotes: 3

Related Questions