Last active
May 26, 2016 12:34
-
-
Save BramEsposito/dadf449659451dc00cbfed55bf119ad6 to your computer and use it in GitHub Desktop.
Optimize your images when uploaded with IMCE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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