Passed
Push — master ( b04009...b8980d )
by Tobias
02:33
created

DocImage::GetImageFromUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

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 9.2
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace WrkLst\DocxMustache;
4
5
use Exception;
6
use Illuminate\Support\Facades\Log;
7
8
class DocImage
9
{
10
    protected function AllowedContentTypeImages()
11
    {
12
        return array(
13
            'image/gif' => '.gif',
14
            'image/jpeg' => '.jpeg',
15
            'image/png' => '.png',
16
            'image/bmp' => '.bmp',
17
        );
18
    }
19
20
    public function GetImageFromUrl($url)
21
    {
22
        $allowed_imgs = $this->AllowedContentTypeImages();
23
24
        if ($img_file_handle = fopen($url.$this->imageManipulation, "rb"))
0 ignored issues
show
Bug introduced by
The property imageManipulation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        {
26
            $img_data = stream_get_contents($img_file_handle);
27
            fclose($img_file_handle);
28
            $fi = new \finfo(FILEINFO_MIME);
29
30
            $image_mime = strstr($fi->buffer($img_data), ';', true);
31
            //dd($image_mime);
32
            if (isset($allowed_imgs[$image_mime]))
33
            {
34
                return array(
35
                    'data' => $img_data,
36
                    'mime' => $image_mime,
37
                );
38
            }
39
        }
40
        return false;
41
    }
42
43
    protected function ResampleImage($parent, $imgs, $k, $data)
44
    {
45
        \Storage::disk($parent->storageDisk)->put($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'], $data);
46
47
        //rework img to new size and jpg format
48
        $img_rework = \Image::make($parent->storagePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src']));
49
50
        $w = $imgs[$k]['width'];
51
        $h = $imgs[$k]['height'];
52
53
        if ($w > $h)
54
        {
55
            $h = null;
56
        } else
57
        {
58
            $w = null;
59
        }
60
61
        $img_rework->resize($w, $h, function($constraint) {
62
            $constraint->aspectRatio();
63
            $constraint->upsize();
64
        });
65
66
        $new_height = $img_rework->height();
67
        $new_width = $img_rework->width();
68
        $img_rework->save($parent->storagePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
69
70
        $parent->zipper->folder('word/media')->add($parent->storagePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest']));
71
72
        return array(
73
            'height' => $new_height,
74
            'width' => $new_width,
75
        );
76
    }
77
}
78