Experimenting with a preview for attachments.

This commit is contained in:
James Cole
2015-07-19 18:37:29 +02:00
parent 4dbc135dce
commit 1656a2f11a
2 changed files with 58 additions and 1 deletions

View File

@@ -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