added image seeding command

pull/49/head
A1Gard 4 months ago
parent 8adccb8580
commit ff0395f4a4

@ -0,0 +1,117 @@
<?php
namespace App\Console\Commands;
use App\Models\Category;
use App\Models\Group;
use App\Models\Post;
use App\Models\Product;
use Illuminate\Console\Command;
use Spatie\Image\Enums\AlignPosition;
use Spatie\Image\Enums\Fit;
use Spatie\Image\Enums\Unit;
use Spatie\Image\Image;
class SeedingImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'seeding:image {model} {directory}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Seeding image model';
/**
* Execute the console command.
*/
public function handle()
{
if (!\File::exists(__DIR__ . '/../../../database/seeders/images/' . $this->argument('directory'))) {
$this->error('Directory not found');
}
$images = \File::files(__DIR__ . '/../../../database/seeders/images/' . $this->argument('directory'));
switch ($this->argument('model')) {
case 'Product':
foreach (Product::all() as $item) {
$this->info('Product: ' . $item->id . ' adding image...');
shuffle($images);
if ($item->media()->count() == 0){
$name = $images[0]->getFilename();
$tempName = explode('.', $name);
$item->name = ucfirst(str_replace('-', ' ', $tempName[0])) . ' model ' . $item->id;
}
$item->addMedia($images[0]->getRealPath())
->preservingOriginal() //middle method
->toMediaCollection(); //finishing method
$item->save();
}
break;
case 'Category':
foreach (Category::all() as $item) {
$this->info('Category: ' . $item->name . ' adding image...');
shuffle($images);
if (!\File::exists(storage_path().'/app/public/categories/')){
mkdir(storage_path().'/app/public/categories/', 0755, true);
}
\File::copy($images[0]->getRealPath(),storage_path().'/app/public/categories/' . $images[0]->getFilename());
$item->image = $images[0]->getFilename();
$i = Image::load($images[0]->getRealPath())
->optimize()
->format('webp');
$i->save(storage_path() . '/app/public/categories/optimized-'. $item->image);
shuffle($images);
\File::copy($images[0]->getRealPath(),storage_path().'/app/public/categories/' . $images[0]->getFilename());
$item->bg = $images[0]->getFilename();
$i = Image::load($images[0]->getRealPath())
->optimize()
->format('webp');
$i->save(storage_path() . '/app/public/categories/optimized-'. $item->bg);
$item->save();
}
break;
case 'Group':
foreach (Group::all() as $item) {
$this->info('Group: ' . $item->name . ' adding image...');
shuffle($images);
if (!\File::exists(storage_path().'/app/public/groups/')){
mkdir(storage_path().'/app/public/groups/', 0755, true);
}
\File::copy($images[0]->getRealPath(),storage_path().'/app/public/groups/' . $images[0]->getFilename());
$item->image = $images[0]->getFilename();
$i = Image::load($images[0]->getRealPath())
->optimize()
->format('webp');
$i->save(storage_path() . '/app/public/groups/optimized-'. $item->image);
shuffle($images);
\File::copy($images[0]->getRealPath(),storage_path().'/app/public/groups/' . $images[0]->getFilename());
$item->bg = $images[0]->getFilename();
$i = Image::load($images[0]->getRealPath())
->optimize()
->format('webp');
$i->save(storage_path() . '/app/public/groups/optimized-'. $item->bg);
$item->save();
}
break;
case 'Post':
foreach (Post::all() as $item) {
$this->info('post: ' . $item->id . ' adding image...');
shuffle($images);
$name = $images[0]->getFilename();
$item->addMedia($images[0]->getRealPath())
->preservingOriginal() //middle method
->toMediaCollection(); //finishing method
$item->save();
}
break;
default:
$this->error('Model not valid');
}
}
}

@ -8,6 +8,7 @@ use App\Models\Customer;
use App\Models\Gallery;
use App\Models\Group;
use App\Models\Post;
use App\Models\Product;
use App\Models\User;
use Illuminate\Http\Request;
use Spatie\Tags\Tag;
@ -55,16 +56,27 @@ class ClientController extends Controller
$area = 'posts-list';
$title = __("Posts list");
$subtitle = '';
$posts = Post::where('status', 1)->orderByDesc('id')->paginate($this->paginate);
$posts = Post::where('status', 1)
->orderByDesc('id')->paginate($this->paginate);
return view('client.default-list', compact('area', 'posts', 'title', 'subtitle'));
}
public function products()
{
$area = 'products-list';
$title = __("Products list");
$subtitle = '';
$products = Product::where('status', 1)
->orderByDesc('id')->paginate($this->paginate);
return view('client.default-list', compact('area', 'products', 'title', 'subtitle'));
}
public function galleries()
{
$area = 'galleries-list';
$title = __("Galleries list");
$subtitle = '';
$galleries = Gallery::where('status', 1)->orderByDesc('id')->paginate($this->paginate);
$galleries = Gallery::where('status', 1)
->orderByDesc('id')->paginate($this->paginate);
return view('client.default-list', compact('area', 'galleries', 'title', 'subtitle'));
}

@ -39,6 +39,11 @@ class Post extends Model implements HasMedia
public function registerMediaConversions(?Media $media = null): void
{
$optimize = getSetting('optimize');
if ($optimize == false){
$optimize = 'webp';
}
$t = explode('x', config('app.media.post_thumb'));
$t = imageSizeConvertValidate('post_thumb');
@ -50,7 +55,7 @@ class Post extends Model implements HasMedia
->optimize()
->sharpen(10)
->nonQueued()
->format(getSetting('optimize'));
->format($optimize);
if (getSetting('watermark')){
$mc->watermark(public_path('upload/images/logo.png'),

@ -56,6 +56,11 @@ class Product extends Model implements HasMedia
public function registerMediaConversions(?Media $media = null): void
{
$optimize = getSetting('optimize');
if ($optimize == false){
$optimize = 'webp';
}
$ti = imageSizeConvertValidate('product_image');
$t = imageSizeConvertValidate('product_thumb');
@ -66,7 +71,7 @@ class Product extends Model implements HasMedia
->optimize()
->sharpen(10)
->nonQueued()
->format(getSetting('optimize'));
->format($optimize);
$mc2 = $this->addMediaConversion('product-image')
->width($ti[0])
@ -75,7 +80,7 @@ class Product extends Model implements HasMedia
->optimize()
->sharpen(10)
->nonQueued()
->format(getSetting('optimize'));
->format($optimize);
if (getSetting('watermark')) {
$mc->watermark(public_path('upload/images/logo.png'),

@ -118,7 +118,7 @@ class AreaSeeder extends Seeder
'name' => 'products-list',
'valid_segments' => json_encode(
["top", "header", "footer", "menu",
"parallax", "other", "products_page", "ads"]
"parallax", "other", "products_page", "ads","products"]
),
'max' => 6,
'preview' => 'client.products',

@ -0,0 +1,21 @@
<?php
namespace Resources\Views\Segments;
use App\Models\Part;
class ProductGrid
{
public static function onAdd(Part $part = null)
{
}
public static function onRemove(Part $part = null)
{
}
public static function onMount(Part $part = null)
{
return $part;
}
}

@ -360,6 +360,7 @@ Route::name('client.')->group(function (){
Route::get('/', [\App\Http\Controllers\ClientController::class,'welcome'])->name('welcome');
Route::get('/posts', [\App\Http\Controllers\ClientController::class,'posts'])->name('posts');
Route::get('/galleries', [\App\Http\Controllers\ClientController::class,'galleries'])->name('galleries');
Route::get('/products', [\App\Http\Controllers\ClientController::class,'products'])->name('products');
Route::get('/tag/{post}', [\App\Http\Controllers\ClientController::class,'tag'])->name('tag'); // wip
Route::get('/group/{group}', [\App\Http\Controllers\ClientController::class,'group'])->name('group');
Route::get('/gallery/{gallery}', [\App\Http\Controllers\ClientController::class,'gallery'])->name('gallery');

Loading…
Cancel
Save