Passed
Push — master ( 37f747...e8097c )
by Jonathan
05:17
created
src/Webtrees/ImageBuilder.php 3 patches
Indentation   +299 added lines, -299 removed lines patch added patch discarded remove patch
@@ -22,61 +22,61 @@  discard block
 block discarded – undo
22 22
  */
23 23
 class ImageBuilder {
24 24
     
25
-    /**
26
-     * Reference media
27
-     * @var Media $media
28
-     */
29
-    protected $media;
25
+	/**
26
+	 * Reference media
27
+	 * @var Media $media
28
+	 */
29
+	protected $media;
30 30
     
31
-    /**
32
-     * Use TTF font
33
-     * @var bool $use_ttf
34
-     */
35
-    protected $use_ttf;
31
+	/**
32
+	 * Use TTF font
33
+	 * @var bool $use_ttf
34
+	 */
35
+	protected $use_ttf;
36 36
     
37
-    /**
38
-     * Expiration offset. Default is one day.
39
-     * @var int $expire_offset
40
-     */
41
-    protected $expire_offset;
37
+	/**
38
+	 * Expiration offset. Default is one day.
39
+	 * @var int $expire_offset
40
+	 */
41
+	protected $expire_offset;
42 42
    
43
-    /**
44
-     * Should the certificate display a watermark
45
-     * @var bool $show_watermark
46
-     */
47
-    protected $show_watermark;
43
+	/**
44
+	 * Should the certificate display a watermark
45
+	 * @var bool $show_watermark
46
+	 */
47
+	protected $show_watermark;
48 48
         
49
-    /**
50
-     * Maximum watermark font size. Default is 18.
51
-     * @var int $font_max_size
52
-     */
53
-    protected $font_max_size;
49
+	/**
50
+	 * Maximum watermark font size. Default is 18.
51
+	 * @var int $font_max_size
52
+	 */
53
+	protected $font_max_size;
54 54
     
55
-    /**
56
-     * Watermark font color, in hexadecimal. Default is #4D6DF3.
57
-     * @var string $font_color
58
-     */
59
-    protected $font_color;
55
+	/**
56
+	 * Watermark font color, in hexadecimal. Default is #4D6DF3.
57
+	 * @var string $font_color
58
+	 */
59
+	protected $font_color;
60 60
     
61
-    /**
62
-     * Should the image be rendered as attachment (vs inline)     * 
63
-     * @var bool $as_attachment
64
-     */
65
-    protected $as_attachment;
61
+	/**
62
+	 * Should the image be rendered as attachment (vs inline)     * 
63
+	 * @var bool $as_attachment
64
+	 */
65
+	protected $as_attachment;
66 66
     
67 67
 	/**
68
-	* Contructor for ImageBuilder
69
-	*
70
-	* @param Media|null $media Reference media object
71
-	*/
68
+	 * Contructor for ImageBuilder
69
+	 *
70
+	 * @param Media|null $media Reference media object
71
+	 */
72 72
 	public function __construct(Media $media = null){
73
-	    $this->media = $media;
74
-	    $this->use_ttf = function_exists('imagettftext');
75
-	    $this->expire_offset = 3600 * 24;
76
-	    $this->show_watermark = true;
77
-	    $this->font_max_size = 18;
78
-	    $this->font_color = '#4D6DF3';
79
-	    $this->as_attachment = false;
73
+		$this->media = $media;
74
+		$this->use_ttf = function_exists('imagettftext');
75
+		$this->expire_offset = 3600 * 24;
76
+		$this->show_watermark = true;
77
+		$this->font_max_size = 18;
78
+		$this->font_color = '#4D6DF3';
79
+		$this->as_attachment = false;
80 80
 	}
81 81
 	
82 82
 	/**
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 	 * @return int
86 86
 	 */
87 87
 	public function getExpireOffset() {
88
-	    return $this->expire_offset;
88
+		return $this->expire_offset;
89 89
 	}
90 90
 	
91 91
 	/**
@@ -95,8 +95,8 @@  discard block
 block discarded – undo
95 95
 	 * @return ImageBuilder
96 96
 	 */
97 97
 	public function setExpireOffset($expireOffset) {
98
-	    if($expireOffset) $this->expire_offset = $expireOffset;
99
-	    return $this;
98
+		if($expireOffset) $this->expire_offset = $expireOffset;
99
+		return $this;
100 100
 	}
101 101
 	
102 102
 	/**
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
 	 * @return bool
106 106
 	 */
107 107
 	public function isShowWatermark() {
108
-	    return $this->show_watermark;
108
+		return $this->show_watermark;
109 109
 	}
110 110
 	
111 111
 	/**
@@ -115,8 +115,8 @@  discard block
 block discarded – undo
115 115
 	 * @return ImageBuilder
116 116
 	 */
117 117
 	public function setShowWatermark($show_watermark) {
118
-	    if(!is_null($show_watermark)) $this->show_watermark = $show_watermark;
119
-	    return $this;
118
+		if(!is_null($show_watermark)) $this->show_watermark = $show_watermark;
119
+		return $this;
120 120
 	}
121 121
 	
122 122
 	/**
@@ -126,8 +126,8 @@  discard block
 block discarded – undo
126 126
 	 * @return ImageBuilder
127 127
 	 */
128 128
 	public function setFontMaxSize($font_max_size) {
129
-	    if($font_max_size) $this->font_max_size = $font_max_size;
130
-	    return $this;
129
+		if($font_max_size) $this->font_max_size = $font_max_size;
130
+		return $this;
131 131
 	}
132 132
 	
133 133
 	/**
@@ -137,8 +137,8 @@  discard block
 block discarded – undo
137 137
 	 * @return ImageBuilder
138 138
 	 */
139 139
 	public function setFontColor($font_color) {
140
-	    if($font_color) $this->font_color = $font_color;
141
-	    return $this;
140
+		if($font_color) $this->font_color = $font_color;
141
+		return $this;
142 142
 	}
143 143
 	
144 144
 	/**
@@ -148,8 +148,8 @@  discard block
 block discarded – undo
148 148
 	 * @return ImageBuilder
149 149
 	 */
150 150
 	public function setAsAttachment($is_attachement) {
151
-	    if(is_bool($is_attachement)) $this->as_attachment = $is_attachement;
152
-	    return $this;
151
+		if(is_bool($is_attachement)) $this->as_attachment = $is_attachement;
152
+		return $this;
153 153
 	}
154 154
 	
155 155
 	/**
@@ -157,135 +157,135 @@  discard block
 block discarded – undo
157 157
 	 */
158 158
 	public function render(){
159 159
 	    
160
-	    if (!$this->media || !$this->media->canShow()) {
161
-	        Log::addMediaLog('Image Builder error: >' . I18N::translate('Missing or private media object.'));
162
-	        $this->renderError();
163
-	    }
160
+		if (!$this->media || !$this->media->canShow()) {
161
+			Log::addMediaLog('Image Builder error: >' . I18N::translate('Missing or private media object.'));
162
+			$this->renderError();
163
+		}
164 164
 	    
165
-	    $serverFilename = $this->media->getServerFilename();
165
+		$serverFilename = $this->media->getServerFilename();
166 166
 	    
167
-	    if (!file_exists($serverFilename)) {
168
-	        Log::addMediaLog('Image Builder error: >'. I18N::translate('The media object does not exist.').'< for path >'.$serverFilename.'<');
169
-	        $this->renderError();
170
-	    }
167
+		if (!file_exists($serverFilename)) {
168
+			Log::addMediaLog('Image Builder error: >'. I18N::translate('The media object does not exist.').'< for path >'.$serverFilename.'<');
169
+			$this->renderError();
170
+		}
171 171
 	    
172
-	    $mimetype = $this->media->mimeType();
173
-	    $imgsize = $this->media->getImageAttributes();
174
-	    $filetime = $this->media->getFiletime();
175
-	    $filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';	    
176
-	    $expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $this->getExpireOffset()) . ' GMT';
172
+		$mimetype = $this->media->mimeType();
173
+		$imgsize = $this->media->getImageAttributes();
174
+		$filetime = $this->media->getFiletime();
175
+		$filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';	    
176
+		$expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $this->getExpireOffset()) . ' GMT';
177 177
 	    
178
-	    $type = Functions::isImageTypeSupported($imgsize['ext']);
179
-	    $usewatermark = false;
180
-	    // if this image supports watermarks and the watermark module is intalled...
181
-	    if ($type) {
182
-	        $usewatermark = $this->isShowWatermark();
183
-	    }
178
+		$type = Functions::isImageTypeSupported($imgsize['ext']);
179
+		$usewatermark = false;
180
+		// if this image supports watermarks and the watermark module is intalled...
181
+		if ($type) {
182
+			$usewatermark = $this->isShowWatermark();
183
+		}
184 184
 	    
185
-	    // determine whether we have enough memory to watermark this image
186
-	    if ($usewatermark) {
187
-	        if (!FunctionsMedia::hasMemoryForImage($serverFilename)) {
188
-	            // not enough memory to watermark this file
189
-	            $usewatermark = false;
190
-	        }
191
-	    }
185
+		// determine whether we have enough memory to watermark this image
186
+		if ($usewatermark) {
187
+			if (!FunctionsMedia::hasMemoryForImage($serverFilename)) {
188
+				// not enough memory to watermark this file
189
+				$usewatermark = false;
190
+			}
191
+		}
192 192
 	    
193
-	    $etag = $this->media->getEtag();
193
+		$etag = $this->media->getEtag();
194 194
 	    
195
-	    // parse IF_MODIFIED_SINCE header from client
196
-	    $if_modified_since = 'x';
197
-	    if (!empty(Filter::server('HTTP_IF_MODIFIED_SINCE'))) {
198
-	        $if_modified_since = preg_replace('/;.*$/', '', Filter::server('HTTP_IF_MODIFIED_SINCE'));
199
-	    }
195
+		// parse IF_MODIFIED_SINCE header from client
196
+		$if_modified_since = 'x';
197
+		if (!empty(Filter::server('HTTP_IF_MODIFIED_SINCE'))) {
198
+			$if_modified_since = preg_replace('/;.*$/', '', Filter::server('HTTP_IF_MODIFIED_SINCE'));
199
+		}
200 200
 	    
201
-	    // parse IF_NONE_MATCH header from client
202
-	    $if_none_match = 'x';
203
-	    if (!empty(Filter::server('HTTP_IF_NONE_MATCH'))) {
204
-	        $if_none_match = str_replace('"', '', Filter::server('HTTP_IF_NONE_MATCH'));
205
-	    }
201
+		// parse IF_NONE_MATCH header from client
202
+		$if_none_match = 'x';
203
+		if (!empty(Filter::server('HTTP_IF_NONE_MATCH'))) {
204
+			$if_none_match = str_replace('"', '', Filter::server('HTTP_IF_NONE_MATCH'));
205
+		}
206 206
 	    
207
-	    // add caching headers.  allow browser to cache file, but not proxy
208
-	    header('Last-Modified: ' . $filetimeHeader);
209
-	    header('ETag: "' . $etag . '"');
210
-	    header('Expires: ' . $expireHeader);
211
-	    header('Cache-Control: max-age=' . $this->getExpireOffset() . ', s-maxage=0, proxy-revalidate');
207
+		// add caching headers.  allow browser to cache file, but not proxy
208
+		header('Last-Modified: ' . $filetimeHeader);
209
+		header('ETag: "' . $etag . '"');
210
+		header('Expires: ' . $expireHeader);
211
+		header('Cache-Control: max-age=' . $this->getExpireOffset() . ', s-maxage=0, proxy-revalidate');
212 212
 	    
213
-	    // if this file is already in the user’s cache, don’t resend it
214
-	    // first check if the if_modified_since param matches
215
-	    if ($if_modified_since === $filetimeHeader) {
216
-	        // then check if the etag matches
217
-	        if ($if_none_match === $etag) {
218
-	            http_response_code(304);
213
+		// if this file is already in the user’s cache, don’t resend it
214
+		// first check if the if_modified_since param matches
215
+		if ($if_modified_since === $filetimeHeader) {
216
+			// then check if the etag matches
217
+			if ($if_none_match === $etag) {
218
+				http_response_code(304);
219 219
 	    
220
-	            return;
221
-	        }
222
-	    }	    
220
+				return;
221
+			}
222
+		}	    
223 223
 
224
-	    // send headers for the image
225
-	    $disposition = $this->as_attachment ? 'attachment' : 'inline';
226
-	    header('Content-Type: ' . $mimetype);
227
-	    header('Content-Disposition: ' . $disposition . '; filename="' . addslashes(basename($this->media->getFilename())) . '"');
224
+		// send headers for the image
225
+		$disposition = $this->as_attachment ? 'attachment' : 'inline';
226
+		header('Content-Type: ' . $mimetype);
227
+		header('Content-Disposition: ' . $disposition . '; filename="' . addslashes(basename($this->media->getFilename())) . '"');
228 228
 	     
229
-	    if ($usewatermark) {
230
-	        // generate the watermarked image
231
-	        $imCreateFunc = 'imagecreatefrom' . $type;
232
-	        $imSendFunc   = 'image' . $type;
229
+		if ($usewatermark) {
230
+			// generate the watermarked image
231
+			$imCreateFunc = 'imagecreatefrom' . $type;
232
+			$imSendFunc   = 'image' . $type;
233 233
 	    
234
-	        if (function_exists($imCreateFunc) && function_exists($imSendFunc)) {
235
-	            $im = $imCreateFunc($serverFilename);
236
-	            $im = $this->applyWatermark($im);
234
+			if (function_exists($imCreateFunc) && function_exists($imSendFunc)) {
235
+				$im = $imCreateFunc($serverFilename);
236
+				$im = $this->applyWatermark($im);
237 237
 	    	    
238
-	            // send the image
239
-	            $imSendFunc($im);
240
-	            imagedestroy($im);
238
+				// send the image
239
+				$imSendFunc($im);
240
+				imagedestroy($im);
241 241
 	    
242
-	            return;
243
-	        } else {
244
-	            // this image is defective.  log it
245
-	            Log::addMediaLog('Image Builder error: >' . I18N::translate('This media file is broken and cannot be watermarked.') . '< in file >' . $serverFilename . '< memory used: ' . memory_get_usage());
246
-	        }
247
-	    }
242
+				return;
243
+			} else {
244
+				// this image is defective.  log it
245
+				Log::addMediaLog('Image Builder error: >' . I18N::translate('This media file is broken and cannot be watermarked.') . '< in file >' . $serverFilename . '< memory used: ' . memory_get_usage());
246
+			}
247
+		}
248 248
 	    
249
-	    // determine filesize of image (could be original or watermarked version)
250
-	    $filesize = filesize($serverFilename);
249
+		// determine filesize of image (could be original or watermarked version)
250
+		$filesize = filesize($serverFilename);
251 251
 	    
252
-	    // set content-length header, send file
253
-	    header('Content-Length: ' . $filesize);
252
+		// set content-length header, send file
253
+		header('Content-Length: ' . $filesize);
254 254
 	    
255
-	    // Some servers disable fpassthru() and readfile()
256
-	    if (function_exists('readfile')) {
257
-	        readfile($serverFilename);
258
-	    } else {
259
-	        $fp = fopen($serverFilename, 'rb');
260
-	        if (function_exists('fpassthru')) {
261
-	            fpassthru($fp);
262
-	        } else {
263
-	            while (!feof($fp)) {
264
-	                echo fread($fp, 65536);
265
-	            }
266
-	        }
267
-	        fclose($fp);
268
-	    }	    
255
+		// Some servers disable fpassthru() and readfile()
256
+		if (function_exists('readfile')) {
257
+			readfile($serverFilename);
258
+		} else {
259
+			$fp = fopen($serverFilename, 'rb');
260
+			if (function_exists('fpassthru')) {
261
+				fpassthru($fp);
262
+			} else {
263
+				while (!feof($fp)) {
264
+					echo fread($fp, 65536);
265
+				}
266
+			}
267
+			fclose($fp);
268
+		}	    
269 269
 	}
270 270
 	
271 271
 	/**
272 272
 	 * Render an error as an image.
273 273
 	 */
274 274
 	protected function renderError() {	
275
-	    $error = I18N::translate('The media file was not found in this family tree.');
275
+		$error = I18N::translate('The media file was not found in this family tree.');
276 276
 
277
-    	$width  = (mb_strlen($error) * 6.5 + 50) * 1.15;
278
-    	$height = 60;
279
-    	$im     = imagecreatetruecolor($width, $height); /* Create a black image */
280
-    	$bgc    = imagecolorallocate($im, 255, 255, 255); /* set background color */
281
-    	imagefilledrectangle($im, 2, 2, $width - 4, $height - 4, $bgc); /* create a rectangle, leaving 2 px border */
277
+		$width  = (mb_strlen($error) * 6.5 + 50) * 1.15;
278
+		$height = 60;
279
+		$im     = imagecreatetruecolor($width, $height); /* Create a black image */
280
+		$bgc    = imagecolorallocate($im, 255, 255, 255); /* set background color */
281
+		imagefilledrectangle($im, 2, 2, $width - 4, $height - 4, $bgc); /* create a rectangle, leaving 2 px border */
282 282
     
283
-    	$this->embedText($im, $error, 100, '255, 0, 0', WT_ROOT . Config::FONT_DEJAVU_SANS_TTF, 'top', 'left');
283
+		$this->embedText($im, $error, 100, '255, 0, 0', WT_ROOT . Config::FONT_DEJAVU_SANS_TTF, 'top', 'left');
284 284
     
285
-    	http_response_code(404);
286
-    	header('Content-Type: image/png');
287
-    	imagepng($im);
288
-    	imagedestroy($im);
285
+		http_response_code(404);
286
+		header('Content-Type: image/png');
287
+		imagepng($im);
288
+		imagedestroy($im);
289 289
 	}
290 290
 	
291 291
 	/**
@@ -297,25 +297,25 @@  discard block
 block discarded – undo
297 297
 	 */
298 298
 	protected function applyWatermark($im) {
299 299
 	    
300
-	    // text to watermark with	    
301
-	    if(method_exists($this->media, 'getWatermarkText')) {
302
-	       $word1_text = $this->media->getWatermarkText();
303
-	    }
304
-	    else {
305
-	        $word1_text = $this->media->getTitle();
306
-	    }
300
+		// text to watermark with	    
301
+		if(method_exists($this->media, 'getWatermarkText')) {
302
+		   $word1_text = $this->media->getWatermarkText();
303
+		}
304
+		else {
305
+			$word1_text = $this->media->getTitle();
306
+		}
307 307
 	
308
-	    $this->embedText(
309
-	        $im, 
310
-	        $word1_text, 
311
-	        $this->font_max_size,
312
-	        $this->font_color,
313
-	        WT_ROOT . Config::FONT_DEJAVU_SANS_TTF,
314
-	        'top', 
315
-	        'left'
316
-	     );
308
+		$this->embedText(
309
+			$im, 
310
+			$word1_text, 
311
+			$this->font_max_size,
312
+			$this->font_color,
313
+			WT_ROOT . Config::FONT_DEJAVU_SANS_TTF,
314
+			'top', 
315
+			'left'
316
+		 );
317 317
 	
318
-	    return ($im);
318
+		return ($im);
319 319
 	}
320 320
 	
321 321
 	/**
@@ -332,94 +332,94 @@  discard block
 block discarded – undo
332 332
 	 */
333 333
 	protected function embedText($im, $text, $maxsize, $color, $font, $vpos, $hpos) {
334 334
 	    
335
-	    // there are two ways to embed text with PHP
336
-	    // (preferred) using GD and FreeType you can embed text using any True Type font
337
-	    // (fall back) if that is not available, you can insert basic monospaced text
335
+		// there are two ways to embed text with PHP
336
+		// (preferred) using GD and FreeType you can embed text using any True Type font
337
+		// (fall back) if that is not available, you can insert basic monospaced text
338 338
 	    
339
-	    $col = $this->hexrgb($color);
340
-	    $textcolor = imagecolorallocate($im, $col['red'], $col['green'], $col['blue']);
339
+		$col = $this->hexrgb($color);
340
+		$textcolor = imagecolorallocate($im, $col['red'], $col['green'], $col['blue']);
341 341
 	    
342
-	    // make adjustments to settings that imagestring and imagestringup can’t handle
343
-	    if (!$this->use_ttf) {
344
-	        // imagestringup only writes up, can’t use top2bottom
345
-	        if ($hpos === 'top2bottom') {
346
-	            $hpos = 'bottom2top';
347
-	        }
348
-	    }
342
+		// make adjustments to settings that imagestring and imagestringup can’t handle
343
+		if (!$this->use_ttf) {
344
+			// imagestringup only writes up, can’t use top2bottom
345
+			if ($hpos === 'top2bottom') {
346
+				$hpos = 'bottom2top';
347
+			}
348
+		}
349 349
 	    
350
-	    $text       = I18N::reverseText($text);
351
-	    $height     = imagesy($im);
352
-	    $width      = imagesx($im);
353
-	    $calc_angle = rad2deg(atan($height / $width));
354
-	    $hypoth     = $height / sin(deg2rad($calc_angle));
350
+		$text       = I18N::reverseText($text);
351
+		$height     = imagesy($im);
352
+		$width      = imagesx($im);
353
+		$calc_angle = rad2deg(atan($height / $width));
354
+		$hypoth     = $height / sin(deg2rad($calc_angle));
355 355
 	    
356
-	    // vertical and horizontal position of the text
357
-	    switch ($vpos) {
358
-	        default:
359
-	        case 'top':
360
-	            $taille   = $this->textLength($maxsize, $width, $text);
361
-	            $pos_y    = $height * 0.15 + $taille;
362
-	            $pos_x    = $width * 0.15;
363
-	            $rotation = 0;
364
-	            break;
365
-	        case 'middle':
366
-	            $taille   = $this->textLength($maxsize, $width, $text);
367
-	            $pos_y    = ($height + $taille) / 2;
368
-	            $pos_x    = $width * 0.15;
369
-	            $rotation = 0;
370
-	            break;
371
-	        case 'bottom':
372
-	            $taille   = $this->textLength($maxsize, $width, $text);
373
-	            $pos_y    = ($height * .85 - $taille);
374
-	            $pos_x    = $width * 0.15;
375
-	            $rotation = 0;
376
-	            break;
377
-	        case 'across':
378
-	            switch ($hpos) {
379
-	                default:
380
-	                case 'left':
381
-	                    $taille   = $this->textLength($maxsize, $hypoth, $text);
382
-	                    $pos_y    = ($height * .85 - $taille);
383
-	                    $pos_x    = $width * 0.15;
384
-	                    $rotation = $calc_angle;
385
-	                    break;
386
-	                case 'right':
387
-	                    $taille   = $this->textLength($maxsize, $hypoth, $text);
388
-	                    $pos_y    = ($height * .15 - $taille);
389
-	                    $pos_x    = $width * 0.85;
390
-	                    $rotation = $calc_angle + 180;
391
-	                    break;
392
-	                case 'top2bottom':
393
-	                    $taille   = $this->textLength($maxsize, $height, $text);
394
-	                    $pos_y    = ($height * .15 - $taille);
395
-	                    $pos_x    = ($width * .90 - $taille);
396
-	                    $rotation = -90;
397
-	                    break;
398
-	                case 'bottom2top':
399
-	                    $taille   = $this->textLength($maxsize, $height, $text);
400
-	                    $pos_y    = $height * 0.85;
401
-	                    $pos_x    = $width * 0.15;
402
-	                    $rotation = 90;
403
-	                    break;
404
-	            }
405
-	            break;
406
-	    }
356
+		// vertical and horizontal position of the text
357
+		switch ($vpos) {
358
+			default:
359
+			case 'top':
360
+				$taille   = $this->textLength($maxsize, $width, $text);
361
+				$pos_y    = $height * 0.15 + $taille;
362
+				$pos_x    = $width * 0.15;
363
+				$rotation = 0;
364
+				break;
365
+			case 'middle':
366
+				$taille   = $this->textLength($maxsize, $width, $text);
367
+				$pos_y    = ($height + $taille) / 2;
368
+				$pos_x    = $width * 0.15;
369
+				$rotation = 0;
370
+				break;
371
+			case 'bottom':
372
+				$taille   = $this->textLength($maxsize, $width, $text);
373
+				$pos_y    = ($height * .85 - $taille);
374
+				$pos_x    = $width * 0.15;
375
+				$rotation = 0;
376
+				break;
377
+			case 'across':
378
+				switch ($hpos) {
379
+					default:
380
+					case 'left':
381
+						$taille   = $this->textLength($maxsize, $hypoth, $text);
382
+						$pos_y    = ($height * .85 - $taille);
383
+						$pos_x    = $width * 0.15;
384
+						$rotation = $calc_angle;
385
+						break;
386
+					case 'right':
387
+						$taille   = $this->textLength($maxsize, $hypoth, $text);
388
+						$pos_y    = ($height * .15 - $taille);
389
+						$pos_x    = $width * 0.85;
390
+						$rotation = $calc_angle + 180;
391
+						break;
392
+					case 'top2bottom':
393
+						$taille   = $this->textLength($maxsize, $height, $text);
394
+						$pos_y    = ($height * .15 - $taille);
395
+						$pos_x    = ($width * .90 - $taille);
396
+						$rotation = -90;
397
+						break;
398
+					case 'bottom2top':
399
+						$taille   = $this->textLength($maxsize, $height, $text);
400
+						$pos_y    = $height * 0.85;
401
+						$pos_x    = $width * 0.15;
402
+						$rotation = 90;
403
+						break;
404
+				}
405
+				break;
406
+		}
407 407
 	    
408
-	    // apply the text
409
-	    if ($this->use_ttf) {
410
-	        // if imagettftext throws errors, catch them with a custom error handler
411
-	        set_error_handler(array($this, 'imageTtfTextErrorHandler'));
412
-	        imagettftext($im, $taille, $rotation, $pos_x, $pos_y, $textcolor, $font, $text);
413
-	        restore_error_handler();
414
-	    }
415
-	    // Don’t use an ‘else’ here since imagettftextErrorHandler may have changed the value of $useTTF from true to false
416
-	    if (!$this->use_ttf) {
417
-	        if ($rotation !== 90) {
418
-	            imagestring($im, 5, $pos_x, $pos_y, $text, $textcolor);
419
-	        } else {
420
-	            imagestringup($im, 5, $pos_x, $pos_y, $text, $textcolor);
421
-	        }
422
-	    }
408
+		// apply the text
409
+		if ($this->use_ttf) {
410
+			// if imagettftext throws errors, catch them with a custom error handler
411
+			set_error_handler(array($this, 'imageTtfTextErrorHandler'));
412
+			imagettftext($im, $taille, $rotation, $pos_x, $pos_y, $textcolor, $font, $text);
413
+			restore_error_handler();
414
+		}
415
+		// Don’t use an ‘else’ here since imagettftextErrorHandler may have changed the value of $useTTF from true to false
416
+		if (!$this->use_ttf) {
417
+			if ($rotation !== 90) {
418
+				imagestring($im, 5, $pos_x, $pos_y, $text, $textcolor);
419
+			} else {
420
+				imagestringup($im, 5, $pos_x, $pos_y, $text, $textcolor);
421
+			}
422
+		}
423 423
 	
424 424
 	}
425 425
 	
@@ -431,53 +431,53 @@  discard block
 block discarded – undo
431 431
 	 */
432 432
 	protected function hexrgb ($hexstr)
433 433
 	{
434
-	    $int = hexdec($hexstr);
434
+		$int = hexdec($hexstr);
435 435
 	
436
-	    return array('red' => 0xFF & ($int >> 0x10),
437
-	        'green' => 0xFF & ($int >> 0x8),
438
-	        'blue' => 0xFF & $int);
436
+		return array('red' => 0xFF & ($int >> 0x10),
437
+			'green' => 0xFF & ($int >> 0x8),
438
+			'blue' => 0xFF & $int);
439 439
 	}
440 440
 	
441
-    /**
442
-     * Generate an approximate length of text, in pixels.
443
-     *
444
-     * @param int    $t
445
-     * @param int    $mxl
446
-     * @param string $text
447
-     *
448
-     * @return int
449
-     */
450
-    function textLength($t, $mxl, $text) {
451
-    	$taille_c = $t;
452
-    	$len      = mb_strlen($text);
453
-    	while (($taille_c - 2) * $len > $mxl) {
454
-    		$taille_c--;
455
-    		if ($taille_c == 2) {
456
-    			break;
457
-    		}
458
-    	}
441
+	/**
442
+	 * Generate an approximate length of text, in pixels.
443
+	 *
444
+	 * @param int    $t
445
+	 * @param int    $mxl
446
+	 * @param string $text
447
+	 *
448
+	 * @return int
449
+	 */
450
+	function textLength($t, $mxl, $text) {
451
+		$taille_c = $t;
452
+		$len      = mb_strlen($text);
453
+		while (($taille_c - 2) * $len > $mxl) {
454
+			$taille_c--;
455
+			if ($taille_c == 2) {
456
+				break;
457
+			}
458
+		}
459 459
     
460
-    	return $taille_c;
461
-    }
460
+		return $taille_c;
461
+	}
462 462
     
463
-    /**
464
-     * imagettftext is the function that is most likely to throw an error
465
-     * use this custom error handler to catch and log it
466
-     *
467
-     * @param int    $errno
468
-     * @param string $errstr
469
-     *
470
-     * @return bool
471
-     */
472
-    function imageTtfTextErrorHandler($errno, $errstr) {
473
-        // log the error
474
-        Log::addErrorLog('Image Builder error: >' . $errno . '/' . $errstr . '< while processing file >' . $this->media->getServerFilename() . '<');
463
+	/**
464
+	 * imagettftext is the function that is most likely to throw an error
465
+	 * use this custom error handler to catch and log it
466
+	 *
467
+	 * @param int    $errno
468
+	 * @param string $errstr
469
+	 *
470
+	 * @return bool
471
+	 */
472
+	function imageTtfTextErrorHandler($errno, $errstr) {
473
+		// log the error
474
+		Log::addErrorLog('Image Builder error: >' . $errno . '/' . $errstr . '< while processing file >' . $this->media->getServerFilename() . '<');
475 475
     
476
-        // change value of useTTF to false so the fallback watermarking can be used.
477
-        $this->use_ttf = false;
476
+		// change value of useTTF to false so the fallback watermarking can be used.
477
+		$this->use_ttf = false;
478 478
     
479
-        return true;
480
-    }
479
+		return true;
480
+	}
481 481
 		
482 482
 }
483 483
 
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	*
70 70
 	* @param Media|null $media Reference media object
71 71
 	*/
72
-	public function __construct(Media $media = null){
72
+	public function __construct(Media $media = null) {
73 73
 	    $this->media = $media;
74 74
 	    $this->use_ttf = function_exists('imagettftext');
75 75
 	    $this->expire_offset = 3600 * 24;
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 	 * @return ImageBuilder
96 96
 	 */
97 97
 	public function setExpireOffset($expireOffset) {
98
-	    if($expireOffset) $this->expire_offset = $expireOffset;
98
+	    if ($expireOffset) $this->expire_offset = $expireOffset;
99 99
 	    return $this;
100 100
 	}
101 101
 	
@@ -115,7 +115,7 @@  discard block
 block discarded – undo
115 115
 	 * @return ImageBuilder
116 116
 	 */
117 117
 	public function setShowWatermark($show_watermark) {
118
-	    if(!is_null($show_watermark)) $this->show_watermark = $show_watermark;
118
+	    if (!is_null($show_watermark)) $this->show_watermark = $show_watermark;
119 119
 	    return $this;
120 120
 	}
121 121
 	
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 	 * @return ImageBuilder
127 127
 	 */
128 128
 	public function setFontMaxSize($font_max_size) {
129
-	    if($font_max_size) $this->font_max_size = $font_max_size;
129
+	    if ($font_max_size) $this->font_max_size = $font_max_size;
130 130
 	    return $this;
131 131
 	}
132 132
 	
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
 	 * @return ImageBuilder
138 138
 	 */
139 139
 	public function setFontColor($font_color) {
140
-	    if($font_color) $this->font_color = $font_color;
140
+	    if ($font_color) $this->font_color = $font_color;
141 141
 	    return $this;
142 142
 	}
143 143
 	
@@ -148,32 +148,32 @@  discard block
 block discarded – undo
148 148
 	 * @return ImageBuilder
149 149
 	 */
150 150
 	public function setAsAttachment($is_attachement) {
151
-	    if(is_bool($is_attachement)) $this->as_attachment = $is_attachement;
151
+	    if (is_bool($is_attachement)) $this->as_attachment = $is_attachement;
152 152
 	    return $this;
153 153
 	}
154 154
 	
155 155
 	/**
156 156
 	 * Render the image to the output.
157 157
 	 */
158
-	public function render(){
158
+	public function render() {
159 159
 	    
160 160
 	    if (!$this->media || !$this->media->canShow()) {
161
-	        Log::addMediaLog('Image Builder error: >' . I18N::translate('Missing or private media object.'));
161
+	        Log::addMediaLog('Image Builder error: >'.I18N::translate('Missing or private media object.'));
162 162
 	        $this->renderError();
163 163
 	    }
164 164
 	    
165 165
 	    $serverFilename = $this->media->getServerFilename();
166 166
 	    
167 167
 	    if (!file_exists($serverFilename)) {
168
-	        Log::addMediaLog('Image Builder error: >'. I18N::translate('The media object does not exist.').'< for path >'.$serverFilename.'<');
168
+	        Log::addMediaLog('Image Builder error: >'.I18N::translate('The media object does not exist.').'< for path >'.$serverFilename.'<');
169 169
 	        $this->renderError();
170 170
 	    }
171 171
 	    
172 172
 	    $mimetype = $this->media->mimeType();
173 173
 	    $imgsize = $this->media->getImageAttributes();
174 174
 	    $filetime = $this->media->getFiletime();
175
-	    $filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';	    
176
-	    $expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $this->getExpireOffset()) . ' GMT';
175
+	    $filetimeHeader = gmdate('D, d M Y H:i:s', $filetime).' GMT';	    
176
+	    $expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $this->getExpireOffset()).' GMT';
177 177
 	    
178 178
 	    $type = Functions::isImageTypeSupported($imgsize['ext']);
179 179
 	    $usewatermark = false;
@@ -205,10 +205,10 @@  discard block
 block discarded – undo
205 205
 	    }
206 206
 	    
207 207
 	    // add caching headers.  allow browser to cache file, but not proxy
208
-	    header('Last-Modified: ' . $filetimeHeader);
209
-	    header('ETag: "' . $etag . '"');
210
-	    header('Expires: ' . $expireHeader);
211
-	    header('Cache-Control: max-age=' . $this->getExpireOffset() . ', s-maxage=0, proxy-revalidate');
208
+	    header('Last-Modified: '.$filetimeHeader);
209
+	    header('ETag: "'.$etag.'"');
210
+	    header('Expires: '.$expireHeader);
211
+	    header('Cache-Control: max-age='.$this->getExpireOffset().', s-maxage=0, proxy-revalidate');
212 212
 	    
213 213
 	    // if this file is already in the user’s cache, don’t resend it
214 214
 	    // first check if the if_modified_since param matches
@@ -223,13 +223,13 @@  discard block
 block discarded – undo
223 223
 
224 224
 	    // send headers for the image
225 225
 	    $disposition = $this->as_attachment ? 'attachment' : 'inline';
226
-	    header('Content-Type: ' . $mimetype);
227
-	    header('Content-Disposition: ' . $disposition . '; filename="' . addslashes(basename($this->media->getFilename())) . '"');
226
+	    header('Content-Type: '.$mimetype);
227
+	    header('Content-Disposition: '.$disposition.'; filename="'.addslashes(basename($this->media->getFilename())).'"');
228 228
 	     
229 229
 	    if ($usewatermark) {
230 230
 	        // generate the watermarked image
231
-	        $imCreateFunc = 'imagecreatefrom' . $type;
232
-	        $imSendFunc   = 'image' . $type;
231
+	        $imCreateFunc = 'imagecreatefrom'.$type;
232
+	        $imSendFunc   = 'image'.$type;
233 233
 	    
234 234
 	        if (function_exists($imCreateFunc) && function_exists($imSendFunc)) {
235 235
 	            $im = $imCreateFunc($serverFilename);
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
 	            return;
243 243
 	        } else {
244 244
 	            // this image is defective.  log it
245
-	            Log::addMediaLog('Image Builder error: >' . I18N::translate('This media file is broken and cannot be watermarked.') . '< in file >' . $serverFilename . '< memory used: ' . memory_get_usage());
245
+	            Log::addMediaLog('Image Builder error: >'.I18N::translate('This media file is broken and cannot be watermarked.').'< in file >'.$serverFilename.'< memory used: '.memory_get_usage());
246 246
 	        }
247 247
 	    }
248 248
 	    
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 	    $filesize = filesize($serverFilename);
251 251
 	    
252 252
 	    // set content-length header, send file
253
-	    header('Content-Length: ' . $filesize);
253
+	    header('Content-Length: '.$filesize);
254 254
 	    
255 255
 	    // Some servers disable fpassthru() and readfile()
256 256
 	    if (function_exists('readfile')) {
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
     	$bgc    = imagecolorallocate($im, 255, 255, 255); /* set background color */
281 281
     	imagefilledrectangle($im, 2, 2, $width - 4, $height - 4, $bgc); /* create a rectangle, leaving 2 px border */
282 282
     
283
-    	$this->embedText($im, $error, 100, '255, 0, 0', WT_ROOT . Config::FONT_DEJAVU_SANS_TTF, 'top', 'left');
283
+    	$this->embedText($im, $error, 100, '255, 0, 0', WT_ROOT.Config::FONT_DEJAVU_SANS_TTF, 'top', 'left');
284 284
     
285 285
     	http_response_code(404);
286 286
     	header('Content-Type: image/png');
@@ -298,7 +298,7 @@  discard block
 block discarded – undo
298 298
 	protected function applyWatermark($im) {
299 299
 	    
300 300
 	    // text to watermark with	    
301
-	    if(method_exists($this->media, 'getWatermarkText')) {
301
+	    if (method_exists($this->media, 'getWatermarkText')) {
302 302
 	       $word1_text = $this->media->getWatermarkText();
303 303
 	    }
304 304
 	    else {
@@ -310,7 +310,7 @@  discard block
 block discarded – undo
310 310
 	        $word1_text, 
311 311
 	        $this->font_max_size,
312 312
 	        $this->font_color,
313
-	        WT_ROOT . Config::FONT_DEJAVU_SANS_TTF,
313
+	        WT_ROOT.Config::FONT_DEJAVU_SANS_TTF,
314 314
 	        'top', 
315 315
 	        'left'
316 316
 	     );
@@ -429,7 +429,7 @@  discard block
 block discarded – undo
429 429
 	 * @param string $hexstr
430 430
 	 * @return int[]
431 431
 	 */
432
-	protected function hexrgb ($hexstr)
432
+	protected function hexrgb($hexstr)
433 433
 	{
434 434
 	    $int = hexdec($hexstr);
435 435
 	
@@ -471,7 +471,7 @@  discard block
 block discarded – undo
471 471
      */
472 472
     function imageTtfTextErrorHandler($errno, $errstr) {
473 473
         // log the error
474
-        Log::addErrorLog('Image Builder error: >' . $errno . '/' . $errstr . '< while processing file >' . $this->media->getServerFilename() . '<');
474
+        Log::addErrorLog('Image Builder error: >'.$errno.'/'.$errstr.'< while processing file >'.$this->media->getServerFilename().'<');
475 475
     
476 476
         // change value of useTTF to false so the fallback watermarking can be used.
477 477
         $this->use_ttf = false;
Please login to merge, or discard this patch.
Braces   +16 added lines, -7 removed lines patch added patch discarded remove patch
@@ -95,7 +95,9 @@  discard block
 block discarded – undo
95 95
 	 * @return ImageBuilder
96 96
 	 */
97 97
 	public function setExpireOffset($expireOffset) {
98
-	    if($expireOffset) $this->expire_offset = $expireOffset;
98
+	    if($expireOffset) {
99
+	    	$this->expire_offset = $expireOffset;
100
+	    }
99 101
 	    return $this;
100 102
 	}
101 103
 	
@@ -115,7 +117,9 @@  discard block
 block discarded – undo
115 117
 	 * @return ImageBuilder
116 118
 	 */
117 119
 	public function setShowWatermark($show_watermark) {
118
-	    if(!is_null($show_watermark)) $this->show_watermark = $show_watermark;
120
+	    if(!is_null($show_watermark)) {
121
+	    	$this->show_watermark = $show_watermark;
122
+	    }
119 123
 	    return $this;
120 124
 	}
121 125
 	
@@ -126,7 +130,9 @@  discard block
 block discarded – undo
126 130
 	 * @return ImageBuilder
127 131
 	 */
128 132
 	public function setFontMaxSize($font_max_size) {
129
-	    if($font_max_size) $this->font_max_size = $font_max_size;
133
+	    if($font_max_size) {
134
+	    	$this->font_max_size = $font_max_size;
135
+	    }
130 136
 	    return $this;
131 137
 	}
132 138
 	
@@ -137,7 +143,9 @@  discard block
 block discarded – undo
137 143
 	 * @return ImageBuilder
138 144
 	 */
139 145
 	public function setFontColor($font_color) {
140
-	    if($font_color) $this->font_color = $font_color;
146
+	    if($font_color) {
147
+	    	$this->font_color = $font_color;
148
+	    }
141 149
 	    return $this;
142 150
 	}
143 151
 	
@@ -148,7 +156,9 @@  discard block
 block discarded – undo
148 156
 	 * @return ImageBuilder
149 157
 	 */
150 158
 	public function setAsAttachment($is_attachement) {
151
-	    if(is_bool($is_attachement)) $this->as_attachment = $is_attachement;
159
+	    if(is_bool($is_attachement)) {
160
+	    	$this->as_attachment = $is_attachement;
161
+	    }
152 162
 	    return $this;
153 163
 	}
154 164
 	
@@ -300,8 +310,7 @@  discard block
 block discarded – undo
300 310
 	    // text to watermark with	    
301 311
 	    if(method_exists($this->media, 'getWatermarkText')) {
302 312
 	       $word1_text = $this->media->getWatermarkText();
303
-	    }
304
-	    else {
313
+	    } else {
305 314
 	        $word1_text = $this->media->getTitle();
306 315
 	    }
307 316
 	
Please login to merge, or discard this patch.