Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2016 12:58
Show Gist options
  • Save anonymous/06bac7c8cdbc498af8462eb44d1d1048 to your computer and use it in GitHub Desktop.
Save anonymous/06bac7c8cdbc498af8462eb44d1d1048 to your computer and use it in GitHub Desktop.
Batch resize images with ImageMagick ( and php )
<?php
//path to image magick
$convert = '/usr/local/bin/convert';
$source = __DIR__ . '/all_images';
$dest = __DIR__ . '/resize';
$quality = 95; // 1-100 ; 92 is default.
$maxSize = 3680; //in pixels
$fileList = glob("$source/*.JPG");
echo "Searching on $source\n Found " . count($fileList) . " files to convert. \n";
echo "This script will rezise images and output them in $dest.\n";
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
fclose($handle);
echo "\n";
echo "continuing...\n";
$opCount = 1;
foreach ( $fileList as $sourceFile) {
$fileName = basename($sourceFile);
echo "$opCount: $fileName old size " . filesize($sourceFile) . "\n";
$cmd = "$convert $sourceFile -strip -quality $quality -resize {$maxSize}x{$maxSize} $dest/$fileName";
`$cmd`;
echo "$opCount: $fileName new size " . filesize("$dest/$fileName") . "\n";
$opCount++;
}
echo "Conversion done.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment