/** * Adds the text to the image. */ public function renderText() { $palette = new RGB(); $color = $palette->color('#005', 100); $font = new Font(dirname(__FILE__) . '/../assets/trebuchet_bi.ttf', 24, $color); $blocks = explode('__', $this->text); $wordWidths = array(); foreach ($blocks as $key => $block) { $blocks[$key] = preg_split('#[^a-z0-9,:\'\\.-]#i', trim($block)); foreach ($blocks[$key] as $word) { $wordWidths[] = $font->box($word)->getWidth(); } } $lineHeight = 45; $maxRatio = 0; $result = array(); $space = $font->box('-')->getWidth(); $stepWidth = 25; for ($maxWidth = floor(max($wordWidths) / $stepWidth) * $stepWidth; $maxWidth <= 375; $maxWidth += $stepWidth) { $wordIndex = 0; $lines = array(); $maxLineWidth = 0; foreach ($blocks as $block) { $lines[] = ''; $lineWidth = 0; foreach ($block as $word) { if ($lineWidth + $wordWidths[$wordIndex] > $maxWidth && $lines[count($lines) - 1] != '') { $lines[] = ''; $lineWidth = 0; } $lines[count($lines) - 1] .= $word . ' '; $lineWidth += $wordWidths[$wordIndex] + $space; if ($lineWidth - $space > $maxLineWidth) { $maxLineWidth = $lineWidth - $space; } ++$wordIndex; } } $min = min(count($lines) * $lineHeight, $maxLineWidth); $max = max(count($lines) * $lineHeight, $maxLineWidth); $ratio = $min / $max; if ($ratio > $maxRatio) { $result = $lines; $maxRatio = $ratio; } } $y = $this->image->getSize()->getHeight() / 2 - $lineHeight * (count($result) / 2) + 4; foreach ($result as $line) { $box = $font->box(trim($line)); $this->image->draw()->text(trim($line), $font, new Point($this->image->getSize()->getWidth() / 2 - $box->getWidth() / 2, $y)); $y += $lineHeight; } }
/** * @param Image $area * @param array $items * @param int $rows */ protected function imagesGrid(Image $area, array $items, $rows) { if (!count($items) || !isset($items[0]) || !isset($items[0]['image'])) { return; } // border size acts as padding // item dimensions area effective (image size + border) $itemHeight = min(42, (int) (($area->getSize()->getHeight() - $this->gridItemMargin * ($rows + 1)) / $rows)); $testImageSize = $this->imagine->open($items[0]['image'])->getSize(); $imageAspect = $testImageSize->getWidth() / $testImageSize->getHeight(); $imageHeight = $itemHeight - $this->gridItemBorderSize * 2; $imageWidth = (int) ($imageHeight * $imageAspect); $itemWidth = $imageWidth + $this->gridItemBorderSize * 2; $imageSize = new Box($imageWidth, $imageHeight); $trimWidth = $itemWidth; $trimHeight = $itemHeight; $offsetX = 0; $offsetY = 0; foreach ($items as $item) { $item = is_array($item) ? (object) $item : $item; // break row if ($offsetX + $itemWidth > $area->getSize()->getWidth()) { $offsetX = 0; $offsetY += $itemHeight + $this->gridItemMargin; } if ($offsetY + $itemHeight > $area->getSize()->getHeight()) { break; } $trimHeight = max($trimHeight, $offsetY + $itemHeight); $trimWidth = max($trimWidth, $offsetX + $itemWidth); if ($trimWidth > $area->getSize()->getWidth()) { continue; } if (!empty($this->gridItemBorderSize)) { $borderColor = isset($item->color) ? $item->color : $this->gridItemBorderColor; // remove one pixel on all sides - polygon coordinates are inclusive! $area->draw()->polygon([new Point($offsetX, $offsetY), new Point($offsetX + $itemWidth - 1, $offsetY), new Point($offsetX + $itemWidth - 1, $offsetY + $itemHeight - 1), new Point($offsetX, $offsetY + $itemHeight - 1)], $this->color($borderColor, $this->gridItemBorderAlpha), true, 1); } $this->addImage($area, $item->image, $offsetX + $this->gridItemBorderSize, $offsetY + $this->gridItemBorderSize, $imageSize); $offsetX += $itemWidth + $this->gridItemMargin; } $area->crop(new Point(0, 0), new Box(min($area->getSize()->getWidth(), $trimWidth), $trimHeight)); }