繁体   English   中英

如何在Laravel 5.6中保存图像元数据

[英]How to save image metadata in Laravel 5.6

我正在尝试制作图片库。 为此,我将原始图像(现在大约为7000,将来将超过60.000)存储在存储laravel路径中。 接下来,我做了一份工作,将路径和元数据(图像大小,分辨率,mimetype,宽度和高度)存储到db。

问题是它非常非常缓慢。

这是我的控制器:

 public function startJob() {

        // Start doing Jobs
        CreateDirectories::withChain([
            new RecordPaths,
            // new OptimizeImage,
            // new SendNotification,
        ])->dispatch()->delay(now()->addSeconds(3));
        echo 'create directories and stored paths to database!';
    }

在我的控制器中,我做了一些工作。 首先,它将创建一个目录,供我存储拇指。 完成这项工作后,下一个是到数据库的RecordPaths。

这是一个非常慢的问题(图像/ 2秒)。

这是我的工作:

class RecordPaths implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

        public function handle()
        {
            $this->truncate();

            $files = Storage::disk('gallery')->allFiles();
            foreach($files as $file) {
                $thumb = new Thumb;
                $thumb->brand = explode("/", $file, 2)[0];
                $thumb->name = array_slice(explode("/", $file),-1)[0];
                $thumb->path = $file;
                //
                $thumb->size = $this->imageMetadata($file, 'fileSize');
                $thumb->width = $this->imageMetadata($file, 'imageWidth');
                $thumb->height = $this->imageMetadata($file, 'imageHeight');
                $thumb->mime = $this->imageMetadata($file, 'mimeType');
                //
                $thumb->save();
            }
        }

        public function truncate() {
            return Thumb::truncate();
        }

        public function imageMetadata($file, $type) {
            $metaData = [];
            $metaData['mimeType'] = \Image::make(storage_path("app\public\gallery\\") . $file)->exif('MimeType');
            $metaData['fileSize'] = \Image::make(storage_path("app\public\gallery\\") . $file)->exif('FileSize');
            $metaData['imageWidth'] = \Image::make(storage_path("app\public\gallery\\") . $file)->exif('ExifImageWidth');
            $metaData['imageHeight'] = \Image::make(storage_path("app\public\gallery\\") . $file)->exif('ExifImageLength');
            return $metaData[$type];
        }
    }

$files = Storage::disk('gallery')->allFiles(); 在handle方法中返回以下内容: 在此处输入图片说明

和我的数据库经过一些插入: 在此处输入图片说明

有人知道如何加快速度吗?

首先只执行一个\\Image::make而不是4,然后仅使用params调用-> exif方法。 其次,替换$thumb->save(); 与批量插入。 您可以使用库或编写自己的代码(请参阅参考资料 )。 PS它将增加您的执行时间。 PSS您也可以尝试使用laravel块或在几个工作人员之间拆分图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM