Passed
Push — master ( bbdf50...dbee39 )
by Tobias
03:38 queued 23s
created

DocImage::ResampleImage()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 73
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 0
Metric Value
c 5
b 3
f 0
dl 0
loc 73
rs 8.6829
cc 4
eloc 32
nc 8
nop 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, $dpi = 72)
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
        if($dpi!=72) $img_rework2 = \Image::make($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src']));
48
49
        $imgWidth = $img_rework->width();
50
        $imgHeight = $img_rework->height();
51
52
        //check https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
53
        // for EMUs calculation
54
        /*
55
        295px @72 dpi = 1530350 EMUs = Multiplier for 72dpi pixels 5187.627118644067797
56
        413px @72 dpi = 2142490 EMUs = Multiplier for 72dpi pixels 5187.627118644067797
57
        */
58
        $availableWidth = (int) ($imgs[$k]['cx'] / 5187.627118644067797);
59
        $availableHeight = (int) ($imgs[$k]['cy'] / 5187.627118644067797);
60
61
        //height based resize
62
        $h = (($imgHeight / $imgWidth) * $availableWidth);
63
        $w = (($imgWidth / $imgHeight) * $h);
64
65
        //if height based resize has too large width, do width based resize
66
        if ($h > $availableHeight) {
67
            $w = (($imgWidth / $imgHeight) * $availableHeight);
68
            $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...
69
        }
70
71
        $h = null;
72
73
        //for getting non high dpi measurements, as the document is on 72 dpi.
74
        //TODO: this should be improved. it does not really need the resampling to identify the new sizes.
75
        //instead this should just be calculated, as resampling the image is too process instensive.
76
        $img_rework->resize($w, $h, function ($constraint) {
77
            $constraint->aspectRatio();
78
            $constraint->upsize();
79
        });
80
        $new_height = $img_rework->height();
81
        $new_width = $img_rework->width();
82
83
        if($dpi!=72)
84
        {
85
            //for storing the image in high dpi, so it has good quality on high dpi screens
86
            $img_rework2->resize(((int)$w*($dpi/72)), $h, function ($constraint) { // make high dpi version for actual storage
0 ignored issues
show
Bug introduced by
The variable $img_rework2 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
87
                $constraint->aspectRatio();
88
                $constraint->upsize();
89
            });
90
            $img_rework2->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
91
92
            //set dpi of image to high dpi
93
            /*$im = new \imagick();
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
            $im->setResolution($dpi,$dpi);
95
            $im->readImage($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
96
            $im->writeImage($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
97
            $im->clear();
98
            $im->destroy();
99
            */
100
        }
101
        else {
102
            $img_rework->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
103
        }
104
105
        $parent->zipper->folder('word/media')->add($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
106
107
        return [
108
            'height' => $new_height,
109
            'width'  => $new_width,
110
            'height_emus' => (int) ($new_height * 5187.627118644067797),
111
            'width_emus' => (int) ($new_width * 5187.627118644067797),
112
        ];
113
    }
114
}
115