Skip to content

Commit

Permalink
Merge branch '4.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Classes/Controller/DummyImageController.php
  • Loading branch information
mficzel committed Sep 27, 2019
2 parents bbc1325 + 204d7f9 commit f21b4a8
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 147 deletions.
67 changes: 38 additions & 29 deletions Classes/Controller/DummyImageController.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php
namespace Sitegeist\Kaleidoscope\Controller;
declare(strict_types=1);

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Mvc\Controller\ActionController;
namespace Sitegeist\Kaleidoscope\Controller;

use Imagine\Image\ImagineInterface;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Palette;
use Imagine\Image\Box;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\Point;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Http\Component\SetHeaderComponent;

class DummyImageController extends ActionController
Expand Down Expand Up @@ -43,8 +44,9 @@ class DummyImageController extends ActionController
* @param string $fg
* @param string $t
* @param string $f
* @return string
*/
public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', string $fg = '#fff', string $t = null, string $f = 'png')
public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', string $fg = '#fff', string $t = null, string $f = 'png'): string
{
// limit input arguments
if ($w > 9999) {
Expand All @@ -56,15 +58,15 @@ public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', st
}

// limit input arguments
if ($w < 10 ) {
if ($w < 10) {
$w = 10;
}

if ($h < 10) {
$h = 10;
}

if (is_null($t)) {
if ($t === null) {
$t = (string)$w . ' x ' . (string)$h;
}

Expand Down Expand Up @@ -108,7 +110,7 @@ public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', st
}

if ($renderPattern) {
$this->renderPattern($image, $renderShape ? $backgroundColor : $foregroundColor, $width, $height, $text);
$this->renderPattern($image, $renderShape ? $backgroundColor : $foregroundColor, $width, $height);
}

// render image
Expand Down Expand Up @@ -136,11 +138,11 @@ public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', st
* @param int $width
* @param int $height
*/
protected function renderBackground(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor, int $width, int $height): void
protected function renderBackground(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor, int $width, int $height): void
{
$image->draw()->polygon(
[
new Point(0,0),
new Point(0, 0),
new Point($width, 0),
new Point($width, $height),
new Point(0, $height)
Expand All @@ -149,16 +151,16 @@ protected function renderBackground(ImageInterface $image, ColorInterface $foreg
true,
1
);

}

/**
* @param ImageInterface $image
* @param ColorInterface $foregroundColor
* @param ColorInterface $backgroundColor
* @param int $width
* @param int $height
*/
protected function renderShape(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor, int $width, int $height): void
protected function renderShape(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor, int $width, int $height): void
{
$imageAspectRatio = $width / $height;
$baseShapeWidth = 600;
Expand All @@ -184,24 +186,24 @@ protected function renderShape(ImageInterface $image, ColorInterface $foreground

// transform shape to center of the image
$factor = ($imageAspectRatio > $baseShapeAspectRatio) ? (float)$height / (float)$baseShapeHeight : (float)$width / (float)$baseShapeWidth;
$xoffset = ($imageAspectRatio > $baseShapeAspectRatio) ? ($width - ($baseShapeWidth * $factor)) / 2.0 : 0.0;
$yoffset = ($imageAspectRatio < $baseShapeAspectRatio) ? ($height - ($baseShapeHeight * $factor)) / 2.0 : 0.0;
$xOffset = ($imageAspectRatio > $baseShapeAspectRatio) ? ($width - ($baseShapeWidth * $factor)) / 2.0 : 0.0;
$yOffset = ($imageAspectRatio < $baseShapeAspectRatio) ? ($height - ($baseShapeHeight * $factor)) / 2.0 : 0.0;

/**
* @var $transformedShape Point[]
*/
$transformedShape = array_map(
function (Point $point) use ($factor, $xoffset, $yoffset) {
return new Point($point->getX() * $factor + $xoffset, $point->getY() * $factor + $yoffset);
static function (Point $point) use ($factor, $xOffset, $yOffset) {
return new Point($point->getX() * $factor + $xOffset, $point->getY() * $factor + $yOffset);
},
$baseShape
);

// adjust some points based on aspect ratio
$transformedShape[0] = new Point(0, $transformedShape[0]->getY());
$transformedShape[1] = new Point(0, 0);
$transformedShape[2] = new Point( $width, 0);
$transformedShape[3] = new Point( $width, $transformedShape[3]->getY());
$transformedShape[2] = new Point($width, 0);
$transformedShape[3] = new Point($width, $transformedShape[3]->getY());

// draw shape
$image->draw()->polygon(
Expand All @@ -219,7 +221,7 @@ function (Point $point) use ($factor, $xoffset, $yoffset) {
* @param int $width
* @param int $height
*/
protected function renderBorder(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor,int $width, int $height): void
protected function renderBorder(ImageInterface $image, ColorInterface $foregroundColor, ColorInterface $backgroundColor, int $width, int $height): void
{
$borderWidth = 10;

Expand Down Expand Up @@ -248,8 +250,9 @@ protected function renderBorder(ImageInterface $image, ColorInterface $foregroun
* @param int $width
* @param int $height
* @param string $text
* @param bool $center
*/
protected function renderText(ImageInterface $image, ColorInterface $textColor, int $width, int $height, string $text, $center = false): void
protected function renderText(ImageInterface $image, ColorInterface $textColor, int $width, int $height, string $text, bool $center = false): void
{
$initialFontSize = 10;
if (file_exists('resource://Neos.Neos/Public/Fonts/NotoSans/NotoSans-Regular.ttf')) {
Expand All @@ -270,16 +273,23 @@ protected function renderText(ImageInterface $image, ColorInterface $textColor,
// render actual text
$actualFont = $this->imagineService->font($fontFile, min([$correctedFontSizeByWidth, $correctedFontSizeByHeight]), $textColor);
$actualFontBox = $actualFont->box($text);
$imageCenterPosition = new Point($width / 2 , $height / 2);
$imageCenterPosition = new Point($width / 2, $height / 2);
$textCenterPosition = new Point\Center($actualFontBox);
if ($center) {
$centeredTextPosition = new Point($imageCenterPosition->getX() - $textCenterPosition->getX(), ($height * .5 - $actualFontBox->getHeight() * .5));
$centeredTextPosition = new Point($imageCenterPosition->getX() - $textCenterPosition->getX(), $height * .5 - $actualFontBox->getHeight() * .5);
} else {
$centeredTextPosition = new Point($imageCenterPosition->getX() - $textCenterPosition->getX(), ($height * .78 - $actualFontBox->getHeight() * .5));
$centeredTextPosition = new Point($imageCenterPosition->getX() - $textCenterPosition->getX(), $height * .78 - $actualFontBox->getHeight() * .5);
}
$image->draw()->text($text, $actualFont, $centeredTextPosition);
}

/**
* @param ImageInterface $image
* @param ColorInterface $patternColor
* @param int $width
* @param int $height
* @return void
*/
protected function renderPattern(ImageInterface $image, ColorInterface $patternColor, int $width, int $height): void
{
$borderWidth = 5;
Expand All @@ -293,13 +303,12 @@ protected function renderPattern(ImageInterface $image, ColorInterface $patternC

for ($i = 0; $i < $patternSize; $i++) {
for ($k = 0; $k < $patternSize; $k++) {

if ($k > $patternSize - $i || $i > $patternSize - $k) {
continue;
}

if (
$i == $k ||
$i === $k ||
($i % 2 && $k % 2)

) {
Expand Down
72 changes: 36 additions & 36 deletions Classes/EelHelpers/AbstractImageSourceHelper.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
declare(strict_types=1);

namespace Sitegeist\Kaleidoscope\EelHelpers;

use Neos\Flow\Annotations as Flow;
use Neos\Utility\Arrays;

/**
* Class AbstractImageSourceHelper
*
* @package Sitegeist\Kaleidoscope\EelHelpers
*/
abstract class AbstractImageSourceHelper implements ImageSourceHelperInterface
{

/**
* @var int
*/
Expand All @@ -22,36 +24,36 @@ abstract class AbstractImageSourceHelper implements ImageSourceHelperInterface
protected $targetHeight;

/**
* @var string|null
* @var string
*/
protected $targetFormat = null;
protected $targetFormat;

/**
* @var mixed
* @var array
* @Flow\InjectConfiguration(path="thumbnailPresets", package="Neos.Media")
*/
protected $thumbnailPresets;

/**
* @param int|null $targetWidth
* @param bool $preserveAspectRatio
* @param bool $preserveAspect
* @return ImageSourceHelperInterface
*/
public function setWidth(int $targetWidth = null, bool $preserveAspect = false) : ImageSourceHelperInterface
public function setWidth(int $targetWidth = null, bool $preserveAspect = false): ImageSourceHelperInterface
{
$newSource = clone($this);
$newSource = clone $this;
$newSource->targetWidth = $targetWidth;
return $newSource;
}

/**
* @param int|null $targetHeight
* @param bool $preserverAspectRatio
* @param bool $preserveAspect
* @return ImageSourceHelperInterface
*/
public function setHeight(int $targetHeight = null, bool $preserveAspect = false) : ImageSourceHelperInterface
public function setHeight(int $targetHeight = null, bool $preserveAspect = false): ImageSourceHelperInterface
{
$newSource = clone($this);
$newSource = clone $this;
$newSource->targetHeight = $targetHeight;
return $newSource;
}
Expand All @@ -60,7 +62,7 @@ public function setHeight(int $targetHeight = null, bool $preserveAspect = false
* @param string|null $format
* @return ImageSourceHelperInterface
*/
public function setFormat(?string $format = null) : ImageSourceHelperInterface
public function setFormat(string $format = null): ImageSourceHelperInterface
{
$newSource = clone($this);
$newSource->targetFormat = $format;
Expand All @@ -72,9 +74,9 @@ public function setFormat(?string $format = null) : ImageSourceHelperInterface
* @param int|null $targetHeight
* @return ImageSourceHelperInterface
*/
public function setDimensions(int $targetWidth = null, int $targetHeight = null) : ImageSourceHelperInterface
public function setDimensions(int $targetWidth = null, int $targetHeight = null): ImageSourceHelperInterface
{
$newSource = clone($this);
$newSource = clone $this;
$newSource->targetWidth = $targetWidth;
$newSource->targetHeight = $targetHeight;
return $newSource;
Expand All @@ -86,22 +88,20 @@ public function setDimensions(int $targetWidth = null, int $targetHeight = null)
* @param string $name
* @return ImageSourceHelperInterface
*/
public function applyPreset(string $name) : ImageSourceHelperInterface
public function applyPreset(string $name): ImageSourceHelperInterface
{
$newSource = clone($this);
if ($this->thumbnailPresets) {
$preset = Arrays::getValueByPath($this->thumbnailPresets, $name);
if ($preset) {
if ($width = Arrays::getValueByPath($preset, 'width')) {
$newSource->setWidth($width);
} elseif ($width = Arrays::getValueByPath($preset, 'maximumWidth')) {
$newSource->setWidth($width);
}
if ($height = Arrays::getValueByPath($preset, 'height')) {
$newSource->setHeight($height);
} elseif ($height = Arrays::getValueByPath($preset, 'maximumHeight')) {
$newSource->setHeight($height);
}
$newSource = clone $this;
if ($this->thumbnailPresets && isset($this->thumbnailPresets[$name])) {
$preset = $this->thumbnailPresets[$name];
if ($width = $preset['width'] ?? null) {
$newSource->setWidth($width);
} elseif ($width = $preset['maximumWidth'] ?? null) {
$newSource->setWidth($width);
}
if ($height = $preset['height'] ?? null) {
$newSource->setHeight($height);
} elseif ($height = $preset['maximumHeight'] ?? null) {
$newSource->setHeight($height);
}
}
return $newSource;
Expand All @@ -126,24 +126,24 @@ public function srcset($mediaDescriptors): string

foreach ($descriptors as $descriptor) {
if (preg_match('/^(?<width>[0-9]+)w$/u', $descriptor, $matches)) {
$width = (int) $matches['width'];
$width = (int)$matches['width'];
$scaleFactor = $width / $this->getCurrentWidth();
$scaled = $this->scale($scaleFactor);
$srcsetArray[] = $scaled->src() . ' ' . $width . 'w';
} elseif (preg_match('/^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches)){
$factor = (float) $matches['factor'];
} elseif (preg_match('/^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches)) {
$factor = (float)$matches['factor'];
$scaled = $this->scale($factor);
$srcsetArray[] = $scaled->src() . ' ' . $factor . 'x';
}
}
return implode(', ', $srcsetArray);
} else {
return $this->src();
}

return $this->src();
}

/**
* Define wich methods are available in the eel context
* Define which methods are available in the Eel context
*
* @param string $methodName
* @return bool
Expand All @@ -152,9 +152,9 @@ public function allowsCallOfMethod($methodName)
{
if (in_array($methodName, ['setWidth', 'setHeight', 'setDimensions', 'setFormat', 'applyPreset', 'src', 'srcset'])) {
return true;
} else {
return false;
}

return false;
}

/**
Expand Down
Loading

0 comments on commit f21b4a8

Please sign in to comment.