-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.php
85 lines (66 loc) · 2.1 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Padam87\PdfPreflight;
use Smalot\PdfParser\Object as PdfObject;
use Smalot\PdfParser\XObject\Image;
class Utils
{
public static function imageToImagick(Image $image): \Imagick
{
$details = $image->getDetails();
$cs = $image->getHeader()->get('ColorSpace');
if ($cs instanceof PdfObject) {
if ($cs->has(0) && $cs->get(0) == 'ICCBased') {
/** @var PdfObject $iccProfile */
$iccProfile = $cs->get(1);
switch ($iccProfile->getDetails()['N']) {
case 1:
$cs = 'DeviceGray';
break;
case 3:
$cs = 'DeviceRGB';
break;
case 4:
$cs = 'DeviceCMYK';
break;
}
}
} else {
$cs = (string) $cs;
}
$img = new \Imagick();
$img->setSize($details['Width'], $details['Height']);
$img->setOption('depth', $details['BitsPerComponent']);
switch ($cs) {
case 'DeviceRGB':
$img->setFormat('RGB');
$img->setColorspace(\Imagick::COLORSPACE_RGB);
break;
case 'DeviceGray':
$img->setFormat('GRAY');
$img->setColorspace(\Imagick::COLORSPACE_GRAY);
break;
case 'DeviceCMYK':
default:
$img->setFormat('CMYK');
$img->setColorspace(\Imagick::COLORSPACE_CMYK);
break;
}
$img->readImageBlob($image->getContent());
return $img;
}
public static function getInkDensity($c, $m, $y, $k): float
{
return (float) (($c + $m + $y + $k) * 100);
}
public static function rgbToCmyk($r, $g, $b): array
{
$c = (255 - $r) / 255;
$m = (255 - $g) / 255;
$y = (255 - $b) / 255;
$k = min([$c, $m, $y]);
$c = $c - $k;
$m = $m - $k;
$y = $y - $k;
return [$c, $m, $y, $k];
}
}