1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
5
6 This file is part of WideImage.
7
8 WideImage is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 WideImage is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with WideImage; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 * @package Internal/Operations
23 **/
24
25 /**
26 * Merge operation class
27 *
28 * @package Internal/Operations
29 */
30 class WideImage_Operation_Merge
31 {
32 /**
33 * Returns a merged image
34 *
35 * @param WideImage_Image $base
36 * @param WideImage_Image $overlay
37 * @param smart_coordinate $left
38 * @param smart_coordinate $top
39 * @param numeric $pct
40 * @return WideImage_Image
41 */
42 function execute($base, $overlay, $left, $top, $pct)
43 {
44 $x = WideImage_Coordinate::fix($left, $base->getWidth(), $overlay->getWidth());
45 $y = WideImage_Coordinate::fix($top, $base->getHeight(), $overlay->getHeight());
46
47 $result = $base->asTrueColor();
48 $result->alphaBlending(true);
49 $result->saveAlpha(true);
50
51 if ($pct <= 0)
52 return $result;
53
54 if ($pct < 100)
55 {
56 if (!imagecopymerge(
57 $result->getHandle(),
58 $overlay->getHandle(),
59 $x, $y, 0, 0,
60 $overlay->getWidth(),
61 $overlay->getHeight(),
62 $pct))
63 throw new WideImage_GDFunctionResultException("imagecopymerge() returned false");
64 }
65 else
66 {
67 if (!imagecopy(
68 $result->getHandle(),
69 $overlay->getHandle(),
70 $x, $y, 0, 0,
71 $overlay->getWidth(),
72 $overlay->getHeight()))
73 throw new WideImage_GDFunctionResultException("imagecopy() returned false");
74 }
75
76 return $result;
77 }
78 }
79