Erykai /
compress
| 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(); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 22 | $sOutputFile = "-sOutputFile="; |
||||||
| 23 | $output = $this->getOutput(); |
||||||
|
0 ignored issues
–
show
It seems like
getOutput() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 24 | $input = $this->getInput(); |
||||||
|
0 ignored issues
–
show
It seems like
getInput() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
It seems like
getWidth() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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 | } |