Skip to content

Commit

Permalink
FEATURE: Remove Image Id from Preview Images
Browse files Browse the repository at this point in the history
  • Loading branch information
cvette committed May 13, 2018
1 parent ab32bee commit 1cda89f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?php
namespace Vette\Shutterstock\Domain\Model\AssetSource\Shutterstock;

use Imagine\Image\Box;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Neos\Flow\Http\Uri;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\HasRemoteOriginalInterface;
use Neos\Media\Domain\Model\AssetSource\AssetSourceInterface;
use Neos\Media\Domain\Model\ImportedAsset;
use Neos\Media\Domain\Repository\ImportedAssetRepository;
use Psr\Http\Message\UriInterface;
use Neos\Flow\Annotations as Flow;

class ShutterstockAssetProxy implements AssetProxyInterface, HasRemoteOriginalInterface
{
/**
* @var ImagineInterface
* @Flow\Inject(lazy = false)
*/
protected $imagineService;

/**
* @var ShutterstockAssetSource
Expand Down Expand Up @@ -98,9 +107,36 @@ public function getPreviewUri(): ?UriInterface

public function getImportStream()
{
if ($this->assetSource->isRemoveImageIdFromPreview()) {
return $this->removeImageId();
}

return fopen($this->shutterstockData['assets']['preview']['url'], 'r');
}

/**
* Crops the image preview so the image Id is not shown
*
* @return bool|resource
*/
protected function removeImageId()
{
$fileHandle = fopen($this->shutterstockData['assets']['preview']['url'], 'r');
$image = $this->imagineService->read($fileHandle);

$width = $this->shutterstockData['assets']['preview']['width'];
$height = $this->shutterstockData['assets']['preview']['height'];

$image->crop(new Point(0,0), new Box($width, $height));
$string = $image->get('jpg');

$stream = fopen('php://memory','r+');
fwrite($stream, $string);
rewind($stream);

return $stream;
}

public function getLocalAssetIdentifier(): ?string
{
return $this->importedAsset instanceof ImportedAsset ? $this->importedAsset->getLocalAssetIdentifier() : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class ShutterstockAssetSource implements AssetSourceInterface
*/
protected $shutterstockClient;

/**
* @var bool
*/
protected $removeImageIdFromPreview = true;


/**
* ShutterstockAssetSource constructor.
Expand Down Expand Up @@ -62,6 +67,10 @@ public function initializeObject()
$this->shutterstockClient->setQueryParams($this->assetSourceOptions['queryParams']);
}

if (isset($this->assetSourceOptions['removeImageIdFromPreview'])) {
$this->removeImageIdFromPreview = $this->assetSourceOptions['removeImageIdFromPreview'];
}

$this->shutterstockClient->setClientKey($this->assetSourceOptions['clientKey']);
$this->shutterstockClient->setClientSecret($this->assetSourceOptions['clientSecret']);
}
Expand Down Expand Up @@ -127,4 +136,20 @@ public function isReadOnly(): bool
{
return true;
}

/**
* @return bool
*/
public function isRemoveImageIdFromPreview(): bool
{
return $this->removeImageIdFromPreview;
}

/**
* @param bool $removeImageDetailsFromPreview
*/
public function setRemoveImageIdFromPreview(bool $removeImageIdFromPreview): void
{
$this->removeImageIdFromPreview = $removeImageIdFromPreview;
}
}

0 comments on commit 1cda89f

Please sign in to comment.