Passed
Push — master ( f4d0e8...419ebb )
by Tobias
02:35
created

DocImage::GetImageFromUrl()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
namespace WrkLst\DocxMustache;
4
5
class DocImage
6
{
7
    public function AllowedContentTypeImages()
8
    {
9
        return [
10
            'image/gif'  => '.gif',
11
            'image/jpeg' => '.jpeg',
12
            'image/png'  => '.png',
13
            'image/bmp'  => '.bmp',
14
        ];
15
    }
16
17
    public function GetImageFromUrl($url, $manipulation)
18
    {
19
        $allowed_imgs = $this->AllowedContentTypeImages();
20
21
        if (trim($url))
22
        if ($img_file_handle = @fopen($url.$manipulation, 'rb')) {
23
            $img_data = stream_get_contents($img_file_handle);
24
            fclose($img_file_handle);
25
            $fi = new \finfo(FILEINFO_MIME);
26
27
            $image_mime = strstr($fi->buffer($img_data), ';', true);
28
            //dd($image_mime);
29
            if (isset($allowed_imgs[$image_mime])) {
30
                return [
31
                    'data' => $img_data,
32
                    'mime' => $image_mime,
33
                ];
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    public function ResampleImage($parent, $imgs, $k, $data)
41
    {
42
        \Storage::disk($parent->storageDisk)->put($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'], $data);
43
44
        //rework img to new size and jpg format
45
        $img_rework = \Image::make($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src']));
46
47
        $imgWidth = $img_rework->width();
48
        $imgHeight = $img_rework->height();
49
        $availableWidth = (int) ($imgs[$k]['cx'] / 5187.627118644067797);
50
        $availableHeight = (int) ($imgs[$k]['cy'] / 5187.627118644067797);
0 ignored issues
show
Unused Code introduced by
$availableHeight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
        //height based resize
53
        $h = (($imgHeight / $imgWidth) * $availableWidth);
54
        $w = (($imgWidth / $imgHeight) * $h);
55
56
        //if height based resize has too large width, do width based resize
57
        if ($w>$availableWidth)
58
        {
59
            $w = (($imgWidth / $imgHeight) * $availableWidth);
60
            $h = (($imgHeight / $imgWidth) * $w);
0 ignored issues
show
Unused Code introduced by
$h is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
        }
62
63
        //only resize one value, aspectRatio() takes care of the other
64
        $h = null;
65
66
        $img_rework->resize($w, $h, function ($constraint) {
67
            $constraint->aspectRatio();
68
            $constraint->upsize();
69
        });
70
71
        $new_height = $img_rework->height();
72
        $new_width = $img_rework->width();
73
        $img_rework->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
74
75
        $parent->zipper->folder('word/media')->add($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
76
77
        return [
78
            'height' => $new_height,
79
            'width'  => $new_width,
80
        ];
81
    }
82
}
83