Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/PhabricatorImageTransformer.php
12241 views
1
<?php
2
3
/**
4
* @task enormous Detecting Enormous Images
5
* @task save Saving Image Data
6
*/
7
final class PhabricatorImageTransformer extends Phobject {
8
9
10
/* -( Saving Image Data )-------------------------------------------------- */
11
12
13
/**
14
* Save an image resource to a string representation suitable for storage or
15
* transmission as an image file.
16
*
17
* Optionally, you can specify a preferred MIME type like `"image/png"`.
18
* Generally, you should specify the MIME type of the original file if you're
19
* applying file transformations. The MIME type may not be honored if
20
* Phabricator can not encode images in the given format (based on available
21
* extensions), but can save images in another format.
22
*
23
* @param resource GD image resource.
24
* @param string? Optionally, preferred mime type.
25
* @return string Bytes of an image file.
26
* @task save
27
*/
28
public static function saveImageDataInAnyFormat($data, $preferred_mime = '') {
29
$preferred = null;
30
switch ($preferred_mime) {
31
case 'image/gif':
32
$preferred = self::saveImageDataAsGIF($data);
33
break;
34
case 'image/png':
35
$preferred = self::saveImageDataAsPNG($data);
36
break;
37
}
38
39
if ($preferred !== null) {
40
return $preferred;
41
}
42
43
$data = self::saveImageDataAsJPG($data);
44
if ($data !== null) {
45
return $data;
46
}
47
48
$data = self::saveImageDataAsPNG($data);
49
if ($data !== null) {
50
return $data;
51
}
52
53
$data = self::saveImageDataAsGIF($data);
54
if ($data !== null) {
55
return $data;
56
}
57
58
throw new Exception(pht('Failed to save image data into any format.'));
59
}
60
61
62
/**
63
* Save an image in PNG format, returning the file data as a string.
64
*
65
* @param resource GD image resource.
66
* @return string|null PNG file as a string, or null on failure.
67
* @task save
68
*/
69
private static function saveImageDataAsPNG($image) {
70
if (!function_exists('imagepng')) {
71
return null;
72
}
73
74
// NOTE: Empirically, the highest compression level (9) seems to take
75
// up to twice as long as the default compression level (6) but produce
76
// only slightly smaller files (10% on avatars, 3% on screenshots).
77
78
ob_start();
79
$result = imagepng($image, null, 6);
80
$output = ob_get_clean();
81
82
if (!$result) {
83
return null;
84
}
85
86
return $output;
87
}
88
89
90
/**
91
* Save an image in GIF format, returning the file data as a string.
92
*
93
* @param resource GD image resource.
94
* @return string|null GIF file as a string, or null on failure.
95
* @task save
96
*/
97
private static function saveImageDataAsGIF($image) {
98
if (!function_exists('imagegif')) {
99
return null;
100
}
101
102
ob_start();
103
$result = imagegif($image);
104
$output = ob_get_clean();
105
106
if (!$result) {
107
return null;
108
}
109
110
return $output;
111
}
112
113
114
/**
115
* Save an image in JPG format, returning the file data as a string.
116
*
117
* @param resource GD image resource.
118
* @return string|null JPG file as a string, or null on failure.
119
* @task save
120
*/
121
private static function saveImageDataAsJPG($image) {
122
if (!function_exists('imagejpeg')) {
123
return null;
124
}
125
126
ob_start();
127
$result = imagejpeg($image);
128
$output = ob_get_clean();
129
130
if (!$result) {
131
return null;
132
}
133
134
return $output;
135
}
136
137
138
}
139
140