View Single Post
Old 10-17-2012, 12:00 PM   PM User | #4
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
actually imagemagick was made for scripting.
Unfortunately I can't help you with macOS. I can however help you with doing what you want to do on server. That's assuming your server runs php, is linux-based, have shell_exec enabled and has imagemagick installed.
If not, then you'll have to look for other solution (or better yet change hosting )

here's what you want to do:
1)take code below, edit vars (read comments in code) and save it on your server as 'resize.php' at the same level with your directory with pictures.

2)crete new directory for resized pics(make sure server can write to that directory)

you should have something like that:
http://domain.com/resize.php
http://domain.com/original-directory/
http://domain.com/resizec-pics/

3)run script from browser nd let it run untill you'll see pop-up message

with 1000 pics it will take a lot time, but you'll get there.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="reload()">
<?php
//configuration

//directory with original imges
$in_dir 'pics';

//directory for resized images
$out_dir 'resized';

//target canve's size
$size '800x800';

//how many pics to process in one run.
//keep it rather low to avoid timeouts
//(it will re-run itself as many times as needed anyways)
$limit 2;

if(!
is_dir($in_dir)){
    die(
'wrong input directory!</body>');
}
if(!
is_dir($out_dir)){
    die(
'wrong output directory!</body>');
}
if(!
file_put_contents($out_dir '/__writetest''1')){
    die(
'can\'t write to output directory. check permissions!</body>');
}else{
    
unlink($out_dir '/__writetest');
}
$files glob($in_dir '/*');

$i 0;
$ii 0;
while(isset(
$files[$i])){
    if(
preg_match('/.+(png|jpg|jpeg|gif|bmp|pcx)/i'basename($files[$i]))){
        if(!
file_exists($out_dir '/' .  basename($files[$i])) && $ii $limit){
            
$file1 $files[$i];
            
$file2 $out_dir '/' .  basename($files[$i]);
            
$command 'convert ' $file1 ' -resize ' $size ' -size ' $size ' xc:white +swap -gravity center -composite ' $file2;
            
shell_exec($command);
            echo 
'Resized ' $files[$i] . ' and saved as ' $file2 '<br />';
            
$ii++;
        }
    }
    
$i++;
}
echo 
'<br /><br /><noscript>reload to proccess next images</noscript>';

    echo 
'<script language="javascript">' "\n";
    echo 
'function reload(){' "\n";
if(
$ii 0){
    echo 
'window.location = \'' $_SERVER['PHP_SELF'] . '?rand=' rand(0999999999999) . '\';' "\n";
}else{
    echo 
'alert(\'all pics resized\');' "\n";
}
    echo 
'}' "\n";
    echo 
'</script>' "\n";
?>
</body>
</html>
It will resize all images from source directory and save them with the same names in another directory. this way once it's done you'll just have to rename directories
just let it run in browser. it will let you know when it's finished.
patryk is offline   Reply With Quote