Skip navigation.

Lolek blog

Everything and nothing...

[php] images layers class

, , , ,

Hi there, it's been awhile since my last post,
so here i back for a moment with new php class.. i know it's not big thing, it's also nothing special, but i want share it with anyone and hope it will someday help people... :wink:
below is the class, so, use it wiselly, it's the alpha version.
Of course, code is simple and there is no need to explain anything.. please just leave the header (i.e. who made it)...

<?php


class mergeException extends Exception{};

class imageAttr {
    public $image;
    public $width;
    public $height;
    public $type;
    public $posX;
    public $posY;
    public function __construct($img,$w,$h,$t,$pX=0,$pY=0) {
        $this->image = $img;
        $this->width = $w;
        $this->height = $h;
        $this->type = $t;
        $this->posX = $pX;
        $this->posY = $pY;
    }
}

class layerAttr {
    public $imagePath;
    public $posX;
    public $posY;
    public function __construct($imgPath,$pX=0,$pY=0) {
        $this->imagePath = $imgPath;
        $this->posX = $pX;
        $this->posY = $pY;
    }
}

/**
 * Class for adding several different images for one i.e. first try to make a layer manager for php
 *
 * @author lolek
 * @copyright lolek
 * @throws mergeException
 * @version 0.0.1 alpha
 */
class imageMerge {
    private $background;
    private $layerArray = array();
    /**
     * default constructor
     *
     * @param String $bgImage
     */
    public function __construct($bgImage) {
        $this->background = null;
        $this->layerArray = array();
        $this->outputImage = null;
        if(is_file($bgImage))
        {
            $this->background = $this->loadImage($bgImage);
        }
        else
        {
            throw new mergeException();
        }
    }

    /**
     * Add layer
     *
     * @param layerAttr $ly
     * @return void
     */
    public function addLayer(layerAttr $ly) {
        //        list($this->width, $this->height, $this->type) = @getimagesize($bgImage);
        if(is_file($ly->imagePath))
        {
            $img = $this->loadImage($ly->imagePath);
            $img->posX = $ly->posX;
            $img->posY = $ly->posY;
            if($this->checkIfInside($img))
            {
                $this->layerArray[] = $img;
            }
        }
        else
        {
            throw new mergeException('Layer not found');
        }
    }

    /**
     * check if the layer is smaller than base image
     *
     * @param imageAttr $img
     * @return bool
     */
    protected function checkIfInside(imageAttr $img) {

        if($img->width > $this->background->width || $img->height>$this->background->height)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * Load image into memory
     *
     * @param String $img
     * @return imageAttr
     */
    protected function loadImage($img) {
        list($w,$h,$type) = @getimagesize($img);
        $outputImg = null;
        switch($type)
        {
            case 1:  $outputImg=imagecreatefromgif($img); break;
            case 2:  $outputImg=imagecreatefromjpeg($img); break;
            case 3:  $outputImg=imagecreatefrompng($img); break;
            default:
                throw new mergeException('Nieobslugiwany typ pliku graficznego'.$img.', typ:'.$this->type);
        }
        return new imageAttr($outputImg,$w,$h,$type);
    }

    /**
     * Used to merge added layers
     *
     * @param String $outputFileName
     * @return void
     */
    public function merge($outputFileName) {

        for($i=0; $i<count($this->layerArray); $i++)
        {
            imagecopyresampled(
            $this->background->image,
            $this->layerArray[$i]->image,
            $this->layerArray[$i]->posX,
            $this->layerArray[$i]->posY,
            0,
            0,
            $this->layerArray[$i]->width,
            $this->layerArray[$i]->height,
            $this->layerArray[$i]->width,
            $this->layerArray[$i]->height
            );
        }

        if(strlen($outputFileName)<4)
        {
            throw new MergeException('Output filename too short');
        }
        if(is_file($outputFileName))
        {
            throw new MergeException('File already exists');
        }

        switch($this->background->type)
        {
            case 1:  imagegif($this->background->image,$outputFileName); break;
            case 2:  imagejpeg($this->background->image,$outputFileName,95); break;
            case 3:  imagepng($this->background->image,$outputFileName,95); break;
            default:
                throw new mergeException('Nieobslugiwany typ pliku graficznego '.$this->background->type);
        }

    }
}

try {

$lyMan = new imageMerge('imgTest/bg.jpg');
$lyMan->addLayer(new layerAttr('imgTest/ly.png',150,250));
$lyMan->merge('');

} catch (mergeException $e)
{
    echo $e->getMessage();
}


?>

a little program to help people "always on top"few days left

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies