Passed
Push — master ( 269417...c6f060 )
by Tobias
02:27
created

DocImage::ResampleImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 49
rs 9.2258
cc 2
eloc 25
nc 2
nop 4
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
38
        return false;
39
    }
40
41
    public function ResampleImage($parent, $imgs, $k, $data)
42
    {
43
        \Storage::disk($parent->storageDisk)->put($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'], $data);
44
45
        //rework img to new size and jpg format
46
        $img_rework = \Image::make($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src']));
47
48
        $imgWidth = $img_rework->width();
49
        $imgHeight = $img_rework->height();
50
51
        //check https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
52
        // for EMUs calculation
53
        /*
54
        295px @72 dpi = 1530350 EMUs = Multiplier for 72dpi pixels 5187.627118644067797
55
        413px @72 dpi = 2142490 EMUs = Multiplier for 72dpi pixels 5187.627118644067797
56
        */
57
        $availableWidth = (int) ($imgs[$k]['cx'] / 5187.627118644067797);
58
        $availableHeight = (int) ($imgs[$k]['cy'] / 5187.627118644067797);
59
60
        //height based resize
61
        $h = (($imgHeight / $imgWidth) * $availableWidth);
62
        $w = (($imgWidth / $imgHeight) * $h);
63
64
        //if height based resize has too large width, do width based resize
65
        if ($h > $availableHeight) {
66
            $w = (($imgWidth / $imgHeight) * $availableHeight);
67
            $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...
68
        }
69
70
        $h = null;
71
72
        $img_rework->resize($w, $h, function ($constraint) {
73
            $constraint->aspectRatio();
74
            $constraint->upsize();
75
        });
76
77
        $new_height = $img_rework->height();
78
        $new_width = $img_rework->width();
79
        $img_rework->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
80
81
        $parent->zipper->folder('word/media')->add($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
82
83
        return [
84
            'height' => $new_height,
85
            'width'  => $new_width,
86
            'height_emus' => (int)($new_height*5187.627118644067797),
87
            'width_emus' => (int)($new_width*5187.627118644067797),
88
        ];
89
    }
90
}
91