Skip to content

Instantly share code, notes, and snippets.

View jonarnes's full-sized avatar

Jon Arne S. jonarnes

View GitHub Profile
export default {
async fetch(request) {
const url = new URL(request.url);
const path = url.pathname + url.search;
const imageEngineURL = new URL(`${HOSTN}${path}`);
const acceptHeader = request.headers.get('Accept');
let format = null;
//customize in preferred order.
(() => {
addEventListener("fetch", (event) => {
if (event.request.url.match(/\.(jpe?g|png|gif|svg|webp|avif|jxl)/i) && event.request.method === "GET") {
event.respondWith(handleRequest(event));
} else {
event.respondWith(fetch(event.request));
}
});
async function handleRequest(event) {
const request = event.request;
@jonarnes
jonarnes / cfWorker.js
Last active March 14, 2024 12:11
Use ImageEngine to optimize images through Cloudflare workers. A starting point which aligns the cache key computation on both sides
addEventListener('fetch', event => {
if (event.request.url.match(/\.(jpe?g|png|gif|svg|webp|avif|jxl)/i) && event.request.method === "GET") {
event.respondWith(handleRequest(event));
}else{
event.respondWith(fetch(event.request));
}
})
async function handleRequest(event) {
const request = event.request;
@jonarnes
jonarnes / ie.php
Created January 24, 2022 10:55
Snippet to enable ImageEngine in laravel apps.
<?php
/**
* Computes an ImageEngine src for images as defined in the config.
*
* May be used in *.blade.php files like this:
* <img src="{{ imageengine( "/img/img.jpeg",array("directives"=>"/w_200/") ) }}">
*
* @param string $asset
* @param array $options
* @return string
Item Cloudinary ImageEngine
Original Payload GB 250 500
Transformations (estimated) 5000
Transformation Credits 5.0
Storage GB (estimated) 5
Storage Credits 5.0
Payload Reduction % 60% 80%
Outbound Optimized Payload GB 100 100
Outbound GB Credits 100.0
Plan Name Plus Basic
### Optimized with Save-Data:on
$ curl -I -H"Save-Data: on" https://try.imgeng.in/http://wurfl.github.io/i/pencils.jpg
HTTP/2 200
content-type: image/jpeg
content-length: 21246
### Optimized
$ curl -I https://try.imgeng.in/http://wurfl.github.io/i/pencils.jpg
HTTP/2 200
content-type: image/jpeg
content-length: 93535
@jonarnes
jonarnes / httparchive.sql
Last active October 2, 2019 09:29
Big Queries for httparchive data
/*
How many images are served directly from s3
*/
SELECT resp_server,
COUNT(distinct pageid) pages_w_amz,
(SELECT COUNT(*) FROM `httparchive.summary_pages.2019_09_01_desktop`) as total_pages
FROM `httparchive.summary_requests.2019_09_01_desktop` requests
where type="image" and REGEXP_CONTAINS(url, r"^https?:\/\/[a-zA-Z0-9_.]*s3.*\.amazonaws\.com")
GROUP BY resp_server
ORDER BY pages_w_amz DESC

Keybase proof

I hereby claim:

  • I am jonarnes on github.
  • I am jonarnes (https://keybase.io/jonarnes) on keybase.
  • I have a public key ASBHRYj1cSbQWCl2taCI_nx6wO5NMNsxTrmZKd3Uqw_L9Qo

To claim this, I am signing this object:

@jonarnes
jonarnes / script.js
Last active August 29, 2015 13:58
wurfl.js css bridge
/*Adding a CSS class to the html element (Modernizr-style)*/
document.documentElement.className += ' ' + (WURFL.is_mobile ? '' : 'no-') + "mobile"; /*adds class="mobile" or class="no-mobile" to html element*/
document.documentElement.className += ' ' + WURFL.form_factor.toLowerCase(); /*Adding the formfactor as class name to html element*/
document.documentElement.className += ' ' + WURFL.complete_device_name.toLowerCase().replace(/\s+/g, ''); /*Removing spaces, and puts the complete device name as the class name on html element*/
/*...or using data- attributes. In the CSS we can then do stuff like:*/
document.documentElement.setAttribute('data-device_name', WURFL.complete_device_name);
document.documentElement.setAttribute('data-form_factor', WURFL.form_factor );
document.documentElement.setAttribute('data-is_mobile', WURFL.is_mobile );