ProcessesImages::processImage()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 21
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 27
rs 8.9617
1
<?php
2
3
namespace FaithGen\SDK\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use Intervention\Image\ImageManager;
7
use InvalidArgumentException;
8
9
trait ProcessesImages
10
{
11
    use FileTraits;
12
13
    /**
14
     * Creates thumbnails for the given model.
15
     *
16
     * @param ImageManager $imageManager
17
     * @param $model
18
     */
19
    protected function processImage(ImageManager $imageManager, $model)
20
    {
21
        if (! in_array(StorageTrait::class, class_uses($model))) {
22
            throw new InvalidArgumentException('The model you used does not use the Storage trait');
23
        }
24
25
        if ($model->images()->exists()) {
26
            foreach ($model->images as $image) {
27
                $originalFile = $this->getImage($model->filesDir(), $image->name);
28
29
                $thumbNailsDimensions = collect($model->getImageDimensions())
30
                    ->except(0)
31
                    ->toArray();
32
33
                foreach ($thumbNailsDimensions as $thumbNailsDimension) {
34
                    try {
35
                        $thumbNail = $this->getImage($model->filesDir(), $image->name, $thumbNailsDimension);
36
                        $imageManager
37
                            ->make($originalFile)
38
                            ->fit($thumbNailsDimension, $thumbNailsDimension,
39
                                function ($constraint) {
40
                                    $constraint->upsize();
41
                                    $constraint->aspectRatio();
42
                                }, 'center')
43
                            ->save($thumbNail);
44
                    } catch (\Exception $e) {
45
                        Log::error($e->getMessage());
46
                    }
47
                }
48
            }
49
        }
50
    }
51
}
52