7
7
8
8
final class Convertor
9
9
{
10
- public const FORMAT_JPG = 'jpg ' ;
10
+ public const
11
+ FormatJpg = 'jpg ' ,
12
+ FormatPng = 'png ' ,
13
+ FormatGif = 'gif ' ;
11
14
12
- public const FORMAT_PNG = 'png ' ;
13
-
14
- public const FORMAT_GIF = 'gif ' ;
15
-
16
- public const SUPPORTED_FORMATS = [self ::FORMAT_JPG , self ::FORMAT_PNG , self ::FORMAT_GIF ];
15
+ public const SupportedFormats = [self ::FormatJpg, self ::FormatPng, self ::FormatGif];
17
16
18
17
19
18
/** @throws \Error */
@@ -28,24 +27,28 @@ public function __construct()
28
27
*
29
28
* @throws ConvertorException
30
29
*/
31
- public static function convert (string $ pdfPath , string $ savePath , string $ format = 'jpg ' , bool $ trim = false ): void
32
- {
33
- if (\in_array ($ format = strtolower ($ format ), self ::SUPPORTED_FORMATS , true ) === false ) {
34
- throw new \InvalidArgumentException (
35
- 'Format " ' . $ format . '" is not supported. '
36
- . 'Did you mean " ' . implode ('", " ' , self ::SUPPORTED_FORMATS ) . '"? ' ,
37
- );
30
+ public static function convert (
31
+ string |Configuration $ pdfPath ,
32
+ ?string $ savePath = null ,
33
+ string $ format = 'jpg ' ,
34
+ bool $ trim = false
35
+ ): void {
36
+ $ configuration = is_string ($ pdfPath )
37
+ ? Configuration::from ($ pdfPath , $ savePath , $ format , $ trim )
38
+ : $ pdfPath ;
39
+
40
+ if (in_array ($ configuration ->format , self ::SupportedFormats, true ) === false ) {
41
+ throw new \InvalidArgumentException (sprintf (
42
+ 'Format "%s" is not supported. Did you mean "%s"? ' ,
43
+ $ configuration ->format ,
44
+ implode ('", " ' , self ::SupportedFormats),
45
+ ));
38
46
}
39
- if (\ is_file ($ pdfPath ) === false ) {
40
- throw new ConvertorException ('File " ' . $ pdfPath . ' " does not exist. ' );
47
+ if (is_file ($ configuration -> pdfPath ) === false ) {
48
+ throw new ConvertorException (sprintf ( 'File "%s " does not exist. ' , $ configuration -> pdfPath ) );
41
49
}
42
50
try {
43
- $ im = self ::process ($ pdfPath , $ savePath );
44
- if ($ trim === true ) {
45
- $ im ->setImageBorderColor ('rgb(255,255,255) ' );
46
- $ im ->trimImage (1 );
47
- self ::write ($ savePath , (string ) $ im );
48
- }
51
+ self ::process ($ configuration );
49
52
} catch (\ImagickException $ e ) {
50
53
throw new ConvertorException ($ e ->getMessage (), $ e ->getCode (), $ e );
51
54
}
@@ -55,17 +58,22 @@ public static function convert(string $pdfPath, string $savePath, string $format
55
58
/**
56
59
* @throws \ImagickException
57
60
*/
58
- private static function process (string $ pdfPath , string $ savePath ): \ Imagick
61
+ private static function process (Configuration $ configuration ): void
59
62
{
60
63
if (class_exists ('\Imagick ' ) === false ) {
61
64
throw new \RuntimeException ('Imagick is not installed. ' );
62
65
}
63
66
64
- $ im = new \Imagick ($ pdfPath );
65
- $ im ->setImageFormat ('jpg ' );
66
- self ::write ($ savePath , (string ) $ im );
67
-
68
- return $ im ;
67
+ $ im = new \Imagick ($ configuration ->pdfPath );
68
+ $ im ->setImageFormat ($ configuration ->format );
69
+ if ($ configuration ->cols !== null && $ configuration ->rows !== null ) {
70
+ $ im ->scaleImage ($ configuration ->cols , $ configuration ->rows , $ configuration ->bestfit );
71
+ }
72
+ if ($ configuration ->trim ) {
73
+ $ im ->setImageBorderColor ('rgb(255,255,255) ' );
74
+ $ im ->trimImage (1 );
75
+ }
76
+ self ::write ($ configuration ->savePath , (string ) $ im );
69
77
}
70
78
71
79
@@ -76,12 +84,12 @@ private static function process(string $pdfPath, string $savePath): \Imagick
76
84
*/
77
85
private static function write (string $ file , string $ content , ?int $ mode = 0_666 ): void
78
86
{
79
- static ::createDir (dirname ($ file ));
87
+ self ::createDir (dirname ($ file ));
80
88
if (@file_put_contents ($ file , $ content ) === false ) { // @ is escalated to exception
81
- throw new ConvertorException ('Unable to write file " ' . $ file . ' ": ' . self ::getLastError ());
89
+ throw new ConvertorException (sprintf ( 'Unable to write file "%s ": %s ' , $ file , self ::getLastError () ));
82
90
}
83
91
if ($ mode !== null && !@chmod ($ file , $ mode )) { // @ is escalated to exception
84
- throw new ConvertorException ('Unable to chmod file " ' . $ file . ' ": ' . self ::getLastError ());
92
+ throw new ConvertorException (sprintf ( 'Unable to chmod file "%s ": %s ' , $ file , self ::getLastError () ));
85
93
}
86
94
}
87
95
@@ -94,7 +102,7 @@ private static function write(string $file, string $content, ?int $mode = 0_666)
94
102
private static function createDir (string $ dir , int $ mode = 0_777 ): void
95
103
{
96
104
if (!is_dir ($ dir ) && !@mkdir ($ dir , $ mode , true ) && !is_dir ($ dir )) { // @ - dir may already exist
97
- throw new ConvertorException ('Unable to create directory " ' . $ dir . ' ": ' . self ::getLastError ());
105
+ throw new ConvertorException (sprintf ( 'Unable to create directory "%s ": %s ' , $ dir , self ::getLastError () ));
98
106
}
99
107
}
100
108
0 commit comments