| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image\Gd; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\NotSupportedException; |
| 6 |
use Intervention\Image\Image; |
| 7 |
|
| 8 |
class Font extends \Intervention\Image\AbstractFont |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Get font size in points |
| 12 |
* |
| 13 |
* @return int |
| 14 |
*/ |
| 15 |
protected function getPointSize() |
| 16 |
{ |
| 17 |
return intval(ceil($this->size * 0.75)); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Filter function to access internal integer font values |
| 22 |
* |
| 23 |
* @return int |
| 24 |
*/ |
| 25 |
private function getInternalFont() |
| 26 |
{ |
| 27 |
$internalfont = is_null($this->file) ? 1 : $this->file; |
| 28 |
$internalfont = is_numeric($internalfont) ? $internalfont : false; |
| 29 |
|
| 30 |
if ( ! in_array($internalfont, [1, 2, 3, 4, 5])) { |
| 31 |
throw new NotSupportedException( |
| 32 |
sprintf('Internal GD font (%s) not available. Use only 1-5.', $internalfont) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
return intval($internalfont); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get width of an internal font character |
| 41 |
* |
| 42 |
* @return int |
| 43 |
*/ |
| 44 |
private function getInternalFontWidth() |
| 45 |
{ |
| 46 |
return $this->getInternalFont() + 4; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get height of an internal font character |
| 51 |
* |
| 52 |
* @return int |
| 53 |
*/ |
| 54 |
private function getInternalFontHeight() |
| 55 |
{ |
| 56 |
switch ($this->getInternalFont()) { |
| 57 |
case 1: |
| 58 |
return 8; |
| 59 |
|
| 60 |
case 2: |
| 61 |
return 14; |
| 62 |
|
| 63 |
case 3: |
| 64 |
return 14; |
| 65 |
|
| 66 |
case 4: |
| 67 |
return 16; |
| 68 |
|
| 69 |
case 5: |
| 70 |
return 16; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Calculates bounding box of current font setting |
| 76 |
* |
| 77 |
* @return Array |
| 78 |
*/ |
| 79 |
public function getBoxSize() |
| 80 |
{ |
| 81 |
$box = []; |
| 82 |
|
| 83 |
if ($this->hasApplicableFontFile()) { |
| 84 |
|
| 85 |
// imagettfbbox() converts numeric entities to their respective |
| 86 |
// character. Preserve any originally double encoded entities to be |
| 87 |
// represented as is. |
| 88 |
// eg: &#160; will render   rather than its character. |
| 89 |
$this->text = preg_replace('/&(#(?:x[a-fA-F0-9]+|[0-9]+);)/', '&\1', $this->text); |
| 90 |
$this->text = mb_encode_numericentity($this->text, array(0x0080, 0xffff, 0, 0xffff), 'UTF-8'); |
| 91 |
|
| 92 |
// get bounding box with angle 0 |
| 93 |
$box = imagettfbbox($this->getPointSize(), 0, $this->file, $this->text); |
| 94 |
|
| 95 |
// rotate points manually |
| 96 |
if ($this->angle != 0) { |
| 97 |
|
| 98 |
$angle = pi() * 2 - $this->angle * pi() * 2 / 360; |
| 99 |
|
| 100 |
for ($i=0; $i<4; $i++) { |
| 101 |
$x = $box[$i * 2]; |
| 102 |
$y = $box[$i * 2 + 1]; |
| 103 |
$box[$i * 2] = cos($angle) * $x - sin($angle) * $y; |
| 104 |
$box[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$box['width'] = intval(abs($box[4] - $box[0])); |
| 109 |
$box['height'] = intval(abs($box[5] - $box[1])); |
| 110 |
|
| 111 |
} else { |
| 112 |
|
| 113 |
// get current internal font size |
| 114 |
$width = $this->getInternalFontWidth(); |
| 115 |
$height = $this->getInternalFontHeight(); |
| 116 |
|
| 117 |
if (strlen($this->text) == 0) { |
| 118 |
// no text -> no boxsize |
| 119 |
$box['width'] = 0; |
| 120 |
$box['height'] = 0; |
| 121 |
} else { |
| 122 |
// calculate boxsize |
| 123 |
$box['width'] = strlen($this->text) * $width; |
| 124 |
$box['height'] = $height; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $box; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Draws font to given image at given position |
| 133 |
* |
| 134 |
* @param Image $image |
| 135 |
* @param int $posx |
| 136 |
* @param int $posy |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function applyToImage(Image $image, $posx = 0, $posy = 0) |
| 140 |
{ |
| 141 |
// parse text color |
| 142 |
$color = new Color($this->color); |
| 143 |
|
| 144 |
if ($this->hasApplicableFontFile()) { |
| 145 |
|
| 146 |
if ($this->angle != 0 || is_string($this->align) || is_string($this->valign)) { |
| 147 |
|
| 148 |
$box = $this->getBoxSize(); |
| 149 |
|
| 150 |
$align = is_null($this->align) ? 'left' : strtolower($this->align); |
| 151 |
$valign = is_null($this->valign) ? 'bottom' : strtolower($this->valign); |
| 152 |
|
| 153 |
// correction on position depending on v/h alignment |
| 154 |
switch ($align.'-'.$valign) { |
| 155 |
|
| 156 |
case 'center-top': |
| 157 |
$posx = $posx - round(($box[6]+$box[4])/2); |
| 158 |
$posy = $posy - round(($box[7]+$box[5])/2); |
| 159 |
break; |
| 160 |
|
| 161 |
case 'right-top': |
| 162 |
$posx = $posx - $box[4]; |
| 163 |
$posy = $posy - $box[5]; |
| 164 |
break; |
| 165 |
|
| 166 |
case 'left-top': |
| 167 |
$posx = $posx - $box[6]; |
| 168 |
$posy = $posy - $box[7]; |
| 169 |
break; |
| 170 |
|
| 171 |
case 'center-center': |
| 172 |
case 'center-middle': |
| 173 |
$posx = $posx - round(($box[0]+$box[4])/2); |
| 174 |
$posy = $posy - round(($box[1]+$box[5])/2); |
| 175 |
break; |
| 176 |
|
| 177 |
case 'right-center': |
| 178 |
case 'right-middle': |
| 179 |
$posx = $posx - round(($box[2]+$box[4])/2); |
| 180 |
$posy = $posy - round(($box[3]+$box[5])/2); |
| 181 |
break; |
| 182 |
|
| 183 |
case 'left-center': |
| 184 |
case 'left-middle': |
| 185 |
$posx = $posx - round(($box[0]+$box[6])/2); |
| 186 |
$posy = $posy - round(($box[1]+$box[7])/2); |
| 187 |
break; |
| 188 |
|
| 189 |
case 'center-bottom': |
| 190 |
$posx = $posx - round(($box[0]+$box[2])/2); |
| 191 |
$posy = $posy - round(($box[1]+$box[3])/2); |
| 192 |
break; |
| 193 |
|
| 194 |
case 'right-bottom': |
| 195 |
$posx = $posx - $box[2]; |
| 196 |
$posy = $posy - $box[3]; |
| 197 |
break; |
| 198 |
|
| 199 |
case 'left-bottom': |
| 200 |
$posx = $posx - $box[0]; |
| 201 |
$posy = $posy - $box[1]; |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// enable alphablending for imagettftext |
| 207 |
imagealphablending($image->getCore(), true); |
| 208 |
|
| 209 |
// draw ttf text |
| 210 |
imagettftext($image->getCore(), $this->getPointSize(), $this->angle, $posx, $posy, $color->getInt(), $this->file, $this->text); |
| 211 |
|
| 212 |
} else { |
| 213 |
|
| 214 |
// get box size |
| 215 |
$box = $this->getBoxSize(); |
| 216 |
$width = $box['width']; |
| 217 |
$height = $box['height']; |
| 218 |
|
| 219 |
// internal font specific position corrections |
| 220 |
if ($this->getInternalFont() == 1) { |
| 221 |
$top_correction = 1; |
| 222 |
$bottom_correction = 2; |
| 223 |
} elseif ($this->getInternalFont() == 3) { |
| 224 |
$top_correction = 2; |
| 225 |
$bottom_correction = 4; |
| 226 |
} else { |
| 227 |
$top_correction = 3; |
| 228 |
$bottom_correction = 4; |
| 229 |
} |
| 230 |
|
| 231 |
// x-position corrections for horizontal alignment |
| 232 |
switch (strtolower($this->align)) { |
| 233 |
case 'center': |
| 234 |
$posx = ceil($posx - ($width / 2)); |
| 235 |
break; |
| 236 |
|
| 237 |
case 'right': |
| 238 |
$posx = ceil($posx - $width) + 1; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
// y-position corrections for vertical alignment |
| 243 |
switch (strtolower($this->valign)) { |
| 244 |
case 'center': |
| 245 |
case 'middle': |
| 246 |
$posy = ceil($posy - ($height / 2)); |
| 247 |
break; |
| 248 |
|
| 249 |
case 'top': |
| 250 |
$posy = ceil($posy - $top_correction); |
| 251 |
break; |
| 252 |
|
| 253 |
default: |
| 254 |
case 'bottom': |
| 255 |
$posy = round($posy - $height + $bottom_correction); |
| 256 |
break; |
| 257 |
} |
| 258 |
|
| 259 |
// draw text |
| 260 |
imagestring($image->getCore(), $this->getInternalFont(), $posx, $posy, $this->text, $color->getInt()); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Set text kerning |
| 266 |
* |
| 267 |
* @param string $kerning |
| 268 |
* @return void |
| 269 |
*/ |
| 270 |
public function kerning($kerning) |
| 271 |
{ |
| 272 |
throw new \Intervention\Image\Exception\NotSupportedException( |
| 273 |
"Kerning is not supported by GD driver." |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|