Пример #1
0
 /**
  * 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;
     }
 }
Пример #2
0
 public static function pad(\Imagine\Gd\Image $img, \Imagine\Image\Box $size, $fcolor = [255, 255, 255], $ftransparency = 0)
 {
     $tsize = $img->getSize();
     $x = $y = 0;
     if ($size->getWidth() > $tsize->getWidth()) {
         $x = round(($size->getWidth() - $tsize->getWidth()) / 2);
     }
     if ($size->getHeight() > $tsize->getHeight()) {
         $y = round(($size->getHeight() - $tsize->getHeight()) / 2);
     }
     $pasteto = new \Imagine\Image\Point($x, $y);
     $imagine = new \Imagine\Gd\Imagine();
     $palette = new \Imagine\Image\Palette\RGB();
     $color = new \Imagine\Image\Palette\Color\RGB($palette, $fcolor, $ftransparency);
     $image = $imagine->create($size, $color);
     $image->paste($img, $pasteto);
     return $image;
 }
 /**
  * @param \pavlinter\display2\objects\Image $image
  * @param \Imagine\Gd\Image $originalImage
  * @return static
  * @throws InvalidConfigException
  */
 public function resize($image, $originalImage)
 {
     if (empty($image->width)) {
         throw new InvalidConfigException('The "width" property must be set for "' . $image::className() . '".');
     }
     if (empty($image->height)) {
         throw new InvalidConfigException('The "height" property must be set for "' . $image::className() . '".');
     }
     /* @var $size \Imagine\Image\Box */
     $size = $originalImage->getSize();
     $wDivider = $size->getWidth() >= $image->width ? $size->getWidth() / $image->width : 0;
     $hDivider = $size->getHeight() >= $image->height ? $size->getHeight() / $image->height : 0;
     $w = $image->width;
     // if image smaller
     $h = $image->height;
     // if image smaller
     if ($wDivider > $hDivider) {
         if ($size->getHeight() >= $image->height) {
             $w = $size->getWidth() / $hDivider;
             $h = $image->height;
         }
     } else {
         //$wDivider <= $hDivider
         if ($size->getWidth() >= $image->width) {
             $w = $image->width;
             $h = $size->getHeight() / $wDivider;
         }
     }
     $Box = new Box($w, $h);
     $newImage = $originalImage->thumbnail($Box);
     $boxNew = $newImage->getSize();
     $x = ($Box->getWidth() - $boxNew->getWidth()) / 2;
     $y = ($Box->getHeight() - $boxNew->getHeight()) / 2;
     $point = new \Imagine\Image\Point($x, $y);
     $palette = new \Imagine\Image\Palette\RGB();
     $color = $palette->color($image->bgColor, $image->bgAlpha);
     return \yii\imagine\Image::getImagine()->create($Box, $color)->paste($newImage, $point);
 }
Пример #4
0
 /**
  * Returns boolean indicating whether or not the image exceeds maximum dimensions
  *
  * @param Image $image
  * @return bool
  */
 private function isTooBig(Image $image)
 {
     $maxDimension = 2000;
     $width = $image->getSize()->getWidth();
     $height = $image->getSize()->getHeight();
     return $width > $maxDimension || $height > $maxDimension;
 }
 /**
  * this method crops the image
  * @param Array $crop crop config
  * @param \Imagine\Gd\Image $image
  */
 protected function createCrop($crop, $image)
 {
     if (!empty($crop['cropped_field'])) {
         $save_path = $this->getUploadPath($crop['cropped_field']);
     } else {
         $save_path = $this->getUploadPath($this->attribute);
     }
     $sizes = explode('-', $crop['value']);
     $real_size = $image->getSize();
     foreach ($sizes as $ind => $cr) {
         $sizes[$ind] = round($sizes[$ind] * ($ind % 2 == 0 ? $real_size->getWidth() : $real_size->getHeight()) / 100);
     }
     $crop_image = $image->crop(new Point($sizes[0], $sizes[1]), new Box($sizes[2] - $sizes[0], $sizes[3] - $sizes[1]));
     if (!empty($crop['crop_width'])) {
         $crop_image = $crop_image->resize(new Box($crop['crop_width'], $crop['crop_width'] / $crop['ratio']));
     }
     $crop_image->save($save_path, isset($crop['save_options']) ? $crop['save_options'] : $this->save_options);
 }
Пример #6
0
 /**
  * @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));
 }