From 1656a2f11a49d33b2d01f22b6d460338cfdf3b10 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 19 Jul 2015 18:37:29 +0200 Subject: [PATCH] Experimenting with a preview for attachments. --- app/Http/Controllers/AttachmentController.php | 54 +++++++++++++++++++ resources/twig/transactions/show.twig | 5 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php index 533fa6f9c1..494cb1b763 100644 --- a/app/Http/Controllers/AttachmentController.php +++ b/app/Http/Controllers/AttachmentController.php @@ -8,6 +8,7 @@ use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Requests\AttachmentFormRequest; use FireflyIII\Models\Attachment; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; +use FireflyIII\Support\CacheProperties; use Input; use Preferences; use Session; @@ -115,6 +116,59 @@ class AttachmentController extends Controller } } + /** + * @param Attachment $attachment + */ + public function preview(AttachmentHelperInterface $helper, Attachment $attachment) + { + if (!function_exists('imagecreatetruecolor')) { + abort(500); + } + + + $mime = $attachment->mime; + $cache = new CacheProperties; + $cache->addProperty('preview-attachment'); + $cache->addProperty($attachment->id); + if ($cache->has()) { + header('Content-Type: image/png'); + + return $cache->get(); + } + + switch ($mime) { + default: + $img = imagecreatetruecolor(100, 10); + imagesavealpha($img, true); + + $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127); + imagefill($img, 0, 0, $trans_colour); + header('Content-Type: image/png'); + imagepng($img); + imagedestroy($img); + $cache->store($img); + exit; + break; + case 'image/jpeg': + case 'image/png': + $img = imagecreatetruecolor(100, 100); + $source = $helper->getAttachmentLocation($attachment); + $decrypt = Crypt::decrypt(file_get_contents($source)); + $original = imagecreatefromstring($decrypt); + $width = imagesx($original); + $height = imagesy($original); + + imagecopyresampled($img, $original, 0, 0, 0, 0, 100, 100, $width, $height); + + header('Content-Type: image/png'); + imagepng($img); + imagedestroy($img); + exit; + + break; + } + } + /** * @param AttachmentFormRequest $request diff --git a/resources/twig/transactions/show.twig b/resources/twig/transactions/show.twig index 9cb115f2c7..15c14a1ba7 100644 --- a/resources/twig/transactions/show.twig +++ b/resources/twig/transactions/show.twig @@ -93,7 +93,7 @@ - + {% if att.title %} {{ att.title }} {% else %} @@ -106,6 +106,9 @@ {{ att.description }} {% endif %} + + + {% endfor %}