partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 795d350ed61f585b85b5959ad3af3a2e52d40447
parent 7d63074c9944ebd4821e933f58c50feff892df85
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 18 Jul 2015 03:43:39 +0200

Moved CachedImage to ImageBundle, removed obsolete code

Diffstat:
Msrc/PartKeepr/ImageBundle/Controller/ImageController.php | 2+-
Asrc/PartKeepr/ImageBundle/Entity/CachedImage.php | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/ImageBundle/Services/ImageService.php | 2+-
Dsrc/backend/PartKeepr/Image/CachedImage.php | 70----------------------------------------------------------------------
Dsrc/backend/PartKeepr/Image/Exceptions/InvalidImageTypeException.php | 12------------
Dsrc/backend/PartKeepr/Image/ImageRenderer.php | 134-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/Image/RenderableImage.php | 17-----------------
Msrc/backend/PartKeepr/PartKeepr.php | 2+-
Dsrc/backend/PartKeepr/TempImage/TempImageService.php | 31-------------------------------
9 files changed, 66 insertions(+), 267 deletions(-)

diff --git a/src/PartKeepr/ImageBundle/Controller/ImageController.php b/src/PartKeepr/ImageBundle/Controller/ImageController.php @@ -6,7 +6,7 @@ use Imagine\Gd\Imagine; use Imagine\Image\Box; use Imagine\Image\Point; use Nelmio\ApiDocBundle\Annotation\ApiDoc; -use PartKeepr\Image\CachedImage; +use PartKeepr\ImageBundle\Entity\CachedImage; use PartKeepr\ImageBundle\Entity\Image as PartKeeprImage; use PartKeepr\ImageBundle\Response\ImageNotFoundResponse; use PartKeepr\UploadedFileBundle\Controller\FileController; diff --git a/src/PartKeepr/ImageBundle/Entity/CachedImage.php b/src/PartKeepr/ImageBundle/Entity/CachedImage.php @@ -0,0 +1,62 @@ +<?php +namespace PartKeepr\ImageBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\Util\BaseEntity; + +/** + * Represents a cached image. Cached images are used for scale/resize + * operations, so that the resize/scale operation doesn't need to be done + * every time a scaled/resized image is requested. + * + * @ORM\Entity + */ +class CachedImage extends BaseEntity +{ + /** + * Specifies the ID of the original image + * + * @var integer + * @ORM\Column(type="integer") + */ + private $originalId; + + /** + * Specifies the type of the original image. + * + * @var string + * @ORM\Column(type="string") + **/ + private $originalType; + + /** + * The cache filename of the image + * + * @var string + * @ORM\Column(type="string") + */ + private $cacheFile; + + /** + * Creates a new cache entry for a specific image. + * + * @param Image $image The image to cache + * @param string $cacheFile The file which holds the cached image + */ + public function __construct(Image $image, $cacheFile) + { + $this->originalId = (int)$image->getId(); + $this->originalType = $image->getType(); + $this->cacheFile = $cacheFile; + } + + /** + * Returns the cache file + * + * @return string The cache file including path + */ + public function getCacheFile() + { + return $this->cacheFile; + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/ImageBundle/Services/ImageService.php b/src/PartKeepr/ImageBundle/Services/ImageService.php @@ -4,7 +4,7 @@ namespace PartKeepr\ImageBundle\Services; use Doctrine\ORM\EntityManager; -use PartKeepr\Image\CachedImage; +use PartKeepr\ImageBundle\Entity\CachedImage; use PartKeepr\UploadedFileBundle\Entity\UploadedFile; use PartKeepr\UploadedFileBundle\Services\UploadedFileService; use Symfony\Component\HttpFoundation\File\File; diff --git a/src/backend/PartKeepr/Image/CachedImage.php b/src/backend/PartKeepr/Image/CachedImage.php @@ -1,69 +0,0 @@ -<?php -namespace PartKeepr\Image; - -use PartKeepr\ImageBundle\Entity\Image; -use PartKeepr\PartKeepr, - Doctrine\ORM\Mapping as ORM; - -/** - * Represents a cached image. Cached images are used for scale/resize - * operations, so that the resize/scale operation doesn't need to be done - * every time a scaled/resized image is requested. - * - * @ORM\Entity - */ -class CachedImage { - /** - * Specifies the ID of the cached image. - * - * @var integer - * @ORM\Id - * @ORM\Column(type="integer") - * @ORM\GeneratedValue(strategy="AUTO") - **/ - private $id; - - /** - * Specifies the ID of the original image - * - * @var integer - * @ORM\Column(type="integer") - */ - private $originalId; - - /** - * Specifies the type of the original image. - * - * @var string - * @ORM\Column(type="string") - **/ - private $originalType; - - /** - * The cache filename of the image - * - * @var string - * @ORM\Column(type="string") - */ - private $cacheFile; - - /** - * Creates a new cache entry for a specific image. - * - * @param Image $image The image to cache - * @param string $cacheFile The file which holds the cached image - */ - public function __construct (Image $image, $cacheFile) { - $this->originalId = (int)$image->getId(); - $this->originalType = $image->getType(); - $this->cacheFile = $cacheFile; - } - - /** - * Returns the cache file - * @return string The cache file including path - */ - public function getCacheFile () { - return $this->cacheFile; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/Image/Exceptions/InvalidImageTypeException.php b/src/backend/PartKeepr/Image/Exceptions/InvalidImageTypeException.php @@ -1,11 +0,0 @@ -<?php -namespace PartKeepr\Image\Exceptions; - -/** - * Thrown if an invalid type was passed - */ -class InvalidImageTypeException extends \Exception { - public function __construct ($type) { - parent::__construct("The image type $type is unknown."); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/Image/ImageRenderer.php b/src/backend/PartKeepr/Image/ImageRenderer.php @@ -1,133 +0,0 @@ -<?php -namespace PartKeepr\Image; - -use PartKeepr\Util\Configuration; - -class ImageRenderer { - private $image = null; - - public function __construct (RenderableImage $image) { - $this->image = $image; - } - - /** - * Renders and outputs the "image not found" image. - * - * Sends the header and immediately outputs the image. - * - * @param int $w Width of the image - * @param int $h Height of the image - */ - public static function outputRenderNotFoundImage ($w, $h) { - $image = \imagecreate($w, $h); - $white = \imagecolorallocate($image, 255,255,255); - $black = \imagecolorallocate($image, 0,0,0); - - header("Content-Type: image/png"); - - $w = $w-1; - $h = $h-1; - \imagefill($image, 0,0, $white); - - /* Draw the X */ - \imageline($image, 0,0,$w,$h, $black); - \imageline($image, $w,0,0,$h, $black); - \imagepng($image); - } - - /** - * Scales the image to fit within the given size. - * - * @param string $outputFile The target file - * @param int $w The width - * @param int $h The height - * @param boolean $padding If true, pad the output image to the given size (transparent background). - * @return string The path to the scaled file - */ - public function fitWithin ($outputFile, $w, $h, $padding = false) { - $image = new \Imagick(); - $image->readImage($this->image->getFilename()); - - $sourceAspectRatio = $image->getImageWidth() / $image->getImageHeight(); - $targetAspectRatio = $w / $h; - - $filter = \Imagick::FILTER_UNDEFINED; - $blur = 1; - - $targetHeight = $h; - $targetWidth = $w; - - if ($sourceAspectRatio < $targetAspectRatio) { - $targetWidth = $h * $sourceAspectRatio; - $image->resizeImage($h * $sourceAspectRatio, $h, $filter, $blur); - } else { - $targetHeight = $w / $sourceAspectRatio; - $image->resizeImage($w, $w / $sourceAspectRatio, $filter, $blur); - } - - if ($padding) { - $posX = intval(($w - $targetWidth) / 2); - $posY = intval(($h - $targetHeight) / 2); - - $image->extentImage($w, $h,-$posX, -$posY); - } - - $image->writeImage($outputFile); - - return $outputFile; - } - - /** - * Scales the image to fit exactly within the given size. - * - * This method ensures that no blank space is in the output image, - * and that the output image is exactly the width and height specified. - * - * @param string $outputFile The output file - * @param int $w The width - * @param int $h The height - * @return string The path to the scaled file - */ - public function fitWithinExact ($outputFile, $w, $h) { - $image = new \Imagick(); - $image->readImage($this->image->getFilename()); - - $sourceAspectRatio = $image->getImageWidth() / $image->getImageHeight(); - $targetAspectRatio = $w / $h; - - $filter = \Imagick::FILTER_UNDEFINED; - $blur = 1; - - if ($sourceAspectRatio < $targetAspectRatio) { - $image->resizeImage($w, $w / $sourceAspectRatio, $filter, $blur); - } else { - $image->resizeImage($h * $sourceAspectRatio, $h, $filter, $blur); - } - - $offsetX = intval(($image->getImageWidth() - $w)/2); - $offsetY = intval(($image->getImageHeight() - $h)/2); - - $image = $image->getImageRegion($w, $h, $offsetX, $offsetY); - - $image->writeImage($outputFile); - - return $outputFile; - } - - /** - * Scales the image to a specific width and height - * - * @param string $outputFile The output file - * @param int $w The width - * @param int $h The height - * @return string The path to the scaled file - */ - public function scaleTo ($outputFile, $w, $h) { - $image = new \Imagick(); - $image->readImage($this->image->getFilename()); - $image->adaptiveResizeImage($w, $h); - $image->writeImage($outputFile); - - return $outputFile; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/Image/RenderableImage.php b/src/backend/PartKeepr/Image/RenderableImage.php @@ -1,16 +0,0 @@ -<?php -namespace PartKeepr\Image; - -/** - * Interface for any image which needs to be rendered. - */ -interface RenderableImage { - /** - * Returns the full image filename including path. - * This is the image where all scale operations take place on. - * - * @param none - * @return string The full image filename including path. - */ - public function getFilename (); -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -186,7 +186,7 @@ class PartKeepr { 'PartKeepr\DistributorBundle\Entity\Distributor', 'PartKeepr\ImageBundle\Entity\Image', - 'PartKeepr\Image\CachedImage', + 'PartKeepr\ImageBundle\Entity\CachedImage', 'PartKeepr\ImageBundle\Entity\TempImage', 'PartKeepr\UploadedFileBundle\Entity\TempUploadedFile', diff --git a/src/backend/PartKeepr/TempImage/TempImageService.php b/src/backend/PartKeepr/TempImage/TempImageService.php @@ -1,30 +0,0 @@ -<?php -namespace PartKeepr\TempImage; - -use PartKeepr\Service\Service; -use PartKeepr\ImageBundle\Entity\TempImage; -use PartKeepr\PartKeepr; - -class TempImageService extends Service { - public function upload () { - $image = new TempImage(); - - if (array_key_exists("userfile", $_FILES) && file_exists($_FILES["userfile"]["tmp_name"])) { - $file = $_FILES['userfile']['tmp_name']; - $filename = $_FILES['userfile']['name']; - - $image->replace($file); - $image->setOriginalFilename(basename($filename)); - } elseif (array_key_exists("url", $_REQUEST)) { - $image->replaceFromURL($_REQUEST["url"]); - } else { - throw new \Exception("Error: No valid file given"); - } - - PartKeepr::getEM()->persist($image); - PartKeepr::getEM()->flush(); - - return array("id" => $image->getId(), "extension" => $image->getExtension(), "size" => $image->getSize(), "originalFilename" => $image->getOriginalFilename()); - } -} - - \ No newline at end of file