From: Matthias Schmidt Date: Sun, 10 May 2015 08:04:59 +0000 (+0200) Subject: Add text-related APIs to image adapter X-Git-Tag: 2.1.4~23 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8c56908c6e26f9dfd98308cd71b671741c80379e;p=GitHub%2FWoltLab%2FWCF.git Add text-related APIs to image adapter --- diff --git a/wcfsetup/install/files/lib/system/image/adapter/GDImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/GDImageAdapter.class.php index f38a9652b7..1ab8ed2cb0 100644 --- a/wcfsetup/install/files/lib/system/image/adapter/GDImageAdapter.class.php +++ b/wcfsetup/install/files/lib/system/image/adapter/GDImageAdapter.class.php @@ -214,21 +214,21 @@ class GDImageAdapter implements IImageAdapter { case 'middleLeft': case 'bottomLeft': $x = $margin; - break; - + break; + case 'topCenter': case 'middleCenter': case 'bottomCenter': $x = floor(($this->getWidth() - $textWidth) / 2); - break; - + break; + case 'topRight': case 'middleRight': case 'bottomRight': $x = $this->getWidth() - $textWidth - $margin; - break; + break; } - + // calculate y coordinate $y = 0; switch ($position) { @@ -236,24 +236,43 @@ class GDImageAdapter implements IImageAdapter { case 'topCenter': case 'topRight': $y = $margin + $firstLineHeight; - break; - + break; + case 'middleLeft': case 'middleCenter': case 'middleRight': $y = floor(($this->getHeight() - $textHeight) / 2) + $firstLineHeight; - break; - + break; + case 'bottomLeft': case 'bottomCenter': case 'bottomRight': $y = $this->getHeight() - $textHeight + $firstLineHeight - $margin; - break; + break; } $this->drawText($text, $x + $offsetX, $y + $offsetY, $font, $size, $opacity); } + /** + * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage() + */ + public function textFitsImage($text, $margin, $font, $size) { + $box = imagettfbbox($size, 0, $font, $text); + + $textWidth = abs($box[0] - $box[2]); + $textHeight = abs($box[7] - $box[1]); + + return ($textWidth + 2 * $margin <= $this->getWidth() && $textHeight + 2 * $margin <= $this->getHeight()); + } + + /** + * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize() + */ + public function adjustFontSize($text, $margin, $font, $size) { + // does nothing + } + /** * @see \wcf\system\image\adapter\IImageAdapter::setColor() */ diff --git a/wcfsetup/install/files/lib/system/image/adapter/IImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/IImageAdapter.class.php index 4e459392f1..159d810536 100644 --- a/wcfsetup/install/files/lib/system/image/adapter/IImageAdapter.class.php +++ b/wcfsetup/install/files/lib/system/image/adapter/IImageAdapter.class.php @@ -110,6 +110,30 @@ interface IImageAdapter { */ public function drawTextRelative($text, $position, $margin, $offsetX, $offsetY, $font, $size, $opacity = 1); + /** + * Returns true if the given text fits the image. + * + * @param string $text + * @param integer $margin + * @param string $font path to TrueType font file + * @param integer $size font size + * @return integer + * @return boolean + */ + public function textFitsImage($text, $margin, $font, $size); + + /** + * Adjusts the given font size so that the given text fits on the current + * image. Returns 0 if no appropriate font size could be determined. + * + * @param string $text + * @param integer $margin + * @param string $font path to TrueType font file + * @param integer $size font size + * @return integer + */ + public function adjustFontSize($text, $margin, $font, $size); + /** * Sets active color. * diff --git a/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php index b4464e7c62..f9edb189ea 100644 --- a/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php +++ b/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php @@ -167,6 +167,25 @@ class ImageAdapter implements IImageAdapter { $this->adapter->drawTextRelative($text, $position, $margin, $offsetX, $offsetY, $font, $size, $opacity); } + /** + * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage() + */ + public function textFitsImage($text, $margin, $font, $size) { + return $this->adapter->textFitsImage($text, $margin, $font, $size); + } + + /** + * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize() + */ + public function adjustFontSize($text, $margin, $font, $size) { + // adjust font size + while ($size && !$this->textFitsImage($text, $margin, $font, $size)) { + $size--; + } + + return $size; + } + /** * @see \wcf\system\image\adapter\IImageAdapter::setColor() */ diff --git a/wcfsetup/install/files/lib/system/image/adapter/ImagickImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/ImagickImageAdapter.class.php index 35f66c449e..bcc50316ac 100644 --- a/wcfsetup/install/files/lib/system/image/adapter/ImagickImageAdapter.class.php +++ b/wcfsetup/install/files/lib/system/image/adapter/ImagickImageAdapter.class.php @@ -200,7 +200,7 @@ class ImagickImageAdapter implements IImageAdapter { if ($this->imagick->getImageFormat() == 'GIF') { $this->imagick = $this->imagick->coalesceImages(); - + do { $this->imagick->drawImage($draw); } @@ -268,6 +268,25 @@ class ImagickImageAdapter implements IImageAdapter { $this->drawText($text, $x + $offsetX, $y + $offsetY, $font, $size, $opacity); } + /** + * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage() + */ + public function textFitsImage($text, $margin, $font, $size) { + $draw = new \ImagickDraw(); + $draw->setFont($font); + $draw->setFontSize($size); + $metrics = $this->imagick->queryFontMetrics($draw, $text); + + return ($metrics['textWidth'] + 2 * $margin <= $this->getWidth() && $metrics['textHeight'] + 2 * $margin <= $this->getHeight()); + } + + /** + * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize() + */ + public function adjustFontSize($text, $margin, $font, $size) { + // does nothing + } + /** * @see \wcf\system\image\adapter\IImageAdapter::setColor() */