Last active
January 24, 2017 22:37
-
-
Save subhashdasyam/6b39430a2ccbb28f6c87106066058231 to your computer and use it in GitHub Desktop.
Little Utils from bountify
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 | |
/* | |
Make sure that /tmp/check.json has permissions to read and write | |
It is better to create /tmp/check.json manually | |
type below commands if you are using only php | |
sudo echo > /tmp/check.json | |
sudo chmod 777 /tmp/check.json | |
sudo chown www-data:www-data /tmp/check.json | |
now call the below class | |
$lu = new LittleUtils(); | |
$lu->optimize('file.gif'); | |
$lu->optimize('file.jpg'); | |
$lu->optimize('file.png'); | |
Expected Output if image size is reduced | |
Array | |
( | |
[0] => 1 | |
[1] => Reduced by 30.2% (258.2 KB) | |
) | |
if already reduced | |
Array | |
( | |
[0] => | |
[1] => No savings | |
) | |
*/ | |
class LittleUtils{ | |
private static $jsonFile = '/tmp/check.json'; | |
private function check(){ | |
$result = array(); | |
$required = array( | |
'PNG' => 'opt-png', | |
'JPG' => 'opt-jpg', | |
'GIF' => 'opt-gif', | |
); | |
$disabled = array_map('trim', explode(',', ini_get('disable_functions'))); | |
if(in_array('exec', $disabled)){ | |
$result['exec'] = array("success"=>false,"msg"=>"<p><strong>CW Image Optimizer requires exec().</strong> Your system administrator has disabled this function.</p>"); | |
} | |
else{ | |
$result['exec'] = array("success"=>true,"msg"=>""); | |
} | |
$missing = array(); | |
foreach($required as $key => $req){ | |
$res = trim(exec('which ' . $req)); | |
if(empty($res)){ | |
$missing[] = $req; | |
} | |
} | |
$msg = implode(', ', $missing); | |
if(!empty($msg)){ | |
//echo "<p><strong>CW Image Optimizer requires <a href=\"http://sourceforge.net/projects/littleutils/\">littleutils</a>.</strong> You are missing: $msg.</p>"; | |
//die(); | |
$result['missing'] = array("success"=>false, "msg"=>"<p><strong>CW Image Optimizer requires <a href=\"http://sourceforge.net/projects/littleutils/\">littleutils</a>.</strong> You are missing: $msg.</p>"); | |
} | |
else{ | |
$result['missing'] = array("success"=>true,"msg"=>""); | |
} | |
if (!is_writable(self::$jsonFile) ) { | |
return "Unable to write the requirements file ".self::$jsonFile.". Please make sure the path is writable"; | |
} | |
return file_put_contents(self::$jsonFile,json_encode($result)); | |
} | |
private function checkSystemRequirements(){ | |
// If requirements file doesn't exists check the requirements and write to file | |
if (!file_exists(self::$jsonFile) && !is_file(self::$jsonFile)) { | |
$this->check(); | |
} | |
$content = json_decode(file_get_contents(self::$jsonFile),true); | |
// if the requirement file exists but if doesn't have required info check requirements again and rewrite the fresh file | |
if(!isset($content['exec']) || !isset($content['missing'])){ | |
$this->check(); | |
} | |
// if the requirements file exists and if the requirements are false check again. | |
if($content['exec']['success'] === false || $content['missing']['success'] === false){ | |
echo $content['exec']['msg']."<br/>".$content['missing']['msg']; | |
$this->check(); | |
} | |
} | |
public function optimize($file){ | |
$success = false; | |
self::checkSystemRequirements(); | |
$file_path = $file; | |
//check if File Exists | |
if ( FALSE === file_exists($file_path) || FALSE === is_file($file_path) ) { | |
$msg = sprintf("Could not find <span class='code'>%s</span>", $file_path); | |
return array($success, $msg); | |
} | |
// check that the file is writable | |
if ( FALSE === is_writable($file_path) ) { | |
$msg = sprintf("<span class='code'>%s</span> is not writable", $file_path); | |
return array($success, $msg); | |
} | |
if(function_exists('getimagesize')){ | |
$type = getimagesize($file_path); | |
if(false !== $type){ | |
$type = $type['mime']; | |
} | |
}elseif(function_exists('mime_content_type')){ | |
$type = mime_content_type($file_path); | |
}else{ | |
$type = 'Missing getimagesize() and mime_content_type() PHP functions'; | |
} | |
switch($type){ | |
case 'image/jpeg': | |
$command = 'opt-jpg'; | |
break; | |
case 'image/png': | |
$command = 'opt-png'; | |
break; | |
case 'image/gif': | |
$command = 'opt-gif'; | |
break; | |
default: | |
return array($success, 'Unknown type: ' . $type); | |
} | |
$result = exec($command . ' ' . escapeshellarg($file)); | |
$result = str_replace($file . ': ', '', $result); | |
if($result == 'unchanged') { | |
return array($success, 'No savings'); | |
} | |
if(strpos($result, ' vs. ') !== false) { | |
$s = explode(' vs. ', $result); | |
$savings = intval($s[0]) - intval($s[1]); | |
$savings_str = $this->cw_image_optimizer_format_bytes($savings, 1); | |
$savings_str = str_replace(' ', ' ', $savings_str); | |
$percent = 100 - (100 * ($s[1] / $s[0])); | |
$results_msg = sprintf("Reduced by %01.1f%% (%s)",$percent,$savings_str); | |
return array(true, $results_msg); | |
} | |
return array($success, 'Bad response from optimizer'); | |
} | |
function cw_image_optimizer_format_bytes($bytes, $precision = 2) { | |
$units = array('B', 'KB', 'MB', 'GB', 'TB'); | |
$bytes = max($bytes, 0); | |
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | |
$pow = min($pow, count($units) - 1); | |
$bytes /= pow(1024, $pow); | |
return round($bytes, $precision) . ' ' . $units[$pow]; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment