Created
August 30, 2018 17:42
-
-
Save vijinho/204874f640073d814922444961f4ab35 to your computer and use it in GitHub Desktop.
find square images in a folder and list number per pixel width size and also which files
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 | |
| // find square 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]; | |
| // only take squares | |
| if (!($width == $height)) { | |
| continue; | |
| } | |
| $counts[$width]++; | |
| $by_size[$width][] = $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