Skip to content

Instantly share code, notes, and snippets.

@BramEsposito
Last active May 26, 2016 12:34
Show Gist options
  • Save BramEsposito/dadf449659451dc00cbfed55bf119ad6 to your computer and use it in GitHub Desktop.
Save BramEsposito/dadf449659451dc00cbfed55bf119ad6 to your computer and use it in GitHub Desktop.
Optimize your images when uploaded with IMCE
<?php
/**
* You need to have module imageapi_optimize enabled
*/
function HOOK_file_presave($file) {
// only temporary files from IMCE
if ($file->source === 'imce' && $file->status == 0) {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($file->uri)) {
$path = $wrapper->realpath();
$fs = filesize($path);
_imageapi_optimize_optimize(image_load($path), $path);
clearstatcache($path);
$nfs = filesize($path);
watchdog('PHP', "%percentage% reduction on %filename (from %filesize to %filesize_new)", array('%percentage' => round(100-$nfs/$fs*100), '%filename' => $file->uri,'%filesize' => $fs, '%filesize_new' => $nfs), WATCHDOG_INFO);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment