ResizeController::performResize()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 9.6666
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Intervention\Image\Facades\Image as InterventionImageV2;
6
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
0 ignored issues
show
Bug introduced by
The type Intervention\Image\Laravel\Facades\Image was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use UniSharp\LaravelFilemanager\Events\ImageIsResizing;
8
use UniSharp\LaravelFilemanager\Events\ImageWasResized;
9
10
class ResizeController extends LfmController
11
{
12
    /**
13
     * Dipsplay image for resizing.
14
     *
15
     * @return mixed
16
     */
17
    public function getResize()
18
    {
19
        $ratio = 1.0;
20
        $image = request('img');
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        $image = /** @scrutinizer ignore-call */ request('img');
Loading history...
21
22
        if (class_exists(InterventionImageV2::class)) {
23
            $original_image = InterventionImageV2::make($this->lfm->setName($image)->path('absolute'));
0 ignored issues
show
Bug introduced by
The method setName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            $original_image = InterventionImageV2::make($this->lfm->/** @scrutinizer ignore-call */ setName($image)->path('absolute'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...ollers\ResizeController. Since you implemented __get, consider adding a @property annotation.
Loading history...
24
        } else {
25
            $original_image = InterventionImageV3::read($this->lfm->setName($image)->path('absolute'));
26
        }
27
        $original_width = $original_image->width();
28
        $original_height = $original_image->height();
29
30
        $scaled = false;
31
32
        // FIXME size should be configurable
33
        if ($original_width > 600) {
34
            $ratio = 600 / $original_width;
35
            $width = $original_width * $ratio;
36
            $height = $original_height * $ratio;
37
            $scaled = true;
38
        } else {
39
            $width = $original_width;
40
            $height = $original_height;
41
        }
42
43
        if ($height > 400) {
44
            $ratio = 400 / $original_height;
45
            $width = $original_width * $ratio;
46
            $height = $original_height * $ratio;
47
            $scaled = true;
48
        }
49
50
        return view('laravel-filemanager::resize')
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::resize')
Loading history...
51
            ->with('img', $this->lfm->pretty($image))
52
            ->with('height', number_format($height, 0))
53
            ->with('width', $width)
54
            ->with('original_height', $original_height)
55
            ->with('original_width', $original_width)
56
            ->with('scaled', $scaled)
57
            ->with('ratio', $ratio);
58
    }
59
60
    public function performResize($overWrite = true)
61
    {
62
        $image_name = request('img');
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $image_name = /** @scrutinizer ignore-call */ request('img');
Loading history...
63
        $image_path = $this->lfm->setName(request('img'))->path('absolute');
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...ollers\ResizeController. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
        $resize_path = $image_path;
65
66
        if (! $overWrite) {
67
            $fileParts = explode('.', $image_name);
68
            $fileParts[count($fileParts) - 2] = $fileParts[count($fileParts) - 2] . '_resized_' . time();
69
            $resize_path = $this->lfm->setName(implode('.', $fileParts))->path('absolute');
70
        }
71
72
        event(new ImageIsResizing($image_path));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        /** @scrutinizer ignore-call */ 
73
        event(new ImageIsResizing($image_path));
Loading history...
73
74
        if (class_exists(InterventionImageV2::class)) {
75
            InterventionImageV2::make($image_path)
76
                ->resize(request('dataWidth'), request('dataHeight'))
77
                ->save($resize_path);
78
        } else {
79
            InterventionImageV3::read($image_path)
80
                ->resize(request('dataWidth'), request('dataHeight'))
81
                ->save($resize_path);
82
        }
83
        event(new ImageWasResized($image_path));
84
85
        return parent::$success_response;
86
    }
87
88
    public function performResizeNew()
89
    {
90
        $this->performResize(false);
91
    }
92
}
93