FEATURE: make thumbnails from uploaded image

This commit is contained in:
Michal Kváček
2016-09-26 15:21:30 +02:00
parent 6868af609f
commit cd28625309
11 changed files with 262 additions and 12 deletions
+1 -6
View File
@@ -17,12 +17,7 @@ class FileManager
switch ($type) {
case 'image':
// try to check if file have thumbnail
if (file_exists(config('app.thumbnail_url') . '/' . $file->filename)) {
$url = config('app.thumbnail_url') . '/' . $file->filename;
} else {
$url = config('app.upload_url') . '/' . $file->filename;
}
$url = Template::thumbnail($file->filename);
return '<img src="' . $url . '" alt="' . $file->description . '" />';
break;
+12
View File
@@ -61,6 +61,18 @@ class Template
return config('app.upload_url') . '/' . $filename;
}
public static function thumbnail($filename)
{
// try to check if file have thumbnail
if (file_exists(config('app.thumbnail_path') . '/' . $filename)) {
$url = config('app.thumbnail_url') . '/' . $filename;
} else {
$url = self::upload($filename);
}
return $url;
}
/**
* Get path to given asset
*
+35
View File
@@ -4,9 +4,13 @@ namespace App\Models;
use App\Events\UploadFile;
use App\Http\Requests\Request;
use Franzose\ClosureTable\Extensions\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File as FileFacade;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpFoundation\File\File as SymphonyFile;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -63,6 +67,12 @@ class File extends Model
// save file into physical location (local FS)
$uploadedFile = $file->move($destination, $filename);
// is uploaded file an image?
if (Str::startsWith($uploadedFile->getMimeType(), 'image')) {
// yes, create thumbnail
$this->createThumbnail($uploadedFile);
}
// insert record into database
$id = $this->saveIntoDatabase($uploadedFile, $request, $file->getClientOriginalName());
@@ -118,4 +128,29 @@ class File extends Model
return $newFile->id;
}
/**
* Create thumbnail of given image
*
* @param SymphonyFile $file
*/
private function createThumbnail(SymphonyFile $file)
{
// does thumbnail directory exist?
if (!is_dir(config('app.thumbnail_path'))) {
FileFacade::makeDirectory(config('app.thumbnail_path'));
}
// get image instance
$image = Image::make(config('app.upload_path') . DIRECTORY_SEPARATOR . $file->getFilename());
// resize image and keep aspect ration
$image->resize(env('THUMBNAIL_WIDTH', 300), null, function (Constraint $constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
// save thumbnail with lower quality
$image->save(config('app.thumbnail_path') . DIRECTORY_SEPARATOR . $file->getFilename(), 75);
}
}
+9 -1
View File
@@ -65,7 +65,7 @@ class GlobalViewDataProvider extends ServiceProvider
return preg_replace($pattern, $code, $view);
});
// shortcut for loading uploaded images
// shortcut for loading uploaded files
Blade::extend(function ($view) {
$pattern = $this->createPattern('upload');
$code = '<?php echo \App\Helpers\Template::upload($2); ?>';
@@ -73,6 +73,14 @@ class GlobalViewDataProvider extends ServiceProvider
return preg_replace($pattern, $code, $view);
});
// shortcut for loading thumbnails of uploaded images
Blade::extend(function ($view) {
$pattern = $this->createPattern('upload');
$code = '<?php echo \App\Helpers\Template::thumbnail($2); ?>';
return preg_replace($pattern, $code, $view);
});
// shortcut for displaying Gravatar
Blade::extend(function ($view) {
$pattern = $this->createPattern('gravatar');
+2 -1
View File
@@ -13,7 +13,8 @@
"pingpong/menus": "^2.2",
"watson/sitemap": "^2.0",
"mcamara/laravel-localization": "^1.1",
"franzose/closure-table": "^4.1"
"franzose/closure-table": "^4.1",
"intervention/image": "^2.3"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Generated
+179 -2
View File
@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "34fec17afe62b99ed90d0f72d3536a7b",
"content-hash": "58f7e332f9be3faf1f8d481191cf1bb1",
"hash": "066078c74cc6b8d99111c0e9b4b385d6",
"content-hash": "1574402e32047ac8589b6fc08d6d744c",
"packages": [
{
"name": "classpreloader/classpreloader",
@@ -704,6 +704,133 @@
],
"time": "2016-07-08 08:19:46"
},
{
"name": "guzzlehttp/psr7",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "64862e854f876bc5aee2996cab2f552db8586065"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/64862e854f876bc5aee2996cab2f552db8586065",
"reference": "64862e854f876bc5aee2996cab2f552db8586065",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2016-08-03 09:33:34"
},
{
"name": "intervention/image",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "4064a980324f6c3bfa2bd981dfb247afa705ec3c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/4064a980324f6c3bfa2bd981dfb247afa705ec3c",
"reference": "4064a980324f6c3bfa2bd981dfb247afa705ec3c",
"shasum": ""
},
"require": {
"ext-fileinfo": "*",
"guzzlehttp/psr7": "~1.1",
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "~0.9.2",
"phpunit/phpunit": "3.*"
},
"suggest": {
"ext-gd": "to use GD library based image processing.",
"ext-imagick": "to use Imagick based image processing.",
"intervention/imagecache": "Caching extension for the Intervention Image library"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
}
},
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src/Intervention/Image"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@olivervogel.net",
"homepage": "http://olivervogel.net/"
}
],
"description": "Image handling and manipulation library with support for Laravel integration",
"homepage": "http://image.intervention.io/",
"keywords": [
"gd",
"image",
"imagick",
"laravel",
"thumbnail",
"watermark"
],
"time": "2016-09-01 17:04:03"
},
{
"name": "jakub-onderka/php-console-color",
"version": "0.1",
@@ -1632,6 +1759,56 @@
"description": "Support Component of Pingpong Sky Package",
"time": "2016-01-07 08:23:04"
},
{
"name": "psr/http-message",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06 14:39:51"
},
{
"name": "psr/log",
"version": "dev-master",
+1
View File
@@ -17,4 +17,5 @@
!services.php
!session.php
!sitemap.php
!image.php
!view.php
+1
View File
@@ -161,6 +161,7 @@ return [
App\Providers\SubmodulesManager::class,
Franzose\ClosureTable\ClosureTableServiceProvider::class,
\Watson\Sitemap\SitemapServiceProvider::class,
Intervention\Image\ImageServiceProvider::class,
/*
* Application Service Providers...
*/
+20
View File
@@ -0,0 +1,20 @@
<?php
return array(
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/
'driver' => 'gd'
);
@@ -18,7 +18,7 @@
<label class="control-label">
@lang('admin/contents.uploadTitleImage')
</label>
<input type="file" id="titlePhoto" name="titlePhoto" class="form-control" />
<input type="file" id="titlePhoto" accept=".jpg,.jpeg,.png" name="titlePhoto" class="form-control" />
@include('admin.snippets.hasError', ['field' => 'titlePhoto'])
</div>
@@ -1,7 +1,7 @@
<div class="row">
@forelse($files as $file)
<div class="col-sm-4 col-md-2 text-center">
<div class="col-sm-4 col-md-3 text-center">
@include('admin.snippets.card', [
'body' => '<a href="'.route('upload.detail', ['id' => $file->id]).'">'.\App\Helpers\FileManager::getPreview($file).'</a>',
'footerClass' => 'text-right',