-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageOverlayer.php
More file actions
172 lines (156 loc) · 5.03 KB
/
Copy pathimageOverlayer.php
File metadata and controls
172 lines (156 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace alesinicio;
class imageOverlayer {
private static $imageType;
private static $quality;
/**
* This method receives a string with the configuration filename as parameter and returns an image resource.
* @param string $configFilename
* @throws \Exception
* @return resource
*/
public static function overlay($configFilename) {
if (!file_exists($configFilename)) throw new \Exception("Could not find configuration file.");
require_once $configFilename;
if (
!isset($overlays) ||
!isset($imgSrc) ||
!isset($finalWidth) ||
!isset($finalHeight)
) throw new \Exception("Invalid configuration file.");
self::getImageType($imgSrc);
self::$quality = $quality;
$output = self::createOverlays($overlays, $imgSrc, $finalWidth, $finalHeight);
return $output;
}
/**
* Converts an image resource into a base64 string.
* @param unknown $resImage
* @return string
*/
public static function imgToBase64($resImage) {
ob_start();
switch (self::$imageType) {
case "PNG":
$quality = 10-floor(self::$quality/10);
if ($quality == 10) $quality = 9;
imagepng($resImage,null,$quality);
$output = "data:image/png;base64,".base64_encode(ob_get_contents());
break;
case "JPG":
imagejpeg($resImage, null, self::$quality);
$output = "data:image/jpeg;base64,".base64_encode(ob_get_contents());
break;
}
ob_end_clean();
return $output;
}
/**
* Wrapper method that receives an array with all the overlays that should be created and returns an image resource.
* @param unknown $overlays
* @param unknown $imgSrc
* @param unknown $finalWidth
* @param unknown $finalHeight
* @param number $quality
* @return resource
*/
private static function createOverlays($overlays, $imgSrc, $finalWidth, $finalHeight) {
$image = null;
for($i=0; $i<count($overlays); $i++) {
$overlay = $overlays[$i];
$imgSrc = ($i == 0 ? $imgSrc : null);
$image = self::imageannotate(
$overlay['text'],
$overlay['font'],
$overlay['fontSize'],
$overlay['color'],
$overlay['posX'],
$overlay['posY'],
$overlay['rotation'],
$image,
$imgSrc
);
}
return self::resizeImage($image, $finalWidth, $finalHeight);
}
/**
* Sets the static property imageType, which determines if the template image is a JPG or PNG.
* @param string $imgSrc
* @throws \Exception
*/
private static function getImageType($imgSrc) {
switch(exif_imagetype($imgSrc)) {
case IMAGETYPE_JPEG:
self::$imageType = "JPG";
break;
case IMAGETYPE_PNG:
self::$imageType = "PNG";
break;
default:
throw new \Exception("Only JPG/PNG should be used as template.");
}
}
/**
* Effectively creates an overlay.
* @param string $text
* @param string $font
* @param int $fontSize
* @param string $arrFontColourRGB
* @param string $positionX
* @param string $positionY
* @param int $outputQuality
* @param int $rotationAngle
* @param image $image
* @param string $sourceFileName
* @throws \Exception
* @return resource
*/
private static function imageannotate($text, $font, $fontSize, $arrFontColourRGB, $positionX, $positionY, $rotationAngle=0, $image=null, $sourceFilename=null) {
if ($image === null) {
if (!file_exists($sourceFilename)) throw new \Exception("Template image not found.");
$image = self::createImageResource($sourceFilename);
}
$colour = imagecolorallocate($image, $arrFontColourRGB['r'], $arrFontColourRGB['g'], $arrFontColourRGB['b']);
imagealphablending($image, false);
imagesavealpha($image, true);
imagealphablending($image, true);
if (imagettftext($image, $fontSize, $rotationAngle, $positionX, $positionY, $colour, $font, $text) === false) throw new \Exception("Error creating overlay.");
return $image;
}
/**
* Creates an image resource from a file.
* @param string $sourceFilename
* @throws \Exception
* @return resource
*/
private static function createImageResource($sourceFilename) {
switch (self::$imageType) {
case "PNG":
$image = imagecreatefrompng($sourceFilename);
break;
case "JPG":
$image = imagecreatefromjpeg($sourceFilename);
break;
}
if ($image === false) throw new \Exception("Error loading template image.");
return $image;
}
/**
* Resizes an image resouce.
* @param resource $image
* @param int $dst_width
* @param int $dst_height
* @return resource
*/
private static function resizeImage($image, $dst_width, $dst_height) {
$width = imagesx($image);
$height = imagesy($image);
$newImg = imagecreatetruecolor($dst_width, $dst_height);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
imagecopyresampled($newImg, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);
return $newImg;
}
}