Skip to content

Commit

Permalink
Implemented image alias generator, using legacy kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed Oct 23, 2012
1 parent 9297314 commit c7c0aa0
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 33 deletions.
6 changes: 6 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/fieldtypes.yml
Expand Up @@ -396,6 +396,12 @@ services:
tags:
- {name: ezpublish.fieldType.externalStorageHandler, alias: ezimage}

# Image alias generator
# TODO: Implement a new one not to rely only on legacy
# See @EzPublishLegacyBundle/config/services.yml
#ezpublish.fieldType.ezimage.aliasGenerator:
# alias: ezpublish.fieldType.ezimage.aliasGenerator.default

ezpublish.fieldType.ezkeyword:
class: %ezpublish.fieldType.ezkeyword.class%
parent: ezpublish.fieldType
Expand Down
Expand Up @@ -3,6 +3,7 @@
{# Following variables are passed:
# - \eZ\Publish\API\Repository\Values\Content\Field field the field to display
# - \eZ\Publish\API\Repository\Values\Content\ContentInfo contentInfo the contentInfo to which the field belongs to
# - \eZ\Publish\API\Repository\Values\Content\VersionInfo versionInfo the versionInfo to which the field belongs to
# - mixed fieldSettings settings of the field (depends on the fieldtype)
# - array parameters options passed to ez_render_field under the parameters key
# - array attr the attributes to add the generate HTML, contains at least a "class" entry
Expand Down Expand Up @@ -334,12 +335,15 @@
{% endspaceless %}
{% endblock %}
{# TODO: Handle image variations #}
{# This field accepts the following parameters:
# - alias (image variation name). Defaults to "original" (e.g. image originally uploaded)
#}
{% block ezimage_field %}
{% spaceless %}
{% if field.value %}
<figure {{ block( 'field_attributes' ) }}>
<img src="{{ asset( field.value.path ) }}" alt="{{ field.value.alternativeText }}" />
{% set imageAlias = ez_image_alias( field, versionInfo, parameters.alias|default( 'original' ) ) %}
<img src="{{ asset( imageAlias.uri ) }}" width="{{ imageAlias.width }}" height="{{ imageAlias.height }}" alt="{{ field.value.alternativeText }}" />
</figure>
{% endif %}
{% endspaceless %}
Expand Down
11 changes: 10 additions & 1 deletion eZ/Bundle/EzPublishLegacyBundle/Resources/config/services.yml
Expand Up @@ -25,6 +25,7 @@ parameters:
ezpublish_legacy.config.resolver.class: eZ\Bundle\EzPublishLegacyBundle\DependencyInjection\Configuration\LegacyConfigResolver
ezpublish_legacy.config.default_scope: site
ezpublish_legacy.setup_wizard.configuration_converter.class: eZ\Bundle\EzPublishLegacyBundle\SetupWizard\ConfigurationConverter
ezpublish_legacy.fieldType.ezimage.variant_service.class: eZ\Publish\Core\MVC\Legacy\FieldType\Image\AliasGenerator

services:
ezpublish_legacy.kernel:
Expand Down Expand Up @@ -125,4 +126,12 @@ services:

ezpublish_legacy.setup_wizard.configuration_converter:
class: %ezpublish_legacy.setup_wizard.configuration_converter.class%
arguments: [@ezpublish_legacy.config.resolver, @ezpublish_legacy.kernel]
arguments: [@ezpublish_legacy.config.resolver, @ezpublish_legacy.kernel]

# Image alias generator using legacy
ezpublish_legacy.fieldType.ezimage.variant_service:
class: %ezpublish_legacy.fieldType.ezimage.variant_service.class%
arguments: [@ezpublish_legacy.kernel, @ezpublish.config.resolver]

ezpublish.fieldType.ezimage.variant_service:
alias: ezpublish_legacy.fieldType.ezimage.variant_service
18 changes: 18 additions & 0 deletions eZ/Publish/API/Repository/Exceptions/InvalidVariantException.php
@@ -0,0 +1,18 @@
<?php
/**
* File containing the InvalidVariantException class.
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/

namespace eZ\Publish\API\Repository\Exceptions;

class InvalidVariantException extends InvalidArgumentException
{
public function __construct( $variantName, $variantType, $code = 0, Exception $previous = null )
{
parent::__construct( "Invalid variant '$variantName' for $variantType", $code, $previous );
}
}
43 changes: 43 additions & 0 deletions eZ/Publish/API/Repository/Values/File/ImageVariant.php
@@ -0,0 +1,43 @@
<?php
/**
* File containing the ImageVariant class.
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/

namespace eZ\Publish\API\Repository\Values\File;

class ImageVariant extends Variant
{
/**
* The width as number of pixels (for example "320").
*
* @var int
*/
protected $width;

/**
* The height as number of pixels (for example "256").
*
* @var int
*/
protected $height;

/**
* The name of the image alias (for example "original").
*
* @var string
*/
protected $name;

/**
* Contains extra information about the image, depending on the image type.
* It will typically contain EXIF information from digital cameras or information about animated GIFs.
* If there is no information, the info will be a boolean FALSE.
*
* @var mixed
*/
protected $info;
}
60 changes: 60 additions & 0 deletions eZ/Publish/API/Repository/Values/File/Variant.php
@@ -0,0 +1,60 @@
<?php
/**
* File containing the file Variant class.
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/

namespace eZ\Publish\API\Repository\Values\File;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* Base class for file variations (i.e. image aliases)
*/
class Variant extends ValueObject
{
/**
* Number of bytes for current variant
*
* @var int
*/
protected $fileSize;

/**
* The MIME type (for example "image/png").
*
* @var string
*/
protected $mimeType;

/**
* The name of the file (for example "my_image.png").
*
* @var string
*/
protected $fileName;

/**
* The path to the file (for example "var/storage/images/test/199-2-eng-GB").
*
* @var string
*/
protected $dirPath;

/**
* Complete path + name of image file (for example "var/storage/images/test/199-2-eng-GB/apple.png").
*
* @var string
*/
protected $uri;

/**
* When the variant was last modified.
*
* @var \DateTime
*/
protected $lastModified;
}
119 changes: 119 additions & 0 deletions eZ/Publish/Core/MVC/Legacy/FieldType/Image/AliasGenerator.php
@@ -0,0 +1,119 @@
<?php
/**
* File containing the image AliasGenerator class.
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/

namespace eZ\Publish\Core\MVC\Legacy\FieldType\Image;

use eZ\Publish\Core\MVC\ConfigResolverInterface,
eZ\Publish\SPI\VariantService,
eZ\Publish\API\Repository\Values\Content\Field,
eZ\Publish\API\Repository\Values\Content\VersionInfo,
eZ\Publish\API\Repository\Values\File\ImageVariant,
eZ\Publish\API\Repository\Exceptions\InvalidVariantException,
eZContentObjectAttribute,
eZImageAliasHandler;

class AliasGenerator implements VariantService
{
/**
* @var \Closure
*/
private $kernelClosure;

/**
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface
*/
private $configResolver;

/**
* @var \eZImageAliasHandler[]
*/
private $aliasHandlers;

/**
* Image variant objects, indexed by <fieldId>-<versionNo>-<variantName>.
* Storing them avoids to run the legacy kernel each time if there are similar images variations required.
*
* @var \eZ\Publish\API\Repository\Values\File\ImageVariant[]
*/
private $variants;

public function __construct( \Closure $legacyKernelClosure, ConfigResolverInterface $configResolver )
{
$this->kernelClosure = $legacyKernelClosure;
$this->configResolver = $configResolver;
}

/**
* @return \eZ\Publish\Core\MVC\Legacy\Kernel
*/
protected function getLegacyKernel()
{
$kernelClosure = $this->kernelClosure;
return $kernelClosure();
}

/**
* Returns an image variant object.
* Variant creation will be done through the legacy eZImageAliasHandler, using the legacy kernel.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $field
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
* @param string $variantName
* @return \eZ\Publish\API\Repository\Values\File\ImageVariant
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidVariantException
*/
public function getVariant( Field $field, VersionInfo $versionInfo, $variantName )
{
$variantIdentifier = "$field->id-$versionInfo->versionNo-$variantName";
if ( isset( $this->variants[$variantIdentifier] ) )
return $this->variants[$variantIdentifier];

$configResolver = $this->configResolver;
// Assigning by reference to be able to modify those arrays within the closure (due to PHP 5.3 limitation with access to $this)
$allAliasHandlers = &$this->aliasHandlers;
$allVariants = &$this->variants;

return $this->getLegacyKernel()->runCallback(
function () use ( $field, $versionInfo, $variantName, $configResolver, $allAliasHandlers, $allVariants, $variantIdentifier )
{
$aliasHandlerIdentifier = "$field->id-$versionInfo->versionNo";
if ( !isset( $allAliasHandlers[$aliasHandlerIdentifier] ) )
{
$allAliasHandlers[$aliasHandlerIdentifier] = new eZImageAliasHandler(
eZContentObjectAttribute::fetch( $field->id, $versionInfo->versionNo )
);
}

/** @var $imageAliasHandler \eZImageAliasHandler */
$imageAliasHandler = $allAliasHandlers[$aliasHandlerIdentifier];
$aliasArray = $imageAliasHandler->imageAlias( $variantName );
if ( $aliasArray === null )
throw new InvalidVariantException( $variantName, 'image' );

$allVariants[$variantIdentifier] = new ImageVariant(
array(
'name' => $variantName,
'fileName' => $aliasArray['filename'],
'dirPath' => $aliasArray['dirpath'],
'fileSize' => $aliasArray['filesize'],
'mimeType' => $aliasArray['mime_type'],
'lastModified' => new \DateTime( '@' . $aliasArray['timestamp'] ),
'uri' => $aliasArray['url'],
'width' => $aliasArray['width'],
'height' => $aliasArray['height'],
)
);

return $allVariants[$variantIdentifier];
},
false
);
}
}

0 comments on commit c7c0aa0

Please sign in to comment.