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; }
public function __invoke($daemon) { error_log('checking gopro files'); $imagine = $this->imagine; $new = []; foreach ($this->gopro->getFiles(GoPro::FILTER_PHOTO) as $file) { if (in_array($file->getSequence(), $this->list)) { continue; } $new[] = $file; } error_log('found files: ' . count($new)); foreach ($new as $file) { error_log('downloading photo: ' . $file->getSequence()); $content = $this->gopro->download($file); error_log('opening photo: ' . $file->getSequence()); $photo = $imagine->load($content); error_log('resizing watermark'); $watermark = $this->watermark->copy(); $photoWidth = $photo->getSize(); $waterSize = $watermark->getSize(); $newSize = $waterSize->widen($photoWidth->getWidth() - 1); $watermark->resize($newSize); error_log('adding watermark'); $pos = new Point(0, $photo->getSize()->getHeight() - $watermark->getSize()->getHeight()); $photo->paste($watermark, $pos); error_log('uploading photo'); $this->darkroom->upload($file, $photo->get('jpg')); error_log('resizing photo'); $photo->resize(new Box($photo->getSize()->getWidth() / 4, $photo->getSize()->getHeight() / 4)); error_log('uploading thumb'); $this->darkroom->upload($file, $photo->get('jpg'), 'th'); unset($photo); unset($watermark); $this->list[] = $file->getSequence(); } if (!count($new)) { error_log('no new files, waiting a bit'); sleep(60); } return true; }
/** * 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 \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); }
/** * 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; }
/** * @param Image $image * @param int $format */ public function __construct(Image $image, $format) { parent::__construct($image->get($format), 200, ['Content-type' => 'image/' . $format]); }
/** * @param $file * @param $size */ private function addOverlay($file, $size) { list($width) = getimagesize($file); $size = $size < 1 ? 1 : $size; $originalLevelWidth = $width / $size; $overlayImagePath = $this->overlayPath . DIRECTORY_SEPARATOR . $originalLevelWidth . '.png'; if (file_exists($overlayImagePath)) { $destination = imagecreatefrompng($file); $src = imagecreatefrompng($overlayImagePath); $overlayImage = new Image($src); $overlayImage->resize(new Box($width, $width)); $tmpFilePath = $this->kernelcachedir . DIRECTORY_SEPARATOR . sha1(time() . rand()) . '.png'; $overlayImage->save($tmpFilePath); $src = imagecreatefrompng($tmpFilePath); $this->imagecopymerge_alpha($destination, $src, 0, 0, 0, 0, $width, $width, 100); imagepng($destination, $file); imagedestroy($destination); imagedestroy($src); unlink($tmpFilePath); } }
public function saveAs($type, $text, $file) { $fontfile = __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'Lato-Regular.ttf'; @unlink($file); switch ($type) { case $type == 99: include_once __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "phpqrcode" . DIRECTORY_SEPARATOR . "qrlib.php"; \QRcode::png($text, $file, QR_ECLEVEL_L, 12); break; case $type == 90: $font = new \Imagine\Gd\Font($fontfile, 35, new \Imagine\Image\Color('fff', 100)); $resource = imagecreatetruecolor(2000, 60); $color = new \Imagine\Image\Color('fff'); $white = imagecolorallocate($resource, 255, 255, 255); $black = imagecolorallocate($resource, 0, 0, 0); if (false === $white) { throw new RuntimeException('Unable to allocate color'); } if (false === imagefill($resource, 0, 0, $white)) { throw new RuntimeException('Could not set background color fill'); } imagettftext($resource, 35, 0, 10, 50, $black, $fontfile, $text); $image = new Image($resource); $image->crop(new \Imagine\Image\Point(0, 0), new \Imagine\Image\Box(20 + $font->box($text)->getWidth(), 60)); $image->save($file); break; case is_numeric($type): $type = $this->types[$type]; default: $validator = new BarcodeValidator(array('adapter' => $type, 'usechecksum' => false)); // if (!$validator->isValid($text)) { // $message = implode("\n", $validator->getMessages()); // throw new \Symfony\Component\HttpKernel\Exception\HttpException(401, $message, null); // } //z apki dostaje barcody z // if($type == 'ean13') // { // $text = substr($text, 0, -1); // } $barcodeOptions = array('text' => $text, 'factor' => 3, 'font' => __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'Lato-Regular.ttf'); $rendererOptions = array(); $imageRenderer = Barcode::factory($type, 'image', $barcodeOptions, $rendererOptions, false); //fix to not throw error when try to render barcode with code with checksum if ($imageRenderer->getBarcode()->getWithChecksum()) { //maybe i got barcode without checksum need to test it by try cache :( try { $imageRenderer->getBarcode()->validateText($text); } catch (\Exception $exc) { //propably length error remove checksum //when barcode have mandatoryChecksum and have default //validateSpecificText then renderer waiting for code without checksum :( $imageRenderer->getBarcode()->setText(substr($text, 0, -1)); } } //catch error and send http error 400 not 500 as default try { $image = new Image($imageRenderer->draw()); } catch (\Exception $exc) { $message = $exc->getMessage(); throw new \Symfony\Component\HttpKernel\Exception\HttpException(400, $message, null); } $image->save($file); } return true; }
/** * 缩略图 * @param Image $image * @return ImageInterface */ protected function thumbnail(Image $image) { return $image->thumbnail($this->thumbBox, $this->thumbMode); }
/** * 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); }
/** * @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)); }