/** * Get a resized thumbnail from the given image * @param Image image source image for the thumbnail * @param int width width to calculate the thumbnail's width * @param int height height to calculate the thumbnail's height * @return Image Image instance of the thumbnail */ protected function createThumbnail(Image $image, $width, $height) { $imageWidth = $image->getWidth(); $imageHeight = $image->getHeight(); if ($imageWidth > $imageHeight) { $this->calculateNewSize($imageWidth, $imageHeight, $width, $height); } else { $this->calculateNewSize($imageHeight, $imageWidth, $height, $width); } return $image->resize($width, $height); }
/** * Get a thumbnail from the given image * @param Image image source image for the thumbnail * @param int width width to calculate the thumbnail's width * @param int height height to calculate the thumbnail's height * @return Image Image instance of the thumbnail */ public function getThumbnail(Image $image, $thumbnailWidth, $thumbnailHeight) { if (Number::isNegative($thumbnailWidth)) { throw new ThumbnailException($thumbnailWidth . ' is an invalid width'); } if (Number::isNegative($thumbnailHeight)) { throw new ThumbnailException($thumbnailHeight . ' is an invalid height'); } if ($image->getWidth() <= $thumbnailWidth && $image->getHeight() <= $thumbnailHeight) { return $image; } return $this->createThumbnail($image, $thumbnailWidth, $thumbnailHeight); }
/** * Draws the layers content on the image * @param zibo\library\image\Image $image The image to draw upon * @param zibo\library\diagram\Diagram $diagram The diagram we're drawing * @return null */ public function draw(Image $image, Diagram $diagram) { $grid = $diagram->getGrid(); $width = $image->getWidth(); $height = $image->getHeight(); $cellDimension = $grid->getCellDimension(); $margin = $diagram->getMargin(); $cellWidth = $cellDimension->getWidth(); $cellHeight = $cellDimension->getHeight(); // take margin into account and go out of the image to start the grid $loopX = $loopY = $this->helperLocation - 1; $x = $margin; while ($x > 0) { $x -= $cellWidth; $loopX--; } $y = $margin; while ($y > 0) { $y -= $cellHeight; $loopY--; } // draw the X-axis for (; $x < $width; $x += $cellWidth) { $loopX++; if ($loopX == $this->helperLocation) { $color = $this->helperColor; $loopX = 0; } else { $color = $this->gridColor; } $image->drawLine(new Point($x, 0), new Point($x, $height), $color); } // draw the Y-axis for (; $y < $height; $y += $cellHeight) { $loopY++; if ($loopY == $this->helperLocation) { $color = $this->helperColor; $loopY = 0; } else { $color = $this->gridColor; } $image->drawLine(new Point(0, $y), new Point($width, $y), $color); } }
/** * Draws the layers content on the image * @param zibo\library\image\Image $image The image to draw upon * @param zibo\library\diagram\Diagram $diagram The diagram we're drawing * @return null */ public function draw(Image $image, Diagram $diagram) { $grid = $diagram->getGrid(); $margin = $diagram->getMargin(); $cellDimension = $grid->getCellDimension(); $cellWidth = $cellDimension->getWidth(); $cellHeight = $cellDimension->getHeight(); $gridDimension = $grid->getGridDimension(); $gridWidth = $gridDimension->getWidth(); $gridHeight = $gridDimension->getHeight(); for ($x = 0; $x <= $gridWidth; $x++) { for ($y = 0; $y <= $gridHeight; $y++) { if ($grid->isFree(new Point($x, $y))) { continue; } $pointX = $x * $cellWidth + $margin; $pointY = $y * $cellHeight + $margin; $point = new Point($pointX, $pointY); $image->fillRectangle($point, $cellDimension, $this->color); } } }
/** * Get a cropped thumbnail from the given image * @param Image image source image for the thumbnail * @param int width width to calculate the thumbnail's width * @param int height height to calculate the thumbnail's height * @return Image Image instance of the thumbnail */ protected function createThumbnail(Image $image, $thumbnailWidth, $thumbnailHeight) { $x = 0; $y = 0; $width = $thumbnailWidth; $height = $thumbnailHeight; if ($image->getWidth() > $image->getHeight()) { $this->calculateNewSize($image->getWidth(), $image->getHeight(), $width, $height, $x, $y); } else { $this->calculateNewSize($image->getHeight(), $image->getWidth(), $height, $width, $y, $x); } $thumbnail = $image->resize($width, $height); $thumbnail = $thumbnail->crop($x, $y, $thumbnailWidth, $thumbnailHeight); return $thumbnail; }
/** * Draw the connection on the provided image * @param zibo\library\image\Image $image * @return null */ public function draw(Image $image) { $previousPoint = null; foreach ($this->points as $point) { if (!$previousPoint) { $previousPoint = $point; continue; } $image->drawLine($previousPoint, $point, $this->color); $previousPoint = $point; } }
public function testResize() { $image = new Image($this->testImage); $resizeWidth = $this->testImageWidth / 2; $resizeHeight = $this->testImageHeight / 2; $resizedImage = $image->resize($resizeWidth, $resizeHeight); $this->assertNotNull($resizedImage, 'resizedimage is null'); $resuzedImageWidth = $resizedImage->getWidth(); $this->assertEquals($resizeWidth, $resuzedImageWidth, 'width is not the requested width'); $resizedImageHeight = $resizedImage->getHeight(); $this->assertEquals($resizeHeight, $resizedImageHeight, 'height is not the requested height'); }
/** * Process the source, apply thumbnailer if set * @param string $source source to process * @return string source to be used by the html of this image tag * @throws zibo\ZiboException when the source file could not be found */ private function processSource($source) { $fileSource = new File($source); if (!$fileSource->isAbsolute() && !String::startsWith($fileSource->getPath(), Zibo::DIRECTORY_APPLICATION . File::DIRECTORY_SEPARATOR)) { $fileSource = Zibo::getInstance()->getFile($fileSource->getPath()); if (!$fileSource) { throw new ZiboException('Could not find ' . $source); } } $fileDestination = $this->getCacheFile($fileSource); if (!$fileDestination->exists() || $fileSource->getModificationTime() > $fileDestination->getModificationTime()) { $image = new CoreImage($fileSource); if ($this->thumbnailer) { $thumbnail = $image->thumbnail($this->thumbnailer, $this->thumbnailWidth, $this->thumbnailHeight); if ($image === $thumbnail) { $fileSource->copy($fileDestination); } else { $thumbnail->write($fileDestination); } } else { $fileSource->copy($fileDestination); } } // remove application/ from the path return substr($fileDestination->getPath(), 12); }
/** * Checks whether a value is a valid image * @param string $value Path to the image * @return boolean true if the value is a valid image, false otherwise */ public function isValid($value) { $this->resetErrors(); if (!$this->isRequired && empty($value)) { return true; } $parameters = array('value' => $value); try { $file = new File($value); $image = new Image($file); } catch (Exception $exception) { $parameters['message'] = $exception->getMessage(); $error = new ValidationError(self::CODE, self::MESSAGE, $parameters); $this->addError($error); return false; } if ($this->minWidth && $image->getWidth() < $this->minWidth) { $parameters['width'] = $this->minWidth; $error = new ValidationError(self::CODE_WIDTH_MIN, self::MESSAGE_WIDTH_MIN, $parameters); $this->addError($error); } if ($this->minHeight && $image->getHeight() < $this->minHeight) { $parameters['height'] = $this->minHeight; $error = new ValidationError(self::CODE_HEIGHT_MIN, self::MESSAGE_HEIGHT_MIN, $parameters); $this->addError($error); } if ($this->maxWidth && $image->getWidth() > $this->maxWidth) { $parameters['width'] = $this->maxWidth; $error = new ValidationError(self::CODE_WIDTH_MAX, self::MESSAGE_WIDTH_MAX, $parameters); $this->addError($error); } if ($this->maxHeight && $image->getHeight() > $this->maxHeight) { $parameters['height'] = $this->maxHeight; $error = new ValidationError(self::CODE_HEIGHT_MAX, self::MESSAGE_HEIGHT_MAX, $parameters); $this->addError($error); } if ($this->errors) { return false; } return true; }
/** * Draws the object on the provided image * @param zibo\library\image\Image $image The image to draw upon * @param zibo\library\image\Point $point The top left corner to start drawing * @return null */ public function draw(Image $image, Point $point) { $x = $point->getX(); $y = $point->getY(); $width = $this->dimension->getWidth(); $height = $this->dimension->getHeight(); $textX = $x + $this->paddingLeft; // draw the box $image->fillRoundedRectangle($point, $this->dimension, 3, $this->backgroundColor); $image->drawRoundedRectangle($point, $this->dimension, 3, $this->frontColor); // draw the title bar $title = $this->meta->getName(); // . ($this->isLinkModel ? ' (L)' : ''); $image->drawText(new Point($textX, $point->getY() + $this->paddingTop), $this->frontColor, $title); $lineY = $y + 2 * $this->paddingTop + self::CHARACTER_HEIGHT + $this->paddingBottom; $image->drawLine(new Point($x, $lineY), new Point($x + $width, $lineY), $this->frontColor); // draw the fields $fieldY = $lineY + $this->paddingTop; $fields = $this->meta->getFields(); foreach ($fields as $fieldName => $field) { $label = '+ ' . $field->getName() . ': ' . $this->getFieldType($field); $this->fieldPoints[$fieldName] = new Point($x, ceil($fieldY + $this->paddingTop + 1)); $image->drawText(new Point($textX, $fieldY), $this->frontColor, $label); $fieldY += $this->paddingTop + self::CHARACTER_HEIGHT; } }
/** * Creates the image needed to draw the layers out of the grid * @return zibo\library\image\Image */ private function createImage() { $gridDimension = $this->grid->getGridDimension(); $cellDimension = $this->grid->getCellDimension(); $margin = 2 * $this->margin; $width = $gridDimension->getWidth() * $cellDimension->getWidth() + $margin; $height = $gridDimension->getHeight() * $cellDimension->getHeight() + $margin; $image = new Image(null, $width, $height); $image->fillRectangle(new Point(0, 0), new Dimension($width, $height), $this->backgroundColor); return $image; }