Изменение размера изображения в Laravel Ошибка вмешательства в изображение в производственном режиме - PullRequest
0 голосов
/ 28 мая 2020

Я загрузил свое приложение Laravel на хост publi c, и теперь я получаю эту ошибку, когда загружаю изображение в свое приложение в рабочем режиме.

Вот ошибка

[2020-05-28 11:55:13] production.ERROR: Image source not readable {"userId":"7ae556c6-4109-4cca-9c90-610763cbbf27","exception":"[object] (Intervention\\Image\\Exception\\NotReadableException(code: 0): Image source not readable at /home/khyberpu/public_html/kpi/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php:346)
[stacktrace]
#0 /home/khyberpu/public_html/kpi/vendor/intervention/image/src/Intervention/Image/AbstractDriver.php(66): Intervention\\Image\\AbstractDecoder->init('/home/khyberpu/...')
#1 /home/khyberpu/public_html/kpi/vendor/intervention/image/src/Intervention/Image/ImageManager.php(54): Intervention\\Image\\AbstractDriver->init('/home/khyberpu/...')
#2 /home/khyberpu/public_html/kpi/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Intervention\\Image\\ImageManager->make('/home/khyberpu/...')
#3 /home/khyberpu/public_html/kpi/app/Services/ImageService.php(24): Illuminate\\Support\\Facades\\Facade::__callStatic('make', Array)
#4 /home/khyberpu/public_html/kpi/app/Traits/UploadAble.php(34): App\\Services\\ImageService->generateThumbnails('products', 'ahGSJDqPbvoc7V2...', Object(Illuminate\\Http\\UploadedFile))
#5 /home/khyberpu/public_html/kpi/app/Repositories/ProductRepository.php(192): App\\Repositories\\ProductRepository->uploadOne(Object(Illuminate\\Http\\UploadedFile), 'products')
#6 /home/khyberpu/public_html/kpi/app/Http/Controllers/Admin/ProductController.php(86): App\\Repositories\\ProductRepository->productImage(Object(App\\Models\\Product), Object(App\\Http\\Requests\\StoreProductFormRequest))
#7 [internal function]: App\\Http\\Controllers\\Admin\\ProductController->store(Object(App\\Http\\Requests\\StoreProductFormRequest))
#8 /home/khyberpu/public_html/kpi/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#9 /home/khyberpu/public_html/kpi/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('store', Array)
#10 /home/khyberpu/public_html/kpi/vendor/laravel/framework/src/Illuminate/Routing/Route.php(225): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Admin\\ProductController), 'store')
#11 ......

Это моя черта UploadAble

<?php

namespace App\Traits;

use App\Services\ImageService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;

/**
 * Trait UploadAble
 * @package App\Traits
 */
trait UploadAble
{
    /**
     * @param UploadedFile $file
     * @param null $folder
     * @param string $disk
     * @param null $filename
     * @return false|string
     */
    public function uploadOne(UploadedFile $file, $folder = null, $disk = 'public', $filename = null)
    {
        $name = !is_null($filename) ? $filename : Str::random(25);

        $image = $file->storeAs(
            $folder,
            $name . "." . $file->getClientOriginalExtension(),
            $disk
        );

        (new ImageService())->generateThumbnails($folder, $name, $file);

        return $image;
    }

    /**
     * @param null $path
     * @param string $disk
     */
    public function deleteOne($path = null, $disk = 'public')
    {
        $nameAndFolder = explode('/', $path);
        $nameAndExtension = explode('.', $nameAndFolder[1]);
        $name = $nameAndExtension[0] . '.webp';
        Storage::disk($disk)->delete($path);
        Storage::disk($disk)->delete($nameAndFolder[0] . '/120/' . $name);
        Storage::disk($disk)->delete($nameAndFolder[0] . '/325/' . $name);
        Storage::disk($disk)->delete($nameAndFolder[0] . '/1024/' . $name);
    }
}

И это моя служба изображений, я просто пытаюсь создать здесь несколько эскизов, и здесь возникает проблема

<?php


namespace App\Services;


use Illuminate\Http\UploadedFile;
use Intervention\Image\Facades\Image;

class ImageService
{
    /**
     * @param $folder
     * @param string $name
     * @param UploadedFile $file
     */
    public function generateThumbnails($folder, string $name, UploadedFile $file): void
    {
        // Create the 120px width image
        if (!file_exists(public_path() . '/storage/' . $folder . '/' . '120')) {
            mkdir(public_path() . '/storage/' . $folder . '/' . '120', 0777, true);
        }

        Image::make(public_path() . '/storage/' . $folder . '/' . $name . '.' . $file->getClientOriginalExtension())
            ->resize(120, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save(public_path() . '/storage/' . $folder . '/120/' . $name . '.webp', 100, 'webp');

        // Create the 325px width image

        if (!file_exists(public_path() . '/storage/' . $folder . '/' . '325')) {
            mkdir(public_path() . '/storage/' . $folder . '/' . '325', 0777, true);
        }

        Image::make(public_path() . '/storage/' . $folder . '/' . $name . '.' . $file->getClientOriginalExtension())
            ->resize(325, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save(public_path() . '/storage/' . $folder . '/325/' . $name . '.webp', 100, 'webp');

        // Create the 1024px width image
        if (!file_exists(public_path() . '/storage/' . $folder . '/' . '1024')) {
            mkdir(public_path() . '/storage/' . $folder . '/' . '1024', 0777, true);
        }

        Image::make(public_path() . '/storage/' . $folder . '/' . $name . '.' . $file->getClientOriginalExtension())
            ->resize(1024, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save(public_path() . '/storage/' . $folder . '/1024/' . $name . '.webp', 90, 'webp');
    }
}

Примечание: он работает в режиме разработки, но не в производственном режиме

...