|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Erykai\Compress; |
|
4
|
|
|
|
|
5
|
|
|
use ErrorException; |
|
6
|
|
|
use Imagick; |
|
7
|
|
|
use ImagickException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
trait TraitCompress |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @return $this |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function compressPdf(): static |
|
18
|
|
|
{ |
|
19
|
|
|
$sDEVICE = "-sDEVICE=pdfwrite"; |
|
20
|
|
|
$dPDFSETTINGS = "-dPDFSETTINGS=/screen"; |
|
21
|
|
|
$dColorImageResolution = "-dColorImageResolution=" . $this->getQuality(); |
|
|
|
|
|
|
22
|
|
|
$sOutputFile = "-sOutputFile="; |
|
23
|
|
|
$output = $this->getOutput(); |
|
|
|
|
|
|
24
|
|
|
$input = $this->getInput(); |
|
|
|
|
|
|
25
|
|
|
$cmd = "gs $sDEVICE $dPDFSETTINGS $dColorImageResolution $sOutputFile$output $input"; |
|
26
|
|
|
shell_exec($cmd); |
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @throws ImagickException |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function compressImg(): static |
|
34
|
|
|
{ |
|
35
|
|
|
$image = new Imagick(); |
|
36
|
|
|
$image->readImage($this->getInput()); |
|
37
|
|
|
$image->resizeImage($this->getWidth(), 0, Imagick::FILTER_POINT, 10); |
|
|
|
|
|
|
38
|
|
|
$image->setImageCompressionQuality($this->getQuality()); |
|
39
|
|
|
$image->stripImage(); |
|
40
|
|
|
$image->writeImage($this->getOutput()); |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function compressVideo(): static |
|
49
|
|
|
{ |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function compressAudio(): static |
|
57
|
|
|
{ |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* copy and remove file |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function copyAndDelete(): void |
|
65
|
|
|
{ |
|
66
|
|
|
copy($this->getOutput(), $this->getInput()); |
|
67
|
|
|
unlink($this->getOutput()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @throws ErrorException |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createDir(string $path): void |
|
75
|
|
|
{ |
|
76
|
|
|
$folders = explode("/", $path); |
|
77
|
|
|
$dir = dirname(__DIR__, 4); |
|
78
|
|
|
foreach ($folders as $folder) { |
|
79
|
|
|
$dir .= "/" . $folder; |
|
80
|
|
|
if (!file_exists($dir) && !mkdir($dir) && !is_dir($dir)) { |
|
81
|
|
|
throw new ErrorException(sprintf('Directory "%s" was not created', $dir)); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |