Skip to content

Instantly share code, notes, and snippets.

@vijinho
Created September 2, 2018 04:25
Show Gist options
  • Save vijinho/c5a2cec4e3f2505606ba8e43cd6c5f58 to your computer and use it in GitHub Desktop.
Save vijinho/c5a2cec4e3f2505606ba8e43cd6c5f58 to your computer and use it in GitHub Desktop.
find small jpg images
<?php
// find small jpeg images
$cmd = 'find . -type f -iname "*.jp*" -print';
$files = cmd_execute($cmd);
$counts = [];
$by_size = [];
foreach ($files as $k => $path) {
$data = getimagesize($path);
// ignore non-jpegs
if (empty($data['mime']) || 2 !== $data[2] || 'image/jpeg' !== $data['mime']) continue;
$width = $data[0];
$height = $data[1];
// ignore large images
$longest = ($width >= $height) ? $width : $height;
if ($longest > 640) {
continue;
}
if (stristr($path, 'social_media/201')) {
echo "$longest: $path\n";
//unlink($path);
//continue;
}
$counts[$longest]++;
$by_size[$longest][] = $path;
//echo "$path\n";
//print_r($data);
unset($files[$k]);
}
echo "\nSummary by size:";
ksort($counts);
print_r($counts);
ksort($by_size);
print_r($by_size);
exit;
/**
* Execute a command and return streams as an array of
* stdin, stdout, stderr
*
* @param string $cmd command to execute
* @return mixed array $streams | boolean false if failure
* @see https://secure.php.net/manual/en/function.proc-open.php
*/
function shell_execute($cmd)
{
$process = proc_open(
$cmd,
[
['pipe', 'r'],
['pipe', 'w'],
['pipe', 'w']
],
$pipes
);
if (is_resource($process)) {
$streams = [];
foreach ($pipes as $p => $v) {
$streams[] = stream_get_contents($pipes[$p]);
}
proc_close($process);
return [
'stdin' => $streams[0],
'stdout' => $streams[1],
'stderr' => $streams[2]
];
}
return false;
}
/**
* Execute a command and return output of stdout or throw exception of stderr
*
* @param string $cmd command to execute
* @param boolean $split split returned results? default on newline
* @param string $exp regular expression to preg_split to split on
* @return mixed string $stdout | Exception if failure
* @see shell_execute($cmd)
*/
function cmd_execute($cmd, $split = true, $exp = "/\n/") {
$result = shell_execute($cmd);
if (!empty($result['stderr'])) {
throw new Exception($result['stderr']);
}
$data = $result['stdout'];
if (empty($split) || empty($exp) || empty($data)) {
return $data;
}
return preg_split($exp, $data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment