1<?php
2/**
3 *--------------------------------------------------------------------
4 *
5 * Enable to join 2 CINDrawing or 2 image object to make only one image.
6 * There are some options for alignment.
7 *
8 *--------------------------------------------------------------------
9 * @author  Akhtar Khan <er.akhtarkhan@gmail.com>
10 * @link http://www.codeitnow.in
11 * @package https://github.com/codeitnowin/barcode-generator
12 */
13namespace CodeItNow\BarcodeBundle\Generator;
14
15class JoinDraw {
16    const ALIGN_RIGHT       = 0;
17    const ALIGN_BOTTOM      = 0;
18    const ALIGN_LEFT        = 1;
19    const ALIGN_TOP         = 1;
20    const ALIGN_CENTER      = 2;
21
22    const POSITION_RIGHT    = 0;
23    const POSITION_BOTTOM   = 1;
24    const POSITION_LEFT     = 2;
25    const POSITION_TOP      = 3;
26
27    private $image1;
28    private $image2;
29    private $alignement;
30    private $position;
31    private $space;
32    private $im;
33
34    /**
35     * Construct the JoinDrawing Object.
36     *  - $image1 and $image2 have to be CINDrawing object or image object.
37     *  - $space is the space between the two graphics in pixel.
38     *  - $position is the position of the $image2 depending the $image1.
39     *  - $alignment is the alignment of the $image2 if this one is smaller than $image1;
40     *    if $image2 is bigger than $image1, the $image1 will be positionned on the opposite side specified.
41     *
42     * @param mixed $image1
43     * @param mixed $image2
44     * @param CINColor $background
45     * @param int $space
46     * @param int $position
47     * @param int $alignment
48     */
49    public function __construct($image1, $image2, $background, $space = 10, $position = self::POSITION_RIGHT, $alignment = self::ALIGN_TOP) {
50        if ($image1 instanceof CINDrawing) {
51            $this->image1 = $image1->get_im();
52        } else {
53            $this->image1 = $image1;
54        }
55        if ($image2 instanceof CINDrawing) {
56            $this->image2 = $image2->get_im();
57        } else {
58            $this->image2 = $image2;
59        }
60
61        $this->background = $background;
62        $this->space = (int)$space;
63        $this->position = (int)$position;
64        $this->alignment = (int)$alignment;
65
66        $this->createIm();
67    }
68
69    /**
70     * Destroys the image.
71     */
72    public function __destruct() {
73        imagedestroy($this->im);
74    }
75
76    /**
77     * Finds the position where the barcode should be aligned.
78     *
79     * @param int $size1
80     * @param int $size2
81     * @param int $ailgnment
82     * @return int
83     */
84    private function findPosition($size1, $size2, $alignment) {
85        $rsize1 = max($size1, $size2);
86        $rsize2 = min($size1, $size2);
87
88        if ($alignment === self::ALIGN_LEFT) { // Or TOP
89            return 0;
90        } elseif ($alignment === self::ALIGN_CENTER) {
91            return $rsize1 / 2 - $rsize2 / 2;
92        } else { // RIGHT or TOP
93            return $rsize1 - $rsize2;
94        }
95    }
96
97    /**
98     * Change the alignments.
99     *
100     * @param int $alignment
101     * @return int
102     */
103    private function changeAlignment($alignment) {
104        if ($alignment === 0) {
105            return 1;
106        } elseif ($alignment === 1) {
107            return 0;
108        } else {
109            return 2;
110        }
111    }
112
113    /**
114     * Creates the image.
115     */
116    private function createIm() {
117        $w1 = imagesx($this->image1);
118        $w2 = imagesx($this->image2);
119        $h1 = imagesy($this->image1);
120        $h2 = imagesy($this->image2);
121
122        if ($this->position === self::POSITION_LEFT || $this->position === self::POSITION_RIGHT) {
123            $w = $w1 + $w2 + $this->space;
124            $h = max($h1, $h2);
125        } else {
126            $w = max($w1, $w2);
127            $h = $h1 + $h2 + $this->space;
128        }
129
130        $this->im = imagecreatetruecolor($w, $h);
131        imagefill($this->im, 0, 0, $this->background->allocate($this->im));
132
133        // We start defining position of images
134        if ($this->position === self::POSITION_TOP) {
135            if ($w1 > $w2) {
136                $posX1 = 0;
137                $posX2 = $this->findPosition($w1, $w2, $this->alignment);
138            } else {
139                $a = $this->changeAlignment($this->alignment);
140                $posX1 = $this->findPosition($w1, $w2, $a);
141                $posX2 = 0;
142            }
143
144            $posY2 = 0;
145            $posY1 = $h2 + $this->space;
146        } elseif ($this->position === self::POSITION_LEFT) {
147            if ($w1 > $w2) {
148                $posY1 = 0;
149                $posY2 = $this->findPosition($h1, $h2, $this->alignment);
150            } else {
151                $a = $this->changeAlignment($this->alignment);
152                $posY2 = 0;
153                $posY1 = $this->findPosition($h1, $h2, $a);
154            }
155
156            $posX2 = 0;
157            $posX1 = $w2 + $this->space;
158        } elseif ($this->position === self::POSITION_BOTTOM) {
159            if ($w1 > $w2) {
160                $posX2 = $this->findPosition($w1, $w2, $this->alignment);
161                $posX1 = 0;
162            } else {
163                $a = $this->changeAlignment($this->alignment);
164                $posX2 = 0;
165                $posX1 = $this->findPosition($w1, $w2, $a);
166            }
167
168            $posY1 = 0;
169            $posY2 = $h1 + $this->space;
170        } else { // defaults to RIGHT
171            if ($w1 > $w2) {
172                $posY2 = $this->findPosition($h1, $h2, $this->alignment);
173                $posY1 = 0;
174            } else {
175                $a = $this->changeAlignment($this->alignment);
176                $posY2 = 0;
177                $posY1 = $this->findPosition($h1, $h2, $a);
178            }
179
180            $posX1 = 0;
181            $posX2 = $w1 + $this->space;
182        }
183
184        imagecopy($this->im, $this->image1, $posX1, $posY1, 0, 0, $w1, $h1);
185        imagecopy($this->im, $this->image2, $posX2, $posY2, 0, 0, $w2, $h2);
186    }
187
188    /**
189     * Returns the new $im created.
190     *
191     * @return resource
192     */
193    public function get_im() {
194        return $this->im;
195    }
196}
197?>