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) {
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()
*/
*/
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.
*
$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()
*/
if ($this->imagick->getImageFormat() == 'GIF') {
$this->imagick = $this->imagick->coalesceImages();
-
+
do {
$this->imagick->drawImage($draw);
}
$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()
*/