partkeepr

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

commit 91d6f315d38b2d9b9c45a21803658f5b7c6b2005
parent 64e3ed3b7f1909e1c24492fd9df0fdac0071f6b5
Author: Felicitus <felicitus@felicitus.org>
Date:   Fri, 17 Jun 2011 15:33:23 +0800

Misc image-related fixes:
- Display a placeholder image if the specified image was not found
- Added selection of scale methods directly as parameter to image.php
- Added parameter to invalidate the cache in image.php
- Added a scale method which pads the image to the output size

Diffstat:
Mfrontend/image.php | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/de/RaumZeitLabor/PartKeepr/Image/Image.php | 38+++++++++++++++++++++++++++++++++++---
Msrc/de/RaumZeitLabor/PartKeepr/UploadedFile/UploadedFile.php | 2++
3 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/frontend/image.php b/frontend/image.php @@ -1,11 +1,16 @@ <?php namespace de\RaumZeitLabor\PartKeepr\Frontend; +use de\RaumZeitLabor\PartKeepr\Part\PartImage; + +use de\RaumZeitLabor\PartKeepr\Footprint\FootprintImage; + use de\RaumZeitLabor\PartKeepr\TempImage\TempImage; declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\PartKeepr; use de\RaumZeitLabor\PartKeepr\Image\Image; +use de\RaumZeitLabor\PartKeepr\Image\CachedImage; use de\RaumZeitLabor\PartKeepr\Manufacturer\ManufacturerICLogo; include("../src/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); @@ -20,6 +25,12 @@ try { case Image::IMAGE_ICLOGO: $image = ManufacturerICLogo::loadById($id); break; + case Image::IMAGE_FOOTPRINT: + $image = FootprintImage::loadById($id); + break; + case Image::IMAGE_PART: + $image = PartImage::loadById($id); + break; default: $image = null; // Add default image? @@ -35,9 +46,57 @@ if ($image == null) { if (array_key_exists("tmpId", $_REQUEST)) { $image = TempImage::loadById($_REQUEST["tmpId"]); } + + if ($image === null) { + /* The image is still null - output an "image not found" image. */ + $image = imagecreate($_REQUEST["w"], $_REQUEST["h"]); + $white = imagecolorallocate($image, 255,255,255); + $black = imagecolorallocate($image, 0,0,0); + + header("Content-Type: image/png"); + + $w = $_REQUEST["w"]-1; + $h = $_REQUEST["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); + exit(); + + } +} + +$mode = "fit"; + +if (array_key_exists("m", $_REQUEST)) { + $mode = $_REQUEST["m"]; +} + +if (array_key_exists("cache", $_REQUEST)) { + if ($_REQUEST["cache"] == "false") { + CachedImage::invalidate($image); + } +} + +switch ($mode) { + case "fitpadding": + $file = $image->fitWithinPadding($_REQUEST["w"],$_REQUEST["h"]); + break; + case "fitexact": + $file = $image->fitWithinExact($_REQUEST["w"],$_REQUEST["h"]); + break; + case "scale": + $file = $image->scaleTo($_REQUEST["w"],$_REQUEST["h"]); + break; + case "fit": + default: + $file = $image->fitWithin($_REQUEST["w"],$_REQUEST["h"]); + break; + } -$file = $image->fitWithin($_REQUEST["w"],$_REQUEST["h"]); header("Content-Type: image/png"); diff --git a/src/de/RaumZeitLabor/PartKeepr/Image/Image.php b/src/de/RaumZeitLabor/PartKeepr/Image/Image.php @@ -14,6 +14,7 @@ abstract class Image extends UploadedFile { const IMAGE_ICLOGO = "iclogo"; const IMAGE_TEMP = "temp"; const IMAGE_PART = "part"; + const IMAGE_FOOTPRINT = "footprint"; /** * Constructs a new image object. @@ -37,6 +38,7 @@ abstract class Image extends UploadedFile { case Image::IMAGE_ICLOGO: case Image::IMAGE_TEMP: case Image::IMAGE_PART: + case Image::IMAGE_FOOTPRINT: parent::setType($type); break; default: @@ -154,16 +156,23 @@ abstract class Image extends UploadedFile { } /** - * Scales the image to fit within the given size. + * Scales the image to fit within the given size. * * @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 ($w, $h) { + public function fitWithin ($w, $h, $padding = false) { $this->ensureCachedirExists(); - $outputFile = Configuration::getOption("partkeepr.images.cache").md5($this->getFilename().$w."x".$h."fw").".png"; + if ($padding) { + $pd = "p"; + } else { + $pd = ""; + } + + $outputFile = Configuration::getOption("partkeepr.images.cache").md5($this->getFilename().$w."x".$h."fw".$pd).".png"; if (file_exists($outputFile)) { return $outputFile; @@ -177,12 +186,24 @@ abstract class Image extends UploadedFile { $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); $cachedImage = new CachedImage($this, $outputFile); @@ -192,6 +213,17 @@ abstract class Image extends UploadedFile { } /** + * Convinience method for fitWithin. Automatically pads the image. + * + * @param int $w The width + * @param int $h The height + * @return string The path to the scaled file + */ + public function fitWithinPadding ($w, $h) { + return $this->fitWithin($w, $h, true); + } + + /** * Ensures that the image cache dir exists. */ public function ensureCachedirExists () { diff --git a/src/de/RaumZeitLabor/PartKeepr/UploadedFile/UploadedFile.php b/src/de/RaumZeitLabor/PartKeepr/UploadedFile/UploadedFile.php @@ -95,6 +95,8 @@ abstract class UploadedFile extends BaseEntity { $this->ensureFilePathExists(); copy($path, $this->getFilename()); + + $this->setOriginalFilename(basename($path)); } /**