|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new ImageResponse($content, 200, [ |
|
107
|
|
|
'content-type' => [$contentType], |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|