Advertisement
Guest User

extend Image model

a guest
May 2nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.91 KB | None | 0 0
  1. <?php
  2. namespace WL\Test\Domain\Model;
  3.  
  4. /*                                                                        *
  5.  * This script belongs to the FLOW3 package "WL.Test".                  *
  6.  *                                                                        *
  7.  *                                                                        */
  8.  
  9. use TYPO3\Flow\Annotations as Flow;
  10. use Doctrine\ORM\Mapping as ORM;
  11.  
  12.  
  13. /**
  14.  * A Files
  15.  *
  16.  
  17.  
  18.  * @Flow\Entity
  19.  * @ORM\Table(name="files")
  20.  * @Flow\Scope("prototype")  
  21.  */
  22. class Files extends \TYPO3\Media\Domain\Model\Image {
  23.  
  24.     /**
  25.      * downloadable
  26.      * @var integer
  27.      * @ORM\Column(nullable=TRUE, options={"default" = 0})
  28.      */
  29.     protected $download;
  30.  
  31.     /**
  32.      * gid
  33.      * @var integer
  34.      * @ORM\Column(nullable=TRUE, options={"default" = 0})
  35.      */
  36.     protected $gemeindeid;
  37.  
  38.     /**
  39.      * @param integer $gemeindeid
  40.      * @return integer
  41.      */
  42.     public function setGemeindeid($gemeindeid) {
  43.         $this->gemeindeid = $gemeindeid;
  44.     }
  45.  
  46.     /**
  47.      * @return integer
  48.      */
  49.     public function getGemeindeid() {
  50.         return $this->gemeindeid;
  51.     }
  52.  
  53.     /**
  54.      * @param integer $download
  55.      * @return void
  56.      */
  57.     public function setDownload($download) {
  58.         $this->download = $download;
  59.     }
  60.  
  61.     /**
  62.      * @return integer
  63.      */
  64.     public function getDownload() {
  65.         return $this->download;
  66.     }
  67. }
  68. ?>
  69.  
  70. =======================================================
  71. =======================================================
  72.  
  73.  
  74. <?php
  75. namespace TYPO3\Media\Domain\Model;
  76.  
  77. /*                                                                        *
  78.  * This script belongs to the TYPO3 Flow package "TYPO3.Media".           *
  79.  *                                                                        *
  80.  * It is free software; you can redistribute it and/or modify it under    *
  81.  * the terms of the GNU General Public License, either version 3 of the   *
  82.  * License, or (at your option) any later version.                        *
  83.  *                                                                        *
  84.  * The TYPO3 project - inspiring people to share!                         *
  85.  *                                                                        */
  86.  
  87. use Doctrine\ORM\Mapping as ORM;
  88. use TYPO3\Flow\Annotations as Flow;
  89.  
  90. /**
  91.  * An image
  92.  *
  93.  * TODO: Remove duplicate code in Image and ImageVariant, by introducing a common base class or through Mixins/Traits (once they are available)
  94.  * @Flow\Entity
  95.  */
  96. class Image implements \TYPO3\Media\Domain\Model\ImageInterface {
  97.  
  98.     /**
  99.      * @var string
  100.      * @Flow\Validate(type="StringLength", options={ "maximum"=255 })
  101.      */
  102.     protected $title = '';
  103.  
  104.     /**
  105.      * @var \TYPO3\Flow\Resource\Resource
  106.      * @ORM\ManyToOne
  107.      * @Flow\Validate(type="NotEmpty")
  108.      */
  109.     protected $resource;
  110.  
  111.     /**
  112.      * @var integer
  113.      * @Flow\Validate(type="NotEmpty")
  114.      */
  115.     protected $width;
  116.  
  117.     /**
  118.      * @var integer
  119.      * @Flow\Validate(type="NotEmpty")
  120.      */
  121.     protected $height;
  122.  
  123.     /**
  124.      * one of PHPs IMAGETYPE_* constants
  125.      *
  126.      * @var integer
  127.      * @Flow\Validate(type="NotEmpty")
  128.      */
  129.     protected $type;
  130.  
  131.     /**
  132.      * @fixme this should be a collection, but that is currently not serialized by Doctrine
  133.      * @var array
  134.      */
  135.     protected $imageVariants = array();
  136.  
  137.     /**
  138.      * @param \TYPO3\Flow\Resource\Resource $resource
  139.      */
  140.     public function __construct(\TYPO3\Flow\Resource\Resource $resource) {
  141.         $this->resource = $resource;
  142.         $this->initialize();
  143.     }
  144.  
  145.     /**
  146.      * Calculates image width, height and type from the image resource
  147.      * The getimagesize() method may either return FALSE; or throw a Warning
  148.      * which is translated to a \TYPO3\Flow\Error\Exception by Flow. In both
  149.      * cases \TYPO3\Media\Exception\ImageFileException should be thrown.
  150.      *
  151.      * @throws \TYPO3\Media\Exception\ImageFileException
  152.      * @return void
  153.      */
  154.     protected function initialize() {
  155.         try {
  156.             $imageSize = getimagesize('resource://' . $this->resource->getResourcePointer()->getHash());
  157.             if ($imageSize === FALSE) {
  158.                 throw new \TYPO3\Media\Exception\ImageFileException('The given resource was not a valid image file', 1336662898);
  159.             }
  160.             $this->width = (integer)$imageSize[0];
  161.             $this->height = (integer)$imageSize[1];
  162.             $this->type = (integer)$imageSize[2];
  163.         } catch(\TYPO3\Media\Exception\ImageFileException $exception) {
  164.             throw $exception;
  165.         } catch(\TYPO3\Flow\Error\Exception $exception) {
  166.             $exceptionMessage = 'An error with code "' . $exception->getCode() . '" occured when trying to read the image: "' . $exception->getMessage() . '"';
  167.             throw new \TYPO3\Media\Exception\ImageFileException($exceptionMessage, 1336663970);
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Sets the image resource and (re-)initializes the image
  173.      *
  174.      * @param \TYPO3\Flow\Resource\Resource $resource
  175.      * @return void
  176.      */
  177.     public function setResource(\TYPO3\Flow\Resource\Resource $resource) {
  178.         $this->resource = $resource;
  179.         $this->initialize();
  180.     }
  181.  
  182.     /**
  183.      * Sets the title of this image (optional)
  184.      *
  185.      * @param string $title
  186.      * @return void
  187.      */
  188.     public function setTitle($title) {
  189.         $this->title = $title;
  190.     }
  191.  
  192.     /**
  193.      * The title of this image
  194.      *
  195.      * @return string
  196.      */
  197.     public function getTitle() {
  198.         return $this->title;
  199.     }
  200.  
  201.     /**
  202.      * Resource of the original image file
  203.      *
  204.      * @return \TYPO3\Flow\Resource\Resource
  205.      */
  206.     public function getResource() {
  207.         return $this->resource;
  208.     }
  209.  
  210.     /**
  211.      * Width of the image in pixels
  212.      *
  213.      * @return integer
  214.      */
  215.     public function getWidth() {
  216.         return $this->width;
  217.     }
  218.  
  219.     /**
  220.      * Height of the image in pixels
  221.      *
  222.      * @return integer
  223.      */
  224.     public function getHeight() {
  225.         return $this->height;
  226.     }
  227.  
  228.     /**
  229.      * Edge / aspect ratio of the image
  230.      *
  231.      * @param boolean $respectOrientation If false (the default), orientation is disregarded and always a value >= 1 is returned (like usual in "4 / 3" or "16 / 9")
  232.      * @return float
  233.      */
  234.     public function getAspectRatio($respectOrientation = FALSE) {
  235.         $aspectRatio = $this->getWidth() / $this->getHeight();
  236.         if ($respectOrientation === FALSE && $aspectRatio < 1) {
  237.             $aspectRatio = 1 / $aspectRatio;
  238.         }
  239.  
  240.         return $aspectRatio;
  241.     }
  242.  
  243.     /**
  244.      * Orientation of this image, i.e. portrait, landscape or square
  245.      *
  246.      * @return string One of this interface's ORIENTATION_* constants.
  247.      */
  248.     public function getOrientation() {
  249.         $aspectRatio = $this->getAspectRatio(TRUE);
  250.         if ($aspectRatio > 1) {
  251.             return ImageInterface::ORIENTATION_LANDSCAPE;
  252.         }
  253.         elseif ($aspectRatio < 1) {
  254.             return ImageInterface::ORIENTATION_PORTRAIT;
  255.         }
  256.         else {
  257.             return ImageInterface::ORIENTATION_SQUARE;
  258.         }
  259.     }
  260.  
  261.     /**
  262.      * Whether this image is square aspect ratio and therefore has a square orientation
  263.      *
  264.      * @return boolean
  265.      */
  266.     public function isOrientationSquare() {
  267.         return $this->getOrientation() === ImageInterface::ORIENTATION_SQUARE;
  268.     }
  269.  
  270.     /**
  271.      * Whether this image is in landscape orientation
  272.      *
  273.      * @return boolean
  274.      */
  275.     public function isOrientationLandscape() {
  276.         return $this->getOrientation() === ImageInterface::ORIENTATION_LANDSCAPE;
  277.     }
  278.  
  279.     /**
  280.      * Whether this image is in portrait orientation
  281.      *
  282.      * @return boolean
  283.      */
  284.     public function isOrientationPortrait() {
  285.         return $this->getOrientation() === ImageInterface::ORIENTATION_PORTRAIT;
  286.     }
  287.  
  288.     /**
  289.      * One of PHPs IMAGETYPE_* constants that reflects the image type
  290.      *
  291.      * @see http://php.net/manual/image.constants.php
  292.      * @return integer
  293.      */
  294.     public function getType() {
  295.         return $this->type;
  296.     }
  297.  
  298.     /**
  299.      * File extension of the image without leading dot.
  300.      * @see http://www.php.net/manual/function.image-type-to-extension.php
  301.      *
  302.      * @return string
  303.      */
  304.     public function getFileExtension() {
  305.         return image_type_to_extension($this->type, FALSE);
  306.     }
  307.  
  308.     /**
  309.      * Returns a thumbnail of this image.
  310.      *
  311.      * If maximum width/height is not specified or exceed the original images size,
  312.      * width/height of the original image is used
  313.      *
  314.      * Note: The image variant that will be created is intentionally not added to the imageVariants collection of this image
  315.      * If you want to create a persisted image variant, use createImageVariant() instead
  316.      *
  317.      * @param integer $maximumWidth
  318.      * @param integer $maximumHeight
  319.      * @param string $ratioMode Whether the resulting image should be cropped if both edge's sizes are supplied that would hurt the aspect ratio.
  320.      * @return \TYPO3\Media\Domain\Model\ImageVariant
  321.      * @see \TYPO3\Media\Domain\Service\ImageService::transformImage()
  322.      */
  323.     public function getThumbnail($maximumWidth = NULL, $maximumHeight = NULL, $ratioMode = ImageInterface::RATIOMODE_INSET) {
  324.         $processingInstructions = array(
  325.             array(
  326.                 'command' => 'thumbnail',
  327.                 'options' => array(
  328.                     'size' => array(
  329.                         'width' => intval($maximumWidth ?: $this->width),
  330.                         'height' => intval($maximumHeight ?: $this->height)
  331.                     ),
  332.                     'mode' => $ratioMode
  333.                 ),
  334.             ),
  335.         );
  336.         return new ImageVariant($this, $processingInstructions);
  337.     }
  338.  
  339.     /**
  340.      * Set all variants of this image.
  341.      *
  342.      * @param array<\TYPO3\Media\Domain\Model\ImageVariant> $imageVariants
  343.      * @return void
  344.      */
  345.     public function setImageVariants(array $imageVariants) {
  346.         $this->imageVariants = $imageVariants;
  347.     }
  348.  
  349.     /**
  350.      * Return all variants of this image.
  351.      *
  352.      * @return array<\TYPO3\Media\Domain\Model\ImageVariant>
  353.      */
  354.     public function getImageVariants() {
  355.         return $this->imageVariants;
  356.     }
  357.  
  358.     /**
  359.      * Create a variant of this image using the given processing instructions.
  360.      *
  361.      * The variant is attached to the image for later (re-)use. If the optional alias parameter is specified, the image
  362.      * variant can later be retrieved via getImageVariantByAlias()
  363.      * An alias could, for example, be "thumbnail", "small", "micro", "face-emphasized" etc.
  364.      *
  365.      * NOTE: If you want the new image variant to be persisted, make sure to update the image with ImageRepository::update()
  366.      *
  367.      * @param array $processingInstructions
  368.      * @param string $alias An optional alias name to allow easier retrieving of a previously created image variant
  369.      * @return \TYPO3\Media\Domain\Model\ImageVariant
  370.      */
  371.     public function createImageVariant(array $processingInstructions, $alias = NULL) {
  372.         $imageVariant = new ImageVariant($this, $processingInstructions, $alias);
  373.         // FIXME we currently need a unique hash because $this->imageVariants has to be an array in order to be serialized by Doctrine
  374.         $uniqueHash = sha1($this->resource->getResourcePointer()->getHash() . '|' . ($alias ?: serialize($processingInstructions)));
  375.         $this->imageVariants[$uniqueHash] = $imageVariant;
  376.         return $imageVariant;
  377.     }
  378.  
  379.     /**
  380.      * Remove the given variant from this image.
  381.      *
  382.      * NOTE: If you want to remove the image variant from persistence, make sure to update the image with ImageRepository::update()
  383.      *
  384.      * @param \TYPO3\Media\Domain\Model\ImageVariant $imageVariant
  385.      * @return void
  386.      */
  387.     public function removeImageVariant(ImageVariant $imageVariant) {
  388.         // FIXME we currently need a unique hash because $this->imageVariants has to be an array in order to be serialized by Doctrine
  389.         $uniqueHash = sha1($this->resource->getResourcePointer()->getHash() . '|' . ($imageVariant->getAlias() ?: serialize($imageVariant->getProcessingInstructions())));
  390.         if (isset($this->imageVariants[$uniqueHash])) {
  391.             unset($this->imageVariants[$uniqueHash]);
  392.         }
  393.     }
  394.  
  395.     /**
  396.      * Gets an ImageVariant by its alias
  397.      *
  398.      * @param string $alias
  399.      * @return \TYPO3\Media\Domain\Model\ImageVariant The ImageVariant if such found for the given alias, or NULL if not
  400.      */
  401.     public function getImageVariantByAlias($alias) {
  402.         foreach ($this->imageVariants as $imageVariant) {
  403.             if ($imageVariant->getAlias() === $alias) {
  404.                 return $imageVariant;
  405.             }
  406.         }
  407.         return NULL;
  408.     }
  409.  
  410.     /**
  411.      * Removes an ImageVariant by its alias
  412.      *
  413.      * @param string $alias
  414.      * @return void
  415.      */
  416.     public function removeImageVariantByAlias($alias) {
  417.         $imageVariant = $this->getImageVariantByAlias($alias);
  418.         if ($imageVariant instanceof ImageVariant) {
  419.             $this->removeImageVariant($imageVariant);
  420.         }
  421.     }
  422.  
  423. }
  424.  
  425. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement