Completed
Pull Request — master (#3)
by Luca
08:03
created

ImageWriter::toWebP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jackal\ImageMerge\Model\Format;
4
5
use Exception;
6
use Jackal\ImageMerge\Http\Response\ImageResponse;
7
8
/**
9
 * Class ImageWriter
10
 * @package Jackal\ImageMerge\Model\Format
11
 */
12
class ImageWriter
13
{
14
    /**
15
     * @param $filePathName
16
     * @throws Exception
17
     */
18
    private static function checkPermissions($filePathName)
19
    {
20
        $directory = dirname($filePathName);
21
        if (!is_dir($directory)) {
22
            if (!mkdir(dirname($filePathName), 0777, true)) {
23
                throw new Exception(sprintf('Cannot create folder %s', $directory));
24
            }
25
        }
26
        if (!is_writable($directory)) {
27
            throw new Exception(sprintf('Cannot write into directory: %s', $directory));
28
        }
29
    }
30
31
    /**
32
     * @param $resource
33
     * @param null $filePathName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filePathName is correct as it would always require null to be passed?
Loading history...
34
     * @return bool|ImageResponse
35
     * @throws Exception
36
     */
37
    public static function toPNG($resource, $filePathName = null)
38
    {
39
        ob_start();
40
        imagepng($resource, null, 9);
41
        $content = ob_get_clean();
42
43
        return self::writeFile($content, $filePathName, 'image/png');
44
    }
45
46
    /**
47
     * @param $resource
48
     * @param null $filePathName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filePathName is correct as it would always require null to be passed?
Loading history...
49
     * @return bool|ImageResponse
50
     * @throws Exception
51
     */
52
    public static function toJPG($resource, $filePathName = null)
53
    {
54
        ob_start();
55
        imagejpeg($resource, null, 100);
56
        $content = ob_get_clean();
57
58
        return self::writeFile($content, $filePathName, 'image/jpg');
59
    }
60
61
    /**
62
     * @param $resource
63
     * @param null $filePathName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filePathName is correct as it would always require null to be passed?
Loading history...
64
     * @return bool|ImageResponse
65
     * @throws Exception
66
     */
67
    public static function toGIF($resource, $filePathName = null)
68
    {
69
        ob_start();
70
        imagegif($resource);
71
        $content = ob_get_clean();
72
73
        return self::writeFile($content, $filePathName, 'image/gif');
74
    }
75
76
    /**
77
     * @param $resource
78
     * @param null $filePathName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filePathName is correct as it would always require null to be passed?
Loading history...
79
     * @return bool|ImageResponse
80
     * @throws Exception
81
     */
82
    public static function toWebP($resource, $filePathName = null)
83
    {
84
        ob_start();
85
        imagewebp($resource);
86
        $content = ob_get_clean();
87
88
        return self::writeFile($content, $filePathName, 'image/webp');
89
    }
90
91
    /**
92
     * @param $content
93
     * @param $filePathName
94
     * @param $contentType
95
     * @return bool|ImageResponse
96
     * @throws Exception
97
     */
98
    private static function writeFile($content, $filePathName, $contentType)
99
    {
100
        if ($filePathName) {
101
            ImageWriter::checkPermissions($filePathName);
102
103
            return file_put_contents($filePathName, $content) == true;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing file_put_contents($filePathName, $content) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
104
        }
105
106
        return new ImageResponse($content, 200, [
107
            'content-type' => [$contentType],
108
        ]);
109
    }
110
}
111