Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13990817
D8225.id19569.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D8225.id19569.diff
View Options
diff --git a/src/infrastructure/celerity/CelerityResourceTransformer.php b/src/infrastructure/celerity/CelerityResourceTransformer.php
--- a/src/infrastructure/celerity/CelerityResourceTransformer.php
+++ b/src/infrastructure/celerity/CelerityResourceTransformer.php
@@ -129,7 +129,14 @@
if ($this->celerityMap) {
$resource_uri = $this->celerityMap->getURIForName($alternative);
if ($resource_uri) {
- $uri = $resource_uri;
+ // Check if we can use a data URI for this resource. If not, just
+ // use a normal Celerity URI.
+ $data_uri = $this->generateDataURI($alternative);
+ if ($data_uri) {
+ $uri = $data_uri;
+ } else {
+ $uri = $resource_uri;
+ }
break;
}
}
@@ -226,4 +233,45 @@
return implode("\n\n", $rules);
}
+
+
+ /**
+ * Attempt to generate a data URI for a resource. We'll generate a data URI
+ * if the resource is a valid resource of an appropriate type, and is
+ * relatively small. Otherwise, this method will return `null` and we'll
+ * end up using a normal URI instead.
+ *
+ * @param string Resource name to attempt to generate a data URI for.
+ * @return string|null Data URI, or null if we declined to generate one.
+ */
+ private function generateDataURI($resource_name) {
+ $ext = last(explode('.', $resource_name));
+ switch ($ext) {
+ case 'png':
+ $type = 'image/png';
+ break;
+ case 'gif':
+ $type = 'image/gif';
+ break;
+ case 'jpg':
+ $type = 'image/jpeg';
+ break;
+ default:
+ return null;
+ }
+
+ // This is completely arbitrary, but roughly puts most of the smaller
+ // images inline and leaves most of the larger images as separate
+ // requests.
+ $maximum_data_size = (1024 * 32);
+
+ $data = $this->celerityMap->getResourceDataForName($resource_name);
+ if (strlen($data) > $maximum_data_size) {
+ return null;
+ }
+
+ return 'data:'.$type.';base64,'.base64_encode($data);
+ }
+
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Oct 23, 6:26 AM (2 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6743720
Default Alt Text
D8225.id19569.diff (2 KB)
Attached To
Mode
D8225: Automatically inline small images using data URIs
Attached
Detach File
Event Timeline
Log In to Comment