PHP Image Collage

Posted by Tully on Sun 19 September 2010

This weekend I started my first Facebook application. I decided to create an application that will take your friends photos and make a collage out of them. I intend to make the size of the collage a variable that can be selected from a drop-down HTML element. I would like the user to be able to create a collage that is the size of there screen resolution. This way they could make a wallpaper for there desktop with all there friends.

Produces

PHP Image Collage

Usage

<?php
include 'Collage.php';
$time = time();
$type = 'jpg';
$imageObj = new Collage($images, 250, 250);
$imageObj->execute();
$imageObj->saveFile("images/{$time}.jpg", $type);
echo "<img src=\"images/{$time}.{$type}\" />";

Code

<?php
/**
* This class is used to create an image collage from
* an array of image links.
*
* @author Tully Rankin
* @website http://www.TullyRankin.com
*/
class Collage {

    /**
    * Holds array of image links
    */
    public $images = array();

    /**
    * Height of the collage image
    */ 
    public $height;

    /**
    * Width of the collage image
    */
    public $width;

    /**
    * Holds the buffer of all images
    */
    public $imgBuf = array();

    /**
    * Stores the created collage image
    */
    public $imgOut;

    /**
    * Holds array of Exception objects
    */
    public $errors = array();

    public function __construct(Array $images, $height=1024, $width=768)
    {
        try {

            if (empty($images))
                throw new Exception('Images can not be empty.');

            $this->images = $images;
            $this->height = $height;
            $this->width = $width;

            $iTmp = "";

            $this->_addImagesToBuffer();
            $this->imgOut = imagecreatetruecolor($this->width, $this->height);
        } catch (Exception $e) {
            $this->errors[] = $e;
        }
    }

    /**
    * Creates the collage
    */
    public function execute()
    {
        $tmpHeight = 0;
        $tmpWidth = 0;

        try {
            do {
                foreach($this->imgBuf as $image) {
                    if ($tmpWidth <= $this->width) { 
                        @imagecopy($this->imgOut, $image, $tmpWidth, $tmpHeight, 0, 0, imagesx($image), imagesy($image));
                        @imagedestroy($image);
                        $tmpWidth += 50;
                    } else {
                        $tmpWidth = 0;
                        @imagecopy($this->imgOut, $image, $tmpWidth, $tmpHeight, 0, 0, imagesx($image), imagesy($image));
                        @imagedestroy($image);
                        $tmpWidth += 50;
                        $tmpHeight += 50;
                    }
                }
                $this->_addImagesToBuffer();;
            } while($tmpHeight <= $this->height);
        } catch (Exception $e) {
            $this->errors[] = $e;
        }
    }

    /**
    * Saves the image to a file
    *
    * @param string $name The name of the image.
    * @param string $type The image type to save as.
    */
    public function saveFile($name, $type="jpeg")
    {
        try {
            ob_start();
            switch($type) {
                case "png":
                    @imagepng($this->imgOut);
                    break;
                case "jpg":
                case "jpeg":
                    @imagejpeg($this->imgOut);
                    break;
                case "gif":
                    @imagegif($this->imgOut);
                    break;
                default:
                    throw new Exception("Unknown image type");
            }
            $contents = ob_get_contents();
            ob_end_clean();
            file_put_contents($name, $contents);
        } catch (Exception $e) {
            $this->errors[] = $e;
        }
    }

    /*
    * Creates an image from each image given in the constructor.
    * Adds it to public imgBuf array
    */
    protected function _addImagesToBuffer()
    {
        $this->imgBuf = array();
        foreach($this->images as $imageSrc) {
            $iTmp = imagecreatefromjpeg($imageSrc);
            array_push($this->imgBuf, $iTmp);
        }

    }
}