Skip to content

Firefly III: a personal finances manager

Experimenting with a preview for attachments.

James Cole1656a2f11a49
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 @@
</td>
<td>
<i class="fa {{ att.mime|mimeIcon }}"></i>
- <a href="{{ route('attachments.download', att.id) }}" title="{{ att.filename }}">
+ <a href="{{ route('attachments.download', att.id) }}" tle="{{ att.filename }}">
{% if att.title %}
{{ att.title }}
{% else %}
@@ -106,6 +106,9 @@
<em>{{ att.description }}</em>
{% endif %}
</td>
+ <td style="width:100px;">
+ <img src="{{ route('attachments.preview', att.id) }}"></img>
+ </td>
</tr>
{% endfor %}
</table>