Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Image |
||
| 28 | { |
||
| 29 | public $img = null; |
||
| 30 | public $rgb = null; |
||
| 31 | public $img_format; |
||
| 32 | public $ttf = null; |
||
| 33 | public $line_style = LINESTYLE_SOLID; |
||
| 34 | public $current_color; |
||
| 35 | public $current_color_name; |
||
| 36 | public $original_width = 0; |
||
| 37 | public $original_height = 0; |
||
| 38 | public $plotwidth = 0; |
||
| 39 | public $plotheight = 0; |
||
| 40 | |||
| 41 | // for __get, __set |
||
| 42 | private $_left_margin = 30; |
||
|
|
|||
| 43 | private $_right_margin = 30; |
||
| 44 | private $_top_margin = 20; |
||
| 45 | private $_bottom_margin = 30; |
||
| 46 | //private $_plotwidth=0,$_plotheight=0; |
||
|
1 ignored issue
–
show
|
|||
| 47 | private $_width = 0; |
||
| 48 | private $_height = 0; |
||
| 49 | private $_line_weight = 1; |
||
| 50 | |||
| 51 | protected $expired = true; |
||
| 52 | protected $lastx = 0; |
||
| 53 | protected $lasty = 0; |
||
| 54 | protected $obs_list = []; |
||
| 55 | protected $font_size = 12; |
||
| 56 | protected $font_family = FF_DEFAULT; |
||
| 57 | protected $font_style = FS_NORMAL; |
||
| 58 | protected $font_file = ''; |
||
| 59 | protected $text_halign = "left"; |
||
| 60 | protected $text_valign = "bottom"; |
||
| 61 | protected $use_anti_aliasing = false; |
||
| 62 | protected $quality = null; |
||
| 63 | protected $colorstack = []; |
||
| 64 | protected $colorstackidx = 0; |
||
| 65 | protected $canvascolor = 'white'; |
||
| 66 | protected $langconv = null; |
||
| 67 | protected $iInterlace = false; |
||
| 68 | protected $bbox_cache = []; // STore the last found tetx bounding box |
||
| 69 | protected $ff_font0; |
||
| 70 | protected $ff_font0_bold; |
||
| 71 | protected $ff_font1; |
||
| 72 | protected $ff_font1_bold; |
||
| 73 | protected $ff_font2; |
||
| 74 | protected $ff_font2_bold; |
||
| 75 | |||
| 76 | //--------------- |
||
| 77 | // CONSTRUCTOR |
||
| 78 | public function __construct($aWidth = 0, $aHeight = 0, $aFormat = DEFAULT_GFORMAT, $aSetAutoMargin = true) |
||
| 79 | { |
||
| 80 | $this->original_width = $aWidth; |
||
| 81 | $this->original_height = $aHeight; |
||
| 82 | $this->CreateImgCanvas($aWidth, $aHeight); |
||
| 83 | |||
| 84 | if ($aSetAutoMargin) { |
||
| 85 | $this->SetAutoMargin(); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (!$this->SetImgFormat($aFormat)) { |
||
| 89 | Util\JpGraphError::RaiseL(25081, $aFormat); //("JpGraph: Selected graphic format is either not supported or unknown [$aFormat]"); |
||
| 90 | } |
||
| 91 | $this->ttf = new TTF(); |
||
| 92 | $this->langconv = new LanguageConv(); |
||
| 93 | |||
| 94 | $this->ff_font0 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT0.gdf"); |
||
| 95 | $this->ff_font1 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1.gdf"); |
||
| 96 | $this->ff_font2 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2.gdf"); |
||
| 97 | $this->ff_font1_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1-Bold.gdf"); |
||
| 98 | $this->ff_font2_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2-Bold.gdf"); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Enable interlacing in images |
||
| 102 | public function SetInterlace($aFlg = true) |
||
| 103 | { |
||
| 104 | $this->iInterlace = $aFlg; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Should we use anti-aliasing. Note: This really slows down graphics! |
||
| 108 | public function SetAntiAliasing($aFlg = true) |
||
| 109 | { |
||
| 110 | $this->use_anti_aliasing = $aFlg; |
||
| 111 | if (function_exists('imageantialias')) { |
||
| 112 | imageantialias($this->img, $aFlg); |
||
| 113 | } /*else { |
||
|
1 ignored issue
–
show
|
|||
| 114 | Util\JpGraphError::RaiseL(25128); //('The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.') |
||
| 115 | }*/ |
||
| 116 | } |
||
| 117 | |||
| 118 | public function GetAntiAliasing() |
||
| 119 | { |
||
| 120 | return $this->use_anti_aliasing; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function CreateRawCanvas($aWidth = 0, $aHeight = 0) |
||
| 124 | { |
||
| 125 | $aWidth *= SUPERSAMPLING_SCALE; |
||
| 126 | $aHeight *= SUPERSAMPLING_SCALE; |
||
| 127 | |||
| 128 | if ($aWidth <= 1 || $aHeight <= 1) { |
||
| 129 | Util\JpGraphError::RaiseL(25082, $aWidth, $aHeight); //("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)"); |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->img = @imagecreatetruecolor($aWidth, $aHeight); |
||
| 133 | if ($this->img < 1) { |
||
| 134 | Util\JpGraphError::RaiseL(25126); |
||
| 135 | //die("Can't create truecolor image. Check that you really have GD2 library installed."); |
||
| 136 | } |
||
| 137 | $this->SetAlphaBlending(); |
||
| 138 | |||
| 139 | if ($this->iInterlace) { |
||
| 140 | imageinterlace($this->img, 1); |
||
| 141 | } |
||
| 142 | if ($this->rgb != null) { |
||
| 143 | $this->rgb->img = $this->img; |
||
| 144 | } else { |
||
| 145 | $this->rgb = new RGB($this->img); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public function CloneCanvasH() |
||
| 150 | { |
||
| 151 | $oldimage = $this->img; |
||
| 152 | $this->CreateRawCanvas($this->width, $this->height); |
||
| 153 | imagecopy($this->img, $oldimage, 0, 0, 0, 0, $this->width, $this->height); |
||
| 154 | return $oldimage; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function CreateImgCanvas($aWidth = 0, $aHeight = 0) |
||
| 158 | { |
||
| 159 | $old = [$this->img, $this->width, $this->height]; |
||
| 160 | |||
| 161 | $aWidth = round($aWidth); |
||
| 162 | $aHeight = round($aHeight); |
||
| 163 | |||
| 164 | $this->width = $aWidth; |
||
| 165 | $this->height = $aHeight; |
||
| 166 | |||
| 167 | if ($aWidth == 0 || $aHeight == 0) { |
||
| 168 | // We will set the final size later. |
||
| 169 | // Note: The size must be specified before any other |
||
| 170 | // img routines that stroke anything are called. |
||
| 171 | $this->img = null; |
||
| 172 | $this->rgb = null; |
||
| 173 | return $old; |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->CreateRawCanvas($aWidth, $aHeight); |
||
| 177 | // Set canvas color (will also be the background color for a |
||
| 178 | // a pallett image |
||
| 179 | $this->SetColor($this->canvascolor); |
||
| 180 | $this->FilledRectangle(0, 0, $this->width - 1, $this->height - 1); |
||
| 181 | |||
| 182 | return $old; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function CopyCanvasH($aToHdl, $aFromHdl, $aToX, $aToY, $aFromX, $aFromY, $aWidth, $aHeight, $aw = -1, $ah = -1) |
||
| 186 | { |
||
| 187 | if ($aw === -1) { |
||
| 188 | $aw = $aWidth; |
||
| 189 | $ah = $aHeight; |
||
| 190 | $f = 'imagecopyresized'; |
||
| 191 | } else { |
||
| 192 | $f = 'imagecopyresampled'; |
||
| 193 | } |
||
| 194 | $f($aToHdl, $aFromHdl, $aToX, $aToY, $aFromX, $aFromY, $aWidth, $aHeight, $aw, $ah); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function Copy($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth = -1, $fromHeight = -1) |
||
| 198 | { |
||
| 199 | $this->CopyCanvasH($this->img, $fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth, $fromHeight); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function CopyMerge($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth = -1, $fromHeight = -1, $aMix = 100) |
||
| 203 | { |
||
| 204 | if ($aMix == 100) { |
||
| 205 | $this->CopyCanvasH($this->img, $fromImg, |
||
| 206 | $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth, $fromHeight); |
||
| 207 | } else { |
||
| 208 | if (($fromWidth != -1 && ($fromWidth != $toWidth)) || ($fromHeight != -1 && ($fromHeight != $fromHeight))) { |
||
| 209 | // Create a new canvas that will hold the re-scaled original from image |
||
| 210 | if ($toWidth <= 1 || $toHeight <= 1) { |
||
| 211 | Util\JpGraphError::RaiseL(25083); //('Illegal image size when copying image. Size for copied to image is 1 pixel or less.'); |
||
| 212 | } |
||
| 213 | |||
| 214 | $tmpimg = @imagecreatetruecolor($toWidth, $toHeight); |
||
| 215 | |||
| 216 | if ($tmpimg < 1) { |
||
| 217 | Util\JpGraphError::RaiseL(25084); //('Failed to create temporary GD canvas. Out of memory ?'); |
||
| 218 | } |
||
| 219 | $this->CopyCanvasH($tmpimg, $fromImg, 0, 0, 0, 0, |
||
| 220 | $toWidth, $toHeight, $fromWidth, $fromHeight); |
||
| 221 | $fromImg = $tmpimg; |
||
| 222 | } |
||
| 223 | imagecopymerge($this->img, $fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $aMix); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | public static function GetWidth($aImg = null) |
||
| 228 | { |
||
| 229 | if ($aImg === null) { |
||
| 230 | $aImg = $this->img; |
||
| 231 | } |
||
| 232 | return imagesx($aImg); |
||
| 233 | } |
||
| 234 | |||
| 235 | public static function GetHeight($aImg = null) |
||
| 236 | { |
||
| 237 | if ($aImg === null) { |
||
| 238 | $aImg = $this->img; |
||
| 239 | } |
||
| 240 | return imagesy($aImg); |
||
| 241 | } |
||
| 242 | |||
| 243 | public static function CreateFromString($aStr) |
||
| 244 | { |
||
| 245 | $img = imagecreatefromstring($aStr); |
||
| 246 | if ($img === false) { |
||
| 247 | Util\JpGraphError::RaiseL(25085); |
||
| 248 | //('An image can not be created from the supplied string. It is either in a format not supported or the string is representing an corrupt image.'); |
||
| 249 | } |
||
| 250 | return $img; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function SetCanvasH($aHdl) |
||
| 254 | { |
||
| 255 | $this->img = $aHdl; |
||
| 256 | $this->rgb->img = $aHdl; |
||
| 257 | } |
||
| 258 | |||
| 259 | public function SetCanvasColor($aColor) |
||
| 260 | { |
||
| 261 | $this->canvascolor = $aColor; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function SetAlphaBlending($aFlg = true) |
||
| 265 | { |
||
| 266 | ImageAlphaBlending($this->img, $aFlg); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function SetAutoMargin() |
||
| 270 | { |
||
| 271 | $min_bm = 5; |
||
| 272 | $lm = min(40, $this->width / 7); |
||
| 273 | $rm = min(20, $this->width / 10); |
||
| 274 | $tm = max(5, $this->height / 7); |
||
| 275 | $bm = max($min_bm, $this->height / 6); |
||
| 276 | $this->SetMargin($lm, $rm, $tm, $bm); |
||
| 277 | } |
||
| 278 | |||
| 279 | //--------------- |
||
| 280 | // PUBLIC METHODS |
||
| 281 | |||
| 282 | public function SetFont($family, $style = FS_NORMAL, $size = 10) |
||
| 283 | { |
||
| 284 | $this->font_family = $family; |
||
| 285 | $this->font_style = $style; |
||
| 286 | $this->font_size = $size * SUPERSAMPLING_SCALE; |
||
| 287 | $this->font_file = ''; |
||
| 288 | if (($this->font_family == FF_FONT1 || $this->font_family == FF_FONT2) && $this->font_style == FS_BOLD) { |
||
| 289 | ++$this->font_family; |
||
| 290 | } |
||
| 291 | if ($this->font_family > FF_FONT2 + 1) { |
||
| 292 | // A TTF font so get the font file |
||
| 293 | |||
| 294 | // Check that this PHP has support for TTF fonts |
||
| 295 | if (!function_exists('imagettfbbox')) { |
||
| 296 | // use internal font when php is configured without '--with-ttf' |
||
| 297 | $this->font_family = FF_FONT1; |
||
| 298 | // Util\JpGraphError::RaiseL(25087);//('This PHP build has not been configured with TTF support. You need to recompile your PHP installation with FreeType support.'); |
||
|
1 ignored issue
–
show
|
|||
| 299 | } else { |
||
| 300 | $this->font_file = $this->ttf->File($this->font_family, $this->font_style); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | // Get the specific height for a text string |
||
| 306 | public function GetTextHeight($txt = "", $angle = 0) |
||
| 307 | { |
||
| 308 | $tmp = preg_split('/\n/', $txt); |
||
| 309 | $n = count($tmp); |
||
| 310 | $m = 0; |
||
| 311 | for ($i = 0; $i < $n; ++$i) { |
||
| 312 | $m = max($m, strlen($tmp[$i])); |
||
| 313 | } |
||
| 314 | |||
| 315 | if ($this->font_family <= FF_FONT2 + 1) { |
||
| 316 | View Code Duplication | if ($angle == 0) { |
|
| 317 | $h = imagefontheight($this->font_family); |
||
| 318 | if ($h === false) { |
||
| 319 | Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.'); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $n * $h; |
||
| 323 | } else { |
||
| 324 | $w = @imagefontwidth($this->font_family); |
||
| 325 | if ($w === false) { |
||
| 326 | Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.'); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $m * $w; |
||
| 330 | } |
||
| 331 | } else { |
||
| 332 | $bbox = $this->GetTTFBBox($txt, $angle); |
||
| 333 | return $bbox[1] - $bbox[5] + 1; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | // Estimate font height |
||
| 338 | public function GetFontHeight($angle = 0) |
||
| 339 | { |
||
| 340 | $txt = "XOMg"; |
||
| 341 | return $this->GetTextHeight($txt, $angle); |
||
| 342 | } |
||
| 343 | |||
| 344 | // Approximate font width with width of letter "O" |
||
| 345 | public function GetFontWidth($angle = 0) |
||
| 346 | { |
||
| 347 | $txt = 'O'; |
||
| 348 | return $this->GetTextWidth($txt, $angle); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Get actual width of text in absolute pixels. Note that the width is the |
||
| 352 | // texts projected with onto the x-axis. Call with angle=0 to get the true |
||
| 353 | // etxt width. |
||
| 354 | public function GetTextWidth($txt, $angle = 0) |
||
| 355 | { |
||
| 356 | $tmp = preg_split('/\n/', $txt); |
||
| 357 | $n = count($tmp); |
||
| 358 | if ($this->font_family <= FF_FONT2 + 1) { |
||
| 359 | $m = 0; |
||
| 360 | for ($i = 0; $i < $n; ++$i) { |
||
| 361 | $l = strlen($tmp[$i]); |
||
| 362 | if ($l > $m) { |
||
| 363 | $m = $l; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | View Code Duplication | if ($angle == 0) { |
|
| 368 | $w = @imagefontwidth($this->font_family); |
||
| 369 | if ($w === false) { |
||
| 370 | Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.'); |
||
| 371 | } |
||
| 372 | return $m * $w; |
||
| 373 | } else { |
||
| 374 | // 90 degrees internal so height becomes width |
||
| 375 | $h = @imagefontheight($this->font_family); |
||
| 376 | if ($h === false) { |
||
| 377 | Util\JpGraphError::RaiseL(25089); //('You have a misconfigured GD font support. The call to imagefontheight() fails.'); |
||
| 378 | } |
||
| 379 | return $n * $h; |
||
| 380 | } |
||
| 381 | } else { |
||
| 382 | // For TTF fonts we must walk through a lines and find the |
||
| 383 | // widest one which we use as the width of the multi-line |
||
| 384 | // paragraph |
||
| 385 | $m = 0; |
||
| 386 | for ($i = 0; $i < $n; ++$i) { |
||
| 387 | $bbox = $this->GetTTFBBox($tmp[$i], $angle); |
||
| 388 | $mm = $bbox[2] - $bbox[0]; |
||
| 389 | if ($mm > $m) { |
||
| 390 | $m = $mm; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | return $m; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | // Draw text with a box around it |
||
| 398 | public function StrokeBoxedText($x, $y, $txt, $dir = 0, $fcolor = "white", $bcolor = "black", |
||
| 399 | $shadowcolor = false, $paragraph_align = "left", |
||
| 400 | $xmarg = 6, $ymarg = 4, $cornerradius = 0, $dropwidth = 3) |
||
| 401 | { |
||
| 402 | $oldx = $this->lastx; |
||
| 403 | $oldy = $this->lasty; |
||
| 404 | |||
| 405 | View Code Duplication | if (!is_numeric($dir)) { |
|
| 406 | if ($dir == "h") { |
||
| 407 | $dir = 0; |
||
| 408 | } elseif ($dir == "v") { |
||
| 409 | $dir = 90; |
||
| 410 | } else { |
||
| 411 | Util\JpGraphError::RaiseL(25090, $dir); |
||
| 412 | } |
||
| 413 | //(" Unknown direction specified in call to StrokeBoxedText() [$dir]"); |
||
| 414 | } |
||
| 415 | |||
| 416 | if ($this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2 + 1) { |
||
| 417 | $width = $this->GetTextWidth($txt, $dir); |
||
| 418 | $height = $this->GetTextHeight($txt, $dir); |
||
| 419 | } else { |
||
| 420 | $width = $this->GetBBoxWidth($txt, $dir); |
||
| 421 | $height = $this->GetBBoxHeight($txt, $dir); |
||
| 422 | } |
||
| 423 | |||
| 424 | $height += 2 * $ymarg; |
||
| 425 | $width += 2 * $xmarg; |
||
| 426 | |||
| 427 | if ($this->text_halign == "right") { |
||
| 428 | $x -= $width; |
||
| 429 | } elseif ($this->text_halign == "center") { |
||
| 430 | $x -= $width / 2; |
||
| 431 | } |
||
| 432 | |||
| 433 | if ($this->text_valign == "bottom") { |
||
| 434 | $y -= $height; |
||
| 435 | } elseif ($this->text_valign == "center") { |
||
| 436 | $y -= $height / 2; |
||
| 437 | } |
||
| 438 | |||
| 439 | $olda = $this->SetAngle(0); |
||
| 440 | |||
| 441 | if ($shadowcolor) { |
||
| 442 | $this->PushColor($shadowcolor); |
||
| 443 | $this->FilledRoundedRectangle($x - $xmarg + $dropwidth, $y - $ymarg + $dropwidth, |
||
| 444 | $x + $width + $dropwidth, $y + $height - $ymarg + $dropwidth, |
||
| 445 | $cornerradius); |
||
| 446 | $this->PopColor(); |
||
| 447 | $this->PushColor($fcolor); |
||
| 448 | $this->FilledRoundedRectangle($x - $xmarg, $y - $ymarg, |
||
| 449 | $x + $width, $y + $height - $ymarg, |
||
| 450 | $cornerradius); |
||
| 451 | $this->PopColor(); |
||
| 452 | $this->PushColor($bcolor); |
||
| 453 | $this->RoundedRectangle($x - $xmarg, $y - $ymarg, |
||
| 454 | $x + $width, $y + $height - $ymarg, $cornerradius); |
||
| 455 | $this->PopColor(); |
||
| 456 | } else { |
||
| 457 | View Code Duplication | if ($fcolor) { |
|
| 458 | $oc = $this->current_color; |
||
| 459 | $this->SetColor($fcolor); |
||
| 460 | $this->FilledRoundedRectangle($x - $xmarg, $y - $ymarg, $x + $width, $y + $height - $ymarg, $cornerradius); |
||
| 461 | $this->current_color = $oc; |
||
| 462 | } |
||
| 463 | View Code Duplication | if ($bcolor) { |
|
| 464 | $oc = $this->current_color; |
||
| 465 | $this->SetColor($bcolor); |
||
| 466 | $this->RoundedRectangle($x - $xmarg, $y - $ymarg, $x + $width, $y + $height - $ymarg, $cornerradius); |
||
| 467 | $this->current_color = $oc; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | $h = $this->text_halign; |
||
| 472 | $v = $this->text_valign; |
||
| 473 | $this->SetTextAlign("left", "top"); |
||
| 474 | |||
| 475 | $debug = false; |
||
| 476 | $this->StrokeText($x, $y, $txt, $dir, $paragraph_align, $debug); |
||
| 477 | |||
| 478 | $bb = [$x - $xmarg, $y + $height - $ymarg, $x + $width, $y + $height - $ymarg, |
||
| 479 | $x + $width, $y - $ymarg, $x - $xmarg, $y - $ymarg]; |
||
| 480 | $this->SetTextAlign($h, $v); |
||
| 481 | |||
| 482 | $this->SetAngle($olda); |
||
| 483 | $this->lastx = $oldx; |
||
| 484 | $this->lasty = $oldy; |
||
| 485 | |||
| 486 | return $bb; |
||
| 487 | } |
||
| 488 | |||
| 489 | // Draw text with a box around it. This time the box will be rotated |
||
| 490 | // with the text. The previous method will just make a larger enough non-rotated |
||
| 491 | // box to hold the text inside. |
||
| 492 | public function StrokeBoxedText2($x, $y, $txt, $dir = 0, $fcolor = "white", $bcolor = "black", |
||
| 493 | $shadowcolor = false, $paragraph_align = "left", |
||
| 494 | $xmarg = 6, $ymarg = 4, $cornerradius = 0, $dropwidth = 3) |
||
| 495 | { |
||
| 496 | |||
| 497 | // This version of boxed text will stroke a rotated box round the text |
||
| 498 | // thta will follow the angle of the text. |
||
| 499 | // This has two implications: |
||
| 500 | // 1) This methos will only support TTF fonts |
||
| 501 | // 2) The only two alignment that makes sense are centered or baselined |
||
| 502 | |||
| 503 | if ($this->font_family <= FF_FONT2 + 1) { |
||
| 504 | Util\JpGraphError::RaiseL(25131); //StrokeBoxedText2() Only support TTF fonts and not built in bitmap fonts |
||
| 505 | } |
||
| 506 | |||
| 507 | $oldx = $this->lastx; |
||
| 508 | $oldy = $this->lasty; |
||
| 509 | $dir = $this->NormAngle($dir); |
||
| 510 | |||
| 511 | View Code Duplication | if (!is_numeric($dir)) { |
|
| 512 | if ($dir == "h") { |
||
| 513 | $dir = 0; |
||
| 514 | } elseif ($dir == "v") { |
||
| 515 | $dir = 90; |
||
| 516 | } else { |
||
| 517 | Util\JpGraphError::RaiseL(25090, $dir); |
||
| 518 | } |
||
| 519 | //(" Unknown direction specified in call to StrokeBoxedText() [$dir]"); |
||
| 520 | } |
||
| 521 | |||
| 522 | $width = $this->GetTextWidth($txt, 0) + 2 * $xmarg; |
||
| 523 | $height = $this->GetTextHeight($txt, 0) + 2 * $ymarg; |
||
| 524 | $rect_width = $this->GetBBoxWidth($txt, $dir); |
||
| 525 | $rect_height = $this->GetBBoxHeight($txt, $dir); |
||
| 526 | |||
| 527 | $baseline_offset = $this->bbox_cache[1] - 1; |
||
| 528 | |||
| 529 | if ($this->text_halign == "center") { |
||
| 530 | if ($dir >= 0 && $dir <= 90) { |
||
| 531 | $x -= $rect_width / 2; |
||
| 532 | $x += sin($dir * M_PI / 180) * $height; |
||
| 533 | $y += $rect_height / 2; |
||
| 534 | } elseif ($dir >= 270 && $dir <= 360) { |
||
| 535 | $x -= $rect_width / 2; |
||
| 536 | $y -= $rect_height / 2; |
||
| 537 | $y += cos($dir * M_PI / 180) * $height; |
||
| 538 | } elseif ($dir >= 90 && $dir <= 180) { |
||
| 539 | $x += $rect_width / 2; |
||
| 540 | $y += $rect_height / 2; |
||
| 541 | $y += cos($dir * M_PI / 180) * $height; |
||
| 542 | } else { |
||
| 543 | // $dir > 180 && $dir < 270 |
||
|
1 ignored issue
–
show
|
|||
| 544 | $x += $rect_width / 2; |
||
| 545 | $x += sin($dir * M_PI / 180) * $height; |
||
| 546 | $y -= $rect_height / 2; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | // Rotate the box around this point |
||
| 551 | $this->SetCenter($x, $y); |
||
| 552 | $olda = $this->SetAngle(-$dir); |
||
| 553 | |||
| 554 | // We need to use adjusted coordinats for the box to be able |
||
| 555 | // to draw the box below the baseline. This cannot be done before since |
||
| 556 | // the rotating point must be the original x,y since that is arounbf the |
||
| 557 | // point where the text will rotate and we cannot change this since |
||
| 558 | // that is where the GD/GreeType will rotate the text |
||
| 559 | |||
| 560 | // For smaller <14pt font we need to do some additional |
||
| 561 | // adjustments to make it look good |
||
| 562 | if ($this->font_size < 14) { |
||
| 563 | $x -= 2; |
||
| 564 | $y += 2; |
||
| 565 | } else { |
||
| 566 | // $y += $baseline_offset; |
||
|
1 ignored issue
–
show
|
|||
| 567 | } |
||
| 568 | |||
| 569 | if ($shadowcolor) { |
||
| 570 | $this->PushColor($shadowcolor); |
||
| 571 | $this->FilledRectangle($x - $xmarg + $dropwidth, $y + $ymarg + $dropwidth - $height, |
||
| 572 | $x + $width + $dropwidth, $y + $ymarg + $dropwidth); |
||
| 573 | //$cornerradius); |
||
| 574 | $this->PopColor(); |
||
| 575 | $this->PushColor($fcolor); |
||
| 576 | $this->FilledRectangle($x - $xmarg, $y + $ymarg - $height, |
||
| 577 | $x + $width, $y + $ymarg); |
||
| 578 | //$cornerradius); |
||
| 579 | $this->PopColor(); |
||
| 580 | $this->PushColor($bcolor); |
||
| 581 | $this->Rectangle($x - $xmarg, $y + $ymarg - $height, |
||
| 582 | $x + $width, $y + $ymarg); |
||
| 583 | //$cornerradius); |
||
| 584 | $this->PopColor(); |
||
| 585 | } else { |
||
| 586 | View Code Duplication | if ($fcolor) { |
|
| 587 | $oc = $this->current_color; |
||
| 588 | $this->SetColor($fcolor); |
||
| 589 | $this->FilledRectangle($x - $xmarg, $y + $ymarg - $height, $x + $width, $y + $ymarg); //,$cornerradius); |
||
| 590 | $this->current_color = $oc; |
||
| 591 | } |
||
| 592 | View Code Duplication | if ($bcolor) { |
|
| 593 | $oc = $this->current_color; |
||
| 594 | $this->SetColor($bcolor); |
||
| 595 | $this->Rectangle($x - $xmarg, $y + $ymarg - $height, $x + $width, $y + $ymarg); //,$cornerradius); |
||
| 596 | $this->current_color = $oc; |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | if ($this->font_size < 14) { |
||
| 601 | $x += 2; |
||
| 602 | $y -= 2; |
||
| 603 | } else { |
||
| 604 | |||
| 605 | // Restore the original y before we stroke the text |
||
| 606 | // $y -= $baseline_offset; |
||
|
1 ignored issue
–
show
|
|||
| 607 | } |
||
| 608 | |||
| 609 | $this->SetCenter(0, 0); |
||
| 610 | $this->SetAngle($olda); |
||
| 611 | |||
| 612 | $h = $this->text_halign; |
||
| 613 | $v = $this->text_valign; |
||
| 614 | if ($this->text_halign == 'center') { |
||
| 615 | $this->SetTextAlign('center', 'basepoint'); |
||
| 616 | } else { |
||
| 617 | $this->SetTextAlign('basepoint', 'basepoint'); |
||
| 618 | } |
||
| 619 | |||
| 620 | $debug = false; |
||
| 621 | $this->StrokeText($x, $y, $txt, $dir, $paragraph_align, $debug); |
||
| 622 | |||
| 623 | $bb = [$x - $xmarg, $y + $height - $ymarg, |
||
| 624 | $x + $width, $y + $height - $ymarg, |
||
| 625 | $x + $width, $y - $ymarg, |
||
| 626 | $x - $xmarg, $y - $ymarg]; |
||
| 627 | |||
| 628 | $this->SetTextAlign($h, $v); |
||
| 629 | $this->SetAngle($olda); |
||
| 630 | |||
| 631 | $this->lastx = $oldx; |
||
| 632 | $this->lasty = $oldy; |
||
| 633 | |||
| 634 | return $bb; |
||
| 635 | } |
||
| 636 | |||
| 637 | // Set text alignment |
||
| 638 | public function SetTextAlign($halign, $valign = "bottom") |
||
| 643 | |||
| 644 | public function _StrokeBuiltinFont($x, $y, $txt, $dir, $paragraph_align, &$aBoundingBox, $aDebug = false) |
||
| 645 | { |
||
| 646 | if (is_numeric($dir) && $dir != 90 && $dir != 0) { |
||
| 647 | Util\JpGraphError::RaiseL(25091); |
||
| 648 | } |
||
| 649 | //(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead."); |
||
| 650 | |||
| 651 | $h = $this->GetTextHeight($txt); |
||
| 652 | $fh = $this->GetFontHeight(); |
||
| 653 | $w = $this->GetTextWidth($txt); |
||
| 654 | |||
| 655 | View Code Duplication | if ($this->text_halign == "right") { |
|
| 656 | $x -= $dir == 0 ? $w : $h; |
||
| 657 | } elseif ($this->text_halign == "center") { |
||
| 658 | // For center we subtract 1 pixel since this makes the middle |
||
| 659 | // be prefectly in the middle |
||
| 660 | $x -= $dir == 0 ? $w / 2 - 1 : $h / 2; |
||
| 661 | } |
||
| 662 | View Code Duplication | if ($this->text_valign == "top") { |
|
| 663 | $y += $dir == 0 ? $h : $w; |
||
| 664 | } elseif ($this->text_valign == "center") { |
||
| 665 | $y += $dir == 0 ? $h / 2 : $w / 2; |
||
| 666 | } |
||
| 667 | |||
| 708 | |||
| 709 | public function AddTxtCR($aTxt) |
||
| 722 | |||
| 723 | public function NormAngle($a) |
||
| 741 | |||
| 742 | public function imagettfbbox_fixed($size, $angle, $fontfile, $text) |
||
| 826 | |||
| 827 | // Deprecated |
||
| 828 | public function GetTTFBBox($aTxt, $aAngle = 0) |
||
| 833 | |||
| 834 | public function GetBBoxTTF($aTxt, $aAngle = 0) |
||
| 888 | |||
| 889 | public function GetBBoxHeight($aTxt, $aAngle = 0) |
||
| 894 | |||
| 895 | public function GetBBoxWidth($aTxt, $aAngle = 0) |
||
| 900 | |||
| 901 | public function _StrokeTTF($x, $y, $txt, $dir, $paragraph_align, &$aBoundingBox, $debug = false) |
||
| 1091 | |||
| 1092 | public function StrokeText($x, $y, $txt, $dir = 0, $paragraph_align = "left", $debug = false) |
||
| 1113 | |||
| 1114 | public function SetMargin($lm, $rm, $tm, $bm) |
||
| 1131 | |||
| 1132 | public function SetTransparent($color) |
||
| 1136 | |||
| 1137 | public function SetColor($color, $aAlpha = 0) |
||
| 1148 | |||
| 1149 | public function PushColor($color) |
||
| 1160 | |||
| 1161 | public function PopColor() |
||
| 1169 | |||
| 1170 | public function SetLineWeight($weight) |
||
| 1177 | |||
| 1178 | public function SetStartPoint($x, $y) |
||
| 1183 | |||
| 1184 | public function Arc($cx, $cy, $w, $h, $s, $e) |
||
| 1197 | |||
| 1198 | public function FilledArc($xc, $yc, $w, $h, $s, $e, $style = '') |
||
| 1219 | |||
| 1220 | public function FilledCakeSlice($cx, $cy, $w, $h, $s, $e) |
||
| 1224 | |||
| 1225 | public function CakeSlice($xc, $yc, $w, $h, $s, $e, $fillcolor = "", $arccolor = "") |
||
| 1261 | |||
| 1262 | public function Ellipse($xc, $yc, $w, $h) |
||
| 1266 | |||
| 1267 | public function Circle($xc, $yc, $r) |
||
| 1273 | |||
| 1274 | public function FilledCircle($xc, $yc, $r) |
||
| 1279 | |||
| 1280 | // Linear Color InterPolation |
||
| 1281 | public function lip($f, $t, $p) |
||
| 1289 | |||
| 1290 | // Set line style dashed, dotted etc |
||
| 1291 | public function SetLineStyle($s) |
||
| 1316 | |||
| 1317 | // Same as Line but take the line_style into account |
||
| 1318 | public function StyleLine($x1, $y1, $x2, $y2, $aStyle = '', $from_grid_class = false) |
||
| 1367 | |||
| 1368 | public function DashedLine($x1, $y1, $x2, $y2, $dash_length = 1, $dash_space = 4) |
||
| 1397 | |||
| 1398 | public function DashedLineForGrid($x1, $y1, $x2, $y2, $dash_length = 1, $dash_space = 4) |
||
| 1432 | |||
| 1433 | public function Line($x1, $y1, $x2, $y2) |
||
| 1449 | |||
| 1450 | public function Polygon($p, $closed = false, $fast = false) |
||
| 1479 | |||
| 1480 | public function FilledPolygon($pts) |
||
| 1495 | |||
| 1496 | public function Rectangle($xl, $yu, $xr, $yl) |
||
| 1500 | |||
| 1501 | public function FilledRectangle($xl, $yu, $xr, $yl) |
||
| 1505 | |||
| 1506 | public function FilledRectangle2($xl, $yu, $xr, $yl, $color1, $color2, $style = 1) |
||
| 1538 | |||
| 1539 | public function ShadowRectangle($xl, $yu, $xr, $yl, $fcolor = false, $shadow_width = 4, $shadow_color = 'darkgray', $useAlpha = true) |
||
| 1574 | |||
| 1575 | public function FilledRoundedRectangle($xt, $yt, $xr, $yl, $r = 5) |
||
| 1605 | |||
| 1606 | public function RoundedRectangle($xt, $yt, $xr, $yl, $r = 5) |
||
| 1629 | |||
| 1630 | public function FilledBevel($x1, $y1, $x2, $y2, $depth = 2, $color1 = '[email protected]', $color2 = '[email protected]') |
||
| 1635 | |||
| 1636 | public function Bevel($x1, $y1, $x2, $y2, $depth = 2, $color1 = '[email protected]', $color2 = '[email protected]') |
||
| 1652 | |||
| 1653 | public function StyleLineTo($x, $y) |
||
| 1659 | |||
| 1660 | public function LineTo($x, $y) |
||
| 1666 | |||
| 1667 | public function Point($x, $y) |
||
| 1671 | |||
| 1672 | public function Fill($x, $y) |
||
| 1676 | |||
| 1677 | public function FillToBorder($x, $y, $aBordColor) |
||
| 1685 | |||
| 1686 | public function SetExpired($aFlg = true) |
||
| 1690 | |||
| 1691 | // Generate image header |
||
| 1692 | public function Headers() |
||
| 1721 | |||
| 1722 | // Adjust image quality for formats that allow this |
||
| 1723 | public function SetQuality($q) |
||
| 1727 | |||
| 1728 | // Stream image to browser or to file |
||
| 1729 | public function Stream($aFile = "") |
||
| 1750 | |||
| 1751 | // Do SuperSampling using $scale |
||
| 1752 | public function DoSupersampling() |
||
| 1763 | |||
| 1764 | // Clear resources used by image (this is normally not used since all resources are/should be |
||
| 1765 | // returned when the script terminates |
||
| 1766 | public function Destroy() |
||
| 1770 | |||
| 1771 | // Specify image format. Note depending on your installation |
||
| 1772 | // of PHP not all formats may be supported. |
||
| 1773 | public function SetImgFormat($aFormat, $aQuality = 75) |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Draw Line |
||
| 1821 | */ |
||
| 1822 | public function DrawLine($im, $x1, $y1, $x2, $y2, $weight, $color) |
||
| 1912 | |||
| 1913 | public function DrawImageSmoothArc($im, $xc, $yc, $w, $h, $s, $e, $color, $style = null) |
||
| 1920 | |||
| 1921 | public function CreateColorForImageSmoothArc($color) |
||
| 1930 | |||
| 1931 | public function imageSmoothCircle(&$img, $cx, $cy, $cr, $color) |
||
| 1987 | |||
| 1988 | View Code Duplication | public function __get($name) |
|
| 2004 | |||
| 2005 | public function __set($name, $value) |
||
| 2009 | } // CLASS |
||
| 2010 |
This check marks private properties in classes that are never used. Those properties can be removed.