Compare commits
20 Commits
5.4
...
8dda10569e
Author | SHA1 | Date | |
---|---|---|---|
8dda10569e | |||
df3bd4c335 | |||
2711b92640 | |||
099b062215 | |||
c515484670 | |||
480a27897e | |||
71a89a5a05 | |||
11f17dee47 | |||
0ce7050828 | |||
1ea4bc83e7 | |||
5d5a9bd4cb | |||
![]() |
a08c93f650 | ||
![]() |
1a9007428e | ||
6f11372668 | |||
ecdf6c4976 | |||
![]() |
b15ab7fd07 | ||
![]() |
7416cf37ec | ||
9f448ef69c | |||
16a4e3ce2a | |||
a1dea266c8 |
@@ -11,7 +11,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.0.0",
|
"php": ">=7.0.0",
|
||||||
"laravel/lumen-framework": "5.4.*",
|
"laravel/lumen-framework": "^7.0",
|
||||||
"symfony/process" : "3.*",
|
"symfony/process" : "3.*",
|
||||||
"intervention/image": "^2.4",
|
"intervention/image": "^2.4",
|
||||||
"intervention/imagecache": "^2.3",
|
"intervention/imagecache": "^2.3",
|
||||||
|
51
config/image-optimizer.php
Executable file
51
config/image-optimizer.php
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
use Spatie\ImageOptimizer\Optimizers\Svgo;
|
||||||
|
use Spatie\ImageOptimizer\Optimizers\Optipng;
|
||||||
|
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
|
||||||
|
use Spatie\ImageOptimizer\Optimizers\Pngquant;
|
||||||
|
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
|
||||||
|
|
||||||
|
return [
|
||||||
|
/**
|
||||||
|
* When calling `optimize` the package will automatically determine which optimizers
|
||||||
|
* should run for the given image.
|
||||||
|
*/
|
||||||
|
'optimizers' => [
|
||||||
|
|
||||||
|
Jpegoptim::class => [
|
||||||
|
'--strip-all', // this strips out all text information such as comments and EXIF data
|
||||||
|
'--all-progressive', // this will make sure the resulting image is a progressive one
|
||||||
|
'-m85'
|
||||||
|
],
|
||||||
|
|
||||||
|
Pngquant::class => [
|
||||||
|
'--force' // required parameter for this package
|
||||||
|
],
|
||||||
|
|
||||||
|
Optipng::class => [
|
||||||
|
'-i0', // this will result in a non-interlaced, progressive scanned image
|
||||||
|
'-o2', // this set the optimization level to two (multiple IDAT compression trials)
|
||||||
|
'-quiet' // required parameter for this package
|
||||||
|
],
|
||||||
|
|
||||||
|
Svgo::class => [
|
||||||
|
'--disable=cleanupIDs' // disabling because it is know to cause troubles
|
||||||
|
],
|
||||||
|
|
||||||
|
Gifsicle::class => [
|
||||||
|
'-b', // required parameter for this package
|
||||||
|
'-O3' // this produces the slowest but best results
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum time in seconds each optimizer is allowed to run separately.
|
||||||
|
*/
|
||||||
|
'timeout' => 60,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If set to `true` all output of the optimizer binaries will be appended to the default log.
|
||||||
|
* You can also set this to a class that implements `Psr\Log\LoggerInterface`.
|
||||||
|
*/
|
||||||
|
'log_optimizer_activity' => false,
|
||||||
|
];
|
@@ -11,6 +11,7 @@ return [
|
|||||||
'custom' => \Meoran\Images\Templates\Custom::class,
|
'custom' => \Meoran\Images\Templates\Custom::class,
|
||||||
|
|
||||||
),
|
),
|
||||||
|
'middlewareAuth' => false,
|
||||||
'lifetime' => 10,
|
'lifetime' => 10,
|
||||||
'cache' => [
|
'cache' => [
|
||||||
'path' => storage_path('app')
|
'path' => storage_path('app')
|
||||||
|
@@ -20,7 +20,7 @@ class CreateAssociateImages extends Migration
|
|||||||
$table->integer('relation_id');
|
$table->integer('relation_id');
|
||||||
$table->integer('position')->nullable();
|
$table->integer('position')->nullable();
|
||||||
|
|
||||||
$table->foreign('image_id')->references('id')->on('images');
|
$table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
@@ -4,6 +4,7 @@ namespace Meoran\Images\Console\Commands;
|
|||||||
|
|
||||||
use FilesystemIterator;
|
use FilesystemIterator;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Filesystem\Filesystem;
|
||||||
use RecursiveDirectoryIterator;
|
use RecursiveDirectoryIterator;
|
||||||
use RecursiveIteratorIterator;
|
use RecursiveIteratorIterator;
|
||||||
|
|
||||||
@@ -39,8 +40,13 @@ class CacheGarbageCollectorCommand extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$path = storage_path('image.path');
|
$base = config('image.cache.path');
|
||||||
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
|
if (empty($base)) {
|
||||||
|
$this->line("Le cache n'est pas configuré. End...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$fs = new Filesystem();
|
||||||
|
$files = collect($fs->allFiles($base));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \SplFileInfo $b
|
* @var \SplFileInfo $b
|
||||||
|
@@ -44,24 +44,32 @@ class RemoveUselessPicturesCommand extends Command
|
|||||||
private function removeNonExistentPictures()
|
private function removeNonExistentPictures()
|
||||||
{
|
{
|
||||||
$base = config('image.path');
|
$base = config('image.path');
|
||||||
|
if (empty($base)) {
|
||||||
$it = new FilesystemIterator($base);
|
throw new \Exception("Config image.path must be defined");
|
||||||
|
}
|
||||||
|
|
||||||
$fs = new Filesystem();
|
$fs = new Filesystem();
|
||||||
$files = $fs->allFiles($base);
|
$files = collect($fs->allFiles($base));
|
||||||
|
$filenames = $files->filter(function ($el) {
|
||||||
|
$mime = mime_content_type($el->getPathName());
|
||||||
|
if (strpos($mime, 'image/') === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})->map(function ($el) {
|
||||||
|
return $el->getFilename();
|
||||||
|
});
|
||||||
|
|
||||||
var_dump($files);
|
|
||||||
exit;
|
|
||||||
/**
|
/**
|
||||||
* Suppression des images qui n'existent pas physiquement
|
* Suppression des images qui n'existent pas physiquement
|
||||||
*/
|
*/
|
||||||
$linesToDelete = Image::whereNotIn('filename', $files)->get();
|
$linesToDelete = Image::whereNotIn('filename', $filenames)->get();
|
||||||
$delete = 0;
|
$delete = 0;
|
||||||
foreach ($linesToDelete as $lineToDelete) {
|
foreach ($linesToDelete as $lineToDelete) {
|
||||||
$lineToDelete->delete();
|
$lineToDelete->delete();
|
||||||
$delete++;
|
$delete++;
|
||||||
}
|
}
|
||||||
$this->info("Images supprimées en base : ".$delete);
|
$this->info("Images supprimées en base : " . $delete);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suppression des images qui ne sont pas en base
|
* Suppression des images qui ne sont pas en base
|
||||||
@@ -76,32 +84,8 @@ class RemoveUselessPicturesCommand extends Command
|
|||||||
$delete++;
|
$delete++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->info("Images supprimées physiquement : ".$delete);
|
$this->info("Images supprimées physiquement : " . $delete);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Suppression des images qui existent physiquement et en base mais qui ne sont pas utilisées
|
|
||||||
*/
|
|
||||||
private function removeNotUsedPictures()
|
|
||||||
{
|
|
||||||
$base = config('picturesPath');
|
|
||||||
|
|
||||||
$pictures = Image::whereDoesntHave('section', function ($query) {
|
|
||||||
$query->withTrashed();
|
|
||||||
})->whereDoesntHave('news', function ($query) {
|
|
||||||
$query->withTrashed();
|
|
||||||
})->whereDoesntHave('pages', function ($query) {
|
|
||||||
$query->withTrashed();
|
|
||||||
})->get();
|
|
||||||
$delete = 0;
|
|
||||||
foreach ($pictures as $picture) {
|
|
||||||
if (is_file($base . $picture->filename)) {
|
|
||||||
unlink($base . $picture->filename);
|
|
||||||
}
|
|
||||||
$picture->delete();
|
|
||||||
$delete++;
|
|
||||||
}
|
|
||||||
$this->info("Images supprimées car non utilisées : ".$delete);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,6 @@ use Intervention\Image\Exception\NotSupportedException;
|
|||||||
use Laravel\Lumen\Routing\Controller as BaseController;
|
use Laravel\Lumen\Routing\Controller as BaseController;
|
||||||
use Meoran\Images\Model\Image;
|
use Meoran\Images\Model\Image;
|
||||||
use Meoran\Images\Templates\Custom;
|
use Meoran\Images\Templates\Custom;
|
||||||
use ReflectionFunction;
|
|
||||||
use ReflectionMethod;
|
|
||||||
|
|
||||||
class ImagesController extends BaseController
|
class ImagesController extends BaseController
|
||||||
{
|
{
|
||||||
@@ -20,7 +18,7 @@ class ImagesController extends BaseController
|
|||||||
{
|
{
|
||||||
$template = app('request')->input('template');
|
$template = app('request')->input('template');
|
||||||
if ($template === null) {
|
if ($template === null) {
|
||||||
$template = 'original';
|
$template = 'large';
|
||||||
}
|
}
|
||||||
switch (strtolower($template)) {
|
switch (strtolower($template)) {
|
||||||
case 'original':
|
case 'original':
|
||||||
@@ -38,12 +36,24 @@ class ImagesController extends BaseController
|
|||||||
'image' => 'required|file|image'
|
'image' => 'required|file|image'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$image = new Image(['content' => $request->file('image')]);
|
$content = $request->file('image');
|
||||||
|
if (empty($content)) {
|
||||||
|
$content = $request->input('image');
|
||||||
|
}
|
||||||
|
$image = new Image(['content' => $content]);
|
||||||
$image->save();
|
$image->save();
|
||||||
|
|
||||||
return response()->json($image);
|
return response()->json($image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function delete($id)
|
||||||
|
{
|
||||||
|
$image = Image::findOrFail($id);
|
||||||
|
$image->delete();
|
||||||
|
|
||||||
|
return response()->json("Delete " . $id . " succesfully");
|
||||||
|
}
|
||||||
|
|
||||||
private function getOriginal($filename)
|
private function getOriginal($filename)
|
||||||
{
|
{
|
||||||
$path = $this->getImagePathOrAbort($filename);
|
$path = $this->getImagePathOrAbort($filename);
|
||||||
@@ -134,11 +144,13 @@ class ImagesController extends BaseController
|
|||||||
{
|
{
|
||||||
// define mime type
|
// define mime type
|
||||||
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
|
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
|
||||||
|
$timeInSecondsToExpire = config('image.lifetime') * 60;
|
||||||
|
|
||||||
// return http response
|
// return http response
|
||||||
return response($content, 200, array(
|
return response($content, 200, array(
|
||||||
'Content-Type' => $mime,
|
'Content-Type' => $mime,
|
||||||
'Cache-Control' => 'max-age=' . (config('image.lifetime') * 60) . ', public',
|
'Cache-Control' => 'max-age=' . $timeInSecondsToExpire . ', public',
|
||||||
|
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', time() + $timeInSecondsToExpire),
|
||||||
'Etag' => md5($content)
|
'Etag' => md5($content)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,6 @@ namespace Meoran\Images\Model;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
use Illuminate\Database\Query\Builder;
|
|
||||||
use Intervention\Image\Exception\NotReadableException;
|
use Intervention\Image\Exception\NotReadableException;
|
||||||
use Intervention\Image\Image as InterventionImage;
|
use Intervention\Image\Image as InterventionImage;
|
||||||
use Meoran\Images\Exception\InvalidContent;
|
use Meoran\Images\Exception\InvalidContent;
|
||||||
@@ -26,11 +25,10 @@ class Image extends Model
|
|||||||
protected $table = 'images';
|
protected $table = 'images';
|
||||||
|
|
||||||
public $fillable = [
|
public $fillable = [
|
||||||
'filename',
|
|
||||||
'content',
|
'content',
|
||||||
'position',
|
'filename',
|
||||||
'created',
|
'created_at',
|
||||||
'updated'
|
'updated_at'
|
||||||
];
|
];
|
||||||
protected $dates = ['created_at', 'updated_at'];
|
protected $dates = ['created_at', 'updated_at'];
|
||||||
protected $appends = ['url'];
|
protected $appends = ['url'];
|
||||||
@@ -81,10 +79,10 @@ class Image extends Model
|
|||||||
static function getAbsolutePath($filename)
|
static function getAbsolutePath($filename)
|
||||||
{
|
{
|
||||||
$basePath = config('image.path');
|
$basePath = config('image.path');
|
||||||
|
if (empty($basePath)) {
|
||||||
$parts = array_slice(str_split(mb_strtolower(str_slug($filename, '')), 2), 0, 2);
|
throw new \Exception('You must defined config image.path');
|
||||||
|
}
|
||||||
|
$parts = array_slice(str_split(md5($filename), 2), 0, 2);
|
||||||
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
|
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
|
||||||
|
|
||||||
return $path;
|
return $path;
|
||||||
@@ -125,25 +123,34 @@ class Image extends Model
|
|||||||
|
|
||||||
public function setContentAttribute($content)
|
public function setContentAttribute($content)
|
||||||
{
|
{
|
||||||
$this->_content = app('image')->make($content);
|
$this->_content = app('image')->make($content)->orientate();
|
||||||
$this->setHash();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHashAttribute()
|
public function getHashAttribute()
|
||||||
{
|
{
|
||||||
if (!array_key_exists('hash', $this->attributes)) {
|
if (empty($this->content)) {
|
||||||
$this->setHash();
|
return null;
|
||||||
}
|
}
|
||||||
return $this->attributes['hash'];
|
if (empty($this->content->getEncoded())) {
|
||||||
|
$this->content->encode();
|
||||||
|
}
|
||||||
|
return sha1($this->content->getEncoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setHash()
|
public function getExtensionAttribute()
|
||||||
{
|
{
|
||||||
if (empty($this->content)) {
|
$mime = $this->content->mime();
|
||||||
$this->attributes['hash'] = null;
|
if (empty($mime)) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
$this->attributes['hash'] = sha1($this->content->getEncoded());
|
return str_replace('image/', '', $mime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMimeAttribute() {
|
||||||
|
if (empty($this->content)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return $this->content->mime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function same(Image $image)
|
public function same(Image $image)
|
||||||
@@ -205,22 +212,20 @@ class Image extends Model
|
|||||||
*/
|
*/
|
||||||
public static function createRelation(Model $class, $relationName)
|
public static function createRelation(Model $class, $relationName)
|
||||||
{
|
{
|
||||||
|
$instance = $class->newRelatedInstance(static::class);
|
||||||
$instance = $class->newRelatedInstance(self::class);
|
$foreignPivotKey = 'relation_id';
|
||||||
|
$relatedPivotKey = 'image_id';
|
||||||
$foreignKey = 'relation_id';
|
|
||||||
$relatedKey = 'image_id';
|
|
||||||
$name = 'relation';
|
|
||||||
$table = 'associate_images';
|
$table = 'associate_images';
|
||||||
$inverse = false;
|
$name = 'relation';
|
||||||
|
|
||||||
$relation = new MorphToMany(
|
$morph = new MorphToMany(
|
||||||
$instance->newQuery(), $class, $name, $table,
|
$instance->newQuery(), $class, $name, $table,
|
||||||
$foreignKey, $relatedKey, $relationName, $inverse
|
$foreignPivotKey, $relatedPivotKey, $class->getKeyName(),
|
||||||
|
$instance->getKeyName(), $relationName, false
|
||||||
);
|
);
|
||||||
$relation->withPivot('position');
|
$morph->withPivot('position');
|
||||||
|
|
||||||
return $relation;
|
return $morph;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toArray()
|
public function toArray()
|
||||||
@@ -230,7 +235,4 @@ class Image extends Model
|
|||||||
return $attributes;
|
return $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeContent(Builder $builder, Image $image) {
|
|
||||||
$builder->where('hash', $image->hash);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,8 @@ class ImagesServiceProvider extends ServiceProvider
|
|||||||
require_once __DIR__ . '/../functions.php';
|
require_once __DIR__ . '/../functions.php';
|
||||||
|
|
||||||
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
|
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
|
||||||
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'images');
|
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');
|
||||||
|
$this->mergeConfigFrom(__DIR__ . '/../../config/image-optimizer.php', 'image-optimizer');
|
||||||
|
|
||||||
|
|
||||||
$this->app->register(ImageServiceProvider::class);
|
$this->app->register(ImageServiceProvider::class);
|
||||||
@@ -34,21 +35,34 @@ class ImagesServiceProvider extends ServiceProvider
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->commands([
|
$this->commands([
|
||||||
// CacheGarbageCollectorCommand::class,
|
CacheGarbageCollectorCommand::class,
|
||||||
// RemoveUselessPicturesCommand::class,
|
RemoveUselessPicturesCommand::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadRoutes()
|
public function loadRoutes()
|
||||||
{
|
{
|
||||||
$this->app->post('images/upload', [
|
$this->app->router->group([
|
||||||
'as' => 'uploadImage', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@upload'
|
'namespace' => '\Meoran\Images\Http\Controllers',
|
||||||
]);
|
], function ($router) {
|
||||||
|
$addAuthMiddleware = config('image.middlewareAuth', false);
|
||||||
|
$authMiddleware = [];
|
||||||
|
if ($addAuthMiddleware) {
|
||||||
|
$authMiddleware = ['middleware' => 'auth'];
|
||||||
|
}
|
||||||
|
$router->post('images/upload', [
|
||||||
|
'as' => 'uploadImage', 'uses' => 'ImagesController@upload'
|
||||||
|
]+$authMiddleware);
|
||||||
|
|
||||||
$this->app->get('images/{filename}', [
|
$router->get('images/{filename}', [
|
||||||
'as' => 'getPicture', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@get'
|
'as' => 'getPicture', 'uses' => 'ImagesController@get'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$router->delete('images/{id}', [
|
||||||
|
'as' => 'deletePicture', 'uses' => 'ImagesController@delete'
|
||||||
|
]+$authMiddleware);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user