Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Cassandre Cantet | d36afa5e7d |
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Meoran\Images\" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -1,6 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
</project>
|
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/images.iml" filepath="$PROJECT_DIR$/.idea/images.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
|
@ -1,4 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="7" />
|
||||
</project>
|
|
@ -1,6 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -1,40 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="8409b98c-02e3-4b35-86a6-bf73d1bc2a05" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/composer.json" beforeDir="false" afterPath="$PROJECT_DIR$/composer.json" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="ComposerSettings" doNotAsk="true" synchronizationState="SYNCHRONIZE">
|
||||
<pharConfigPath>$PROJECT_DIR$/composer.json</pharConfigPath>
|
||||
<execution />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectId" id="1bGCgaByldIqNY40ITgHAqcYemj" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="autoscrollFromSource" value="true" />
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||
<property name="settings.editor.selected.configurable" value="configurable.group.language" />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
<option name="version" value="2" />
|
||||
</component>
|
||||
<component name="WindowStateProjectService">
|
||||
<state x="623" y="267" width="424" height="479" key="FileChooserDialogImpl" timestamp="1588249996502">
|
||||
<screen x="0" y="0" width="1680" height="1020" />
|
||||
</state>
|
||||
<state x="623" y="267" width="424" height="479" key="FileChooserDialogImpl/0.0.1680.1020/1680.0.1680.1050@0.0.1680.1020" timestamp="1588249996502" />
|
||||
</component>
|
||||
</project>
|
|
@ -11,10 +11,11 @@
|
|||
],
|
||||
"require": {
|
||||
"php": ">=7.0.0",
|
||||
"laravel/framework": "^8.0",
|
||||
"intervention/image": "^2.5",
|
||||
"intervention/imagecache": "^2.5",
|
||||
"spatie/laravel-image-optimizer": "^1.6"
|
||||
"laravel/lumen-framework": "5.4.*",
|
||||
"symfony/process" : "3.*",
|
||||
"intervention/image": "^2.4",
|
||||
"intervention/imagecache": "^2.3",
|
||||
"spatie/laravel-image-optimizer": "^1.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
<?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,7 +11,6 @@ return [
|
|||
'custom' => \Meoran\Images\Templates\Custom::class,
|
||||
|
||||
),
|
||||
'middlewareAuth' => false,
|
||||
'lifetime' => 10,
|
||||
'cache' => [
|
||||
'path' => storage_path('app')
|
||||
|
|
|
@ -11,10 +11,10 @@ class CreateImagesTable extends Migration
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void
|
||||
public function up()
|
||||
{
|
||||
Schema::create('images', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('id', true);
|
||||
$table->string('filename');
|
||||
$table->dateTime('created_at');
|
||||
$table->dateTime('updated_at');
|
||||
|
@ -27,7 +27,7 @@ class CreateImagesTable extends Migration
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down(): void
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('images');
|
||||
}
|
|
@ -11,14 +11,17 @@ class CreateAssociateImages extends Migration
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void
|
||||
public function up()
|
||||
{
|
||||
Schema::create('associate_images', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('image_id')->constrained()->onDelete('cascade');
|
||||
$table->integer('id', true);
|
||||
$table->integer('image_id');
|
||||
$table->string('relation_type');
|
||||
$table->unsignedBigInteger('relation_id');
|
||||
$table->integer('relation_id');
|
||||
$table->integer('position')->nullable();
|
||||
|
||||
$table->foreign('image_id')->references('id')->on('images');
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -27,8 +30,9 @@ class CreateAssociateImages extends Migration
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down(): void
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('associate_images');
|
||||
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ namespace Meoran\Images\Console\Commands;
|
|||
|
||||
use FilesystemIterator;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
|
@ -40,13 +39,8 @@ class CacheGarbageCollectorCommand extends Command
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
$base = config('image.cache.path');
|
||||
if (empty($base)) {
|
||||
$this->line("Le cache n'est pas configuré. End...");
|
||||
return;
|
||||
}
|
||||
$fs = new Filesystem();
|
||||
$files = collect($fs->allFiles($base));
|
||||
$path = storage_path('image.path');
|
||||
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
|
||||
|
||||
/**
|
||||
* @var \SplFileInfo $b
|
||||
|
|
|
@ -44,32 +44,24 @@ class RemoveUselessPicturesCommand extends Command
|
|||
private function removeNonExistentPictures()
|
||||
{
|
||||
$base = config('image.path');
|
||||
if (empty($base)) {
|
||||
throw new \Exception("Config image.path must be defined");
|
||||
}
|
||||
|
||||
$it = new FilesystemIterator($base);
|
||||
|
||||
$fs = new Filesystem();
|
||||
$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();
|
||||
});
|
||||
$files = $fs->allFiles($base);
|
||||
|
||||
var_dump($files);
|
||||
exit;
|
||||
/**
|
||||
* Suppression des images qui n'existent pas physiquement
|
||||
*/
|
||||
$linesToDelete = Image::whereNotIn('filename', $filenames)->get();
|
||||
$linesToDelete = Image::whereNotIn('filename', $files)->get();
|
||||
$delete = 0;
|
||||
foreach ($linesToDelete as $lineToDelete) {
|
||||
$lineToDelete->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
|
||||
|
@ -84,8 +76,32 @@ class RemoveUselessPicturesCommand extends Command
|
|||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,34 +2,25 @@
|
|||
|
||||
namespace Meoran\Images\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Intervention\Image\Constraint;
|
||||
use Intervention\Image\Exception\ImageException;
|
||||
use Intervention\Image\Exception\NotSupportedException;
|
||||
use Laravel\Lumen\Routing\Controller as BaseController;
|
||||
use Meoran\Images\Model\Image;
|
||||
use Meoran\Images\Templates\Custom;
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Class ImagesController
|
||||
* @package Meoran\Images\Http\Controllers
|
||||
*/
|
||||
class ImagesController extends Controller
|
||||
class ImagesController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
* @return Application|ResponseFactory|Response
|
||||
*/
|
||||
public function get($filename)
|
||||
{
|
||||
$template = app('request')->input('template');
|
||||
if ($template === null) {
|
||||
$template = 'large';
|
||||
$template = 'original';
|
||||
}
|
||||
switch (strtolower($template)) {
|
||||
case 'original':
|
||||
|
@ -41,35 +32,18 @@ class ImagesController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function upload(Request $request): JsonResponse
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'image' => 'required|file|image'
|
||||
]);
|
||||
|
||||
$content = $request->file('image');
|
||||
if (empty($content)) {
|
||||
$content = $request->input('image');
|
||||
}
|
||||
$image = new Image(['content' => $content]);
|
||||
$image = new Image(['content' => $request->file('image')]);
|
||||
$image->save();
|
||||
|
||||
return response()->json($image);
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$image = Image::findOrFail($id);
|
||||
$image->delete();
|
||||
|
||||
return response()->json("Delete " . $id . " succesfully");
|
||||
}
|
||||
|
||||
private function getOriginal($filename)
|
||||
{
|
||||
$path = $this->getImagePathOrAbort($filename);
|
||||
|
@ -95,12 +69,6 @@ class ImagesController extends Controller
|
|||
abort(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @param $filename
|
||||
* @return Application|ResponseFactory|Response
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function getImage($template, $filename)
|
||||
{
|
||||
$template = $this->getTemplate($template);
|
||||
|
@ -119,13 +87,6 @@ class ImagesController extends Controller
|
|||
return $this->buildResponse($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @param $path
|
||||
* @param null $image
|
||||
* @return mixed
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
private function applyTemplate($template, $path, $image = null)
|
||||
{
|
||||
if (empty($image)) {
|
||||
|
@ -135,12 +96,12 @@ class ImagesController extends Controller
|
|||
if ($template instanceof Closure) {
|
||||
// build from closure callback template
|
||||
return $template($image->make($path));
|
||||
}
|
||||
|
||||
// build from filter template
|
||||
$res = $image->make($path)->filter($template);
|
||||
if ($res === null) {
|
||||
abort(404);
|
||||
} else {
|
||||
// build from filter template
|
||||
$res = $image->make($path)->filter($template);
|
||||
if ($res === null) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,30 +130,20 @@ class ImagesController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Application|ResponseFactory|Response
|
||||
*/
|
||||
private function buildResponse($content)
|
||||
{
|
||||
// define mime type
|
||||
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
|
||||
$timeInSecondsToExpire = config('image.lifetime') * 60;
|
||||
|
||||
// return http response
|
||||
return response($content, 200, array(
|
||||
'Content-Type' => $mime,
|
||||
'Cache-Control' => 'max-age=' . $timeInSecondsToExpire . ', public',
|
||||
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', time() + $timeInSecondsToExpire),
|
||||
'Cache-Control' => 'max-age=' . (config('image.lifetime') * 60) . ', public',
|
||||
'Etag' => md5($content)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateName
|
||||
* @return Custom
|
||||
*/
|
||||
private function getCustomTemplate($templateName): Custom
|
||||
private function getCustomTemplate($templateName)
|
||||
{
|
||||
$custom = new Custom();
|
||||
$custom->actions = [$templateName];
|
||||
|
|
|
@ -2,78 +2,50 @@
|
|||
|
||||
namespace Meoran\Images\Model;
|
||||
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Intervention\Image\Exception\NotReadableException;
|
||||
use Intervention\Image\Image as InterventionImage;
|
||||
use Meoran\Images\Exception\InvalidContent;
|
||||
use RuntimeException;
|
||||
use Spatie\ImageOptimizer\OptimizerChain;
|
||||
|
||||
/**
|
||||
* Class Image
|
||||
* @property InterventionImage $content
|
||||
* @property string filename
|
||||
* @property string hash
|
||||
* @package App\Model
|
||||
*/
|
||||
class Image extends Model
|
||||
{
|
||||
|
||||
/** @var string[] */
|
||||
public $fillable = [
|
||||
'content',
|
||||
'filename',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
/** @var InterventionImage */
|
||||
/**
|
||||
* @var InterventionImage $_content
|
||||
*/
|
||||
protected $_content;
|
||||
/** @var string */
|
||||
|
||||
protected $table = 'images';
|
||||
/** @var string[] */
|
||||
|
||||
public $fillable = [
|
||||
'filename',
|
||||
'content',
|
||||
'position',
|
||||
'created',
|
||||
'updated'
|
||||
];
|
||||
protected $dates = ['created_at', 'updated_at'];
|
||||
/** @var string[] */
|
||||
protected $appends = ['url'];
|
||||
protected $hidden = [];
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return string
|
||||
*/
|
||||
public static function sanitizeFilename(string $filename): string
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
return Str::slug($filename, '-');
|
||||
$route = config('image.route');
|
||||
if (empty($route)) {
|
||||
return null;
|
||||
}
|
||||
return app('url')->to('/' . $route . '/' . $this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model $class
|
||||
* @param $relationName
|
||||
* @return MorphToMany
|
||||
*/
|
||||
public static function createRelation(Model $class, $relationName): MorphToMany
|
||||
{
|
||||
$instance = $class->newRelatedInstance(static::class);
|
||||
$foreignPivotKey = 'relation_id';
|
||||
$relatedPivotKey = 'image_id';
|
||||
$table = 'associate_images';
|
||||
$name = 'relation';
|
||||
|
||||
$morph = new MorphToMany(
|
||||
$instance->newQuery(), $class, $name, $table,
|
||||
$foreignPivotKey, $relatedPivotKey, $class->getKeyName(),
|
||||
$instance->getKeyName(), $relationName, false
|
||||
);
|
||||
$morph->withPivot('position');
|
||||
|
||||
return $morph;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected static function boot(): void
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
|
@ -97,56 +69,7 @@ class Image extends Model
|
|||
|
||||
}
|
||||
|
||||
protected function savePicture()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return true;
|
||||
}
|
||||
$this->generateFilename();
|
||||
return $this->saveContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $force
|
||||
*/
|
||||
public function generateFilename($force = false): void
|
||||
{
|
||||
if ($this->filename && !$force) {
|
||||
return;
|
||||
}
|
||||
$this->filename = self::generateRandomFilename();
|
||||
}
|
||||
|
||||
/**+
|
||||
* @return string
|
||||
*/
|
||||
public static function generateRandomFilename(): string
|
||||
{
|
||||
return mb_strtolower(Str::random(60));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InterventionImage
|
||||
*/
|
||||
protected function saveContent(): InterventionImage
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
throw new \InvalidArgumentException("Content is Empty");
|
||||
}
|
||||
$path = $this->getPath();
|
||||
$dir = dirname($path);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$res = $this->content->save($path);
|
||||
app(OptimizerChain::class)->optimize($path);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPath(): ?string
|
||||
public function getPath()
|
||||
{
|
||||
if (empty($this->filename)) {
|
||||
return null;
|
||||
|
@ -155,48 +78,29 @@ class Image extends Model
|
|||
return self::getAbsolutePath($this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
* @return string
|
||||
*/
|
||||
public static function getAbsolutePath($filename): string
|
||||
static function getAbsolutePath($filename)
|
||||
{
|
||||
$basePath = config('image.path');
|
||||
if (empty($basePath)) {
|
||||
throw new RuntimeException('You must defined config image.path');
|
||||
}
|
||||
$parts = array_slice(str_split(md5($filename), 2), 0, 2);
|
||||
return $basePath . '/' . implode('/', $parts) . '/' . $filename;
|
||||
|
||||
$parts = array_slice(str_split(mb_strtolower(str_slug($filename, '')), 2), 0, 2);
|
||||
|
||||
|
||||
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function deletePicture(): bool
|
||||
static function generateRandomFilename()
|
||||
{
|
||||
$path = $this->getPath();
|
||||
if (is_file($path)) {
|
||||
return unlink($path);
|
||||
}
|
||||
return true;
|
||||
return mb_strtolower(str_random(60));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttribute(): ?string
|
||||
static function sanitizeFilename($filename)
|
||||
{
|
||||
$route = config('image.route');
|
||||
if (empty($route)) {
|
||||
return null;
|
||||
}
|
||||
return app('url')->to('/' . $route . '/' . $this->filename);
|
||||
return str_slug($filename, '-');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setFilenameAttribute(string $value): void
|
||||
public function setFilenameAttribute($value)
|
||||
{
|
||||
$pattern = '/[^a-z_\-\.0-9]/i';
|
||||
$patternLetter = '/[a-z0-9]+/i';
|
||||
|
@ -206,76 +110,50 @@ class Image extends Model
|
|||
$this->attributes['filename'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function fileExist(): bool
|
||||
public function fileExist()
|
||||
{
|
||||
return is_file($this->getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function setContentAttribute($content): void
|
||||
public function generateFilename($force = false)
|
||||
{
|
||||
$this->_content = app('image')->make($content)->orientate();
|
||||
if ($this->filename && !$force) {
|
||||
return;
|
||||
}
|
||||
$this->filename = self::generateRandomFilename();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHashAttribute(): ?string
|
||||
public function setContentAttribute($content)
|
||||
{
|
||||
$this->_content = app('image')->make($content);
|
||||
$this->setHash();
|
||||
}
|
||||
|
||||
public function getHashAttribute()
|
||||
{
|
||||
if (!array_key_exists('hash', $this->attributes)) {
|
||||
$this->setHash();
|
||||
}
|
||||
return $this->attributes['hash'];
|
||||
}
|
||||
|
||||
protected function setHash()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return null;
|
||||
$this->attributes['hash'] = null;
|
||||
return;
|
||||
}
|
||||
if (empty($this->content->getEncoded())) {
|
||||
$this->content->encode();
|
||||
}
|
||||
return sha1($this->content->getEncoded());
|
||||
$this->attributes['hash'] = sha1($this->content->getEncoded());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getExtensionAttribute(): ?string
|
||||
{
|
||||
$mime = $this->content->mime();
|
||||
if (empty($mime)) {
|
||||
return null;
|
||||
}
|
||||
return str_replace('image/', '', $mime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMimeAttribute(): ?string
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return null;
|
||||
}
|
||||
return $this->content->mime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Image $image
|
||||
* @return bool
|
||||
*/
|
||||
public function same(Image $image): bool
|
||||
public function same(Image $image)
|
||||
{
|
||||
$hash1 = $this->hash;
|
||||
$hash2 = $image->hash;
|
||||
return !empty($hash1) && !empty($hash2) && $hash1 === $hash2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InterventionImage|null
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function getContentAttribute(): ?InterventionImage
|
||||
public function getContentAttribute($value)
|
||||
{
|
||||
if (empty($this->_content)) {
|
||||
try {
|
||||
|
@ -287,4 +165,72 @@ class Image extends Model
|
|||
return $this->_content;
|
||||
}
|
||||
|
||||
protected function savePicture()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return true;
|
||||
}
|
||||
$this->generateFilename();
|
||||
return $this->saveContent();
|
||||
}
|
||||
|
||||
protected function saveContent()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
throw new \InvalidArgumentException("Content is Empty");
|
||||
}
|
||||
$path = $this->getPath();
|
||||
$dir = dirname($path);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0775, true);
|
||||
}
|
||||
$res = $this->content->save($path);
|
||||
app(OptimizerChain::class)->optimize($path);
|
||||
return $res;
|
||||
}
|
||||
|
||||
protected function deletePicture()
|
||||
{
|
||||
$path = $this->getPath();
|
||||
if (is_file($path)) {
|
||||
return unlink($path);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model $class
|
||||
* @param $relationName
|
||||
* @return MorphToMany
|
||||
*/
|
||||
public static function createRelation(Model $class, $relationName)
|
||||
{
|
||||
|
||||
$instance = $class->newRelatedInstance(self::class);
|
||||
|
||||
$foreignKey = 'relation_id';
|
||||
$relatedKey = 'image_id';
|
||||
$name = 'relation';
|
||||
$table = 'associate_images';
|
||||
$inverse = false;
|
||||
|
||||
$relation = new MorphToMany(
|
||||
$instance->newQuery(), $class, $name, $table,
|
||||
$foreignKey, $relatedKey, $relationName, $inverse
|
||||
);
|
||||
$relation->withPivot('position');
|
||||
|
||||
return $relation;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$attributes = parent::toArray();
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public function scopeContent(Builder $builder, Image $image) {
|
||||
$builder->where('hash', $image->hash);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ class ImagesServiceProvider extends ServiceProvider
|
|||
|
||||
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
|
||||
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');
|
||||
$this->mergeConfigFrom(__DIR__ . '/../../config/image-optimizer.php', 'image-optimizer');
|
||||
|
||||
|
||||
$this->app->register(ImageServiceProvider::class);
|
||||
|
@ -35,34 +34,21 @@ class ImagesServiceProvider extends ServiceProvider
|
|||
]);
|
||||
|
||||
$this->commands([
|
||||
CacheGarbageCollectorCommand::class,
|
||||
RemoveUselessPicturesCommand::class,
|
||||
// CacheGarbageCollectorCommand::class,
|
||||
// RemoveUselessPicturesCommand::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function loadRoutes()
|
||||
{
|
||||
$this->app->router->group([
|
||||
'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->post('images/upload', [
|
||||
'as' => 'uploadImage', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@upload'
|
||||
]);
|
||||
|
||||
$router->get('images/{filename}', [
|
||||
'as' => 'getPicture', 'uses' => 'ImagesController@get'
|
||||
]);
|
||||
|
||||
$router->delete('images/{id}', [
|
||||
'as' => 'deletePicture', 'uses' => 'ImagesController@delete'
|
||||
]+$authMiddleware);
|
||||
});
|
||||
$this->app->get('images/{filename}', [
|
||||
'as' => 'getPicture', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@get'
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,19 +7,11 @@ use Intervention\Image\Exception\NotSupportedException;
|
|||
use Intervention\Image\Filters\FilterInterface;
|
||||
use Intervention\Image\Image;
|
||||
|
||||
/**
|
||||
* Class Custom
|
||||
* @package Meoran\Images\Templates
|
||||
*/
|
||||
class Custom implements FilterInterface
|
||||
{
|
||||
public $actions;
|
||||
public $actions = null;
|
||||
|
||||
/**
|
||||
* @param Image $image
|
||||
* @return Image|null
|
||||
*/
|
||||
public function applyFilter(Image $image): ?Image
|
||||
public function applyFilter(Image $image)
|
||||
{
|
||||
$actions = $this->actions;
|
||||
if ($actions === null) {
|
||||
|
@ -39,18 +31,16 @@ class Custom implements FilterInterface
|
|||
if (isset($exploded[1])) {
|
||||
$params = explode(',', $exploded[1]);
|
||||
}
|
||||
$params[] = static function (Constraint $constraint) {
|
||||
$params[] = function (Constraint $constraint) {
|
||||
$constraint->upsize();
|
||||
$constraint->aspectRatio();
|
||||
};
|
||||
$params = array_map(static function($el) {
|
||||
if (is_string($el) && $el === 'null') {
|
||||
return null;
|
||||
}
|
||||
$params = array_map(function($el) {
|
||||
if (is_string($el) && $el === 'null') return null;
|
||||
return $el;
|
||||
},$params);
|
||||
try {
|
||||
call_user_func_array([$image, $methodName], $params);
|
||||
call_user_func_array([$image,$methodName], $params);
|
||||
} catch (NotSupportedException $exception) {
|
||||
return null;
|
||||
}
|
||||
|
@ -58,4 +48,4 @@ class Custom implements FilterInterface
|
|||
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,15 +8,11 @@ use Intervention\Image\Image;
|
|||
|
||||
class Large implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* @param Image $image
|
||||
* @return Image
|
||||
*/
|
||||
public function applyFilter(Image $image): Image
|
||||
public function applyFilter(Image $image)
|
||||
{
|
||||
return $image->resize(1920, null, function (Constraint $constraint) {
|
||||
$constraint->upsize();
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,11 +8,11 @@ use Intervention\Image\Image;
|
|||
|
||||
class Medium implements FilterInterface
|
||||
{
|
||||
public function applyFilter(Image $image): Image
|
||||
public function applyFilter(Image $image)
|
||||
{
|
||||
return $image->resize(960, null, function (Constraint $constraint) {
|
||||
$constraint->upsize();
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,11 +8,11 @@ use Intervention\Image\Image;
|
|||
|
||||
class Small implements FilterInterface
|
||||
{
|
||||
public function applyFilter(Image $image): Image
|
||||
public function applyFilter(Image $image)
|
||||
{
|
||||
return $image->resize(480, null, function (Constraint $constraint) {
|
||||
$constraint->upsize();
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,11 +8,11 @@ use Intervention\Image\Image;
|
|||
|
||||
class Thumb implements FilterInterface
|
||||
{
|
||||
public function applyFilter(Image $image): Image
|
||||
public function applyFilter(Image $image)
|
||||
{
|
||||
return $image->resize(120, null, function (Constraint $constraint) {
|
||||
$constraint->upsize();
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue