开发者

Easy way to resize a directory full of images

Looking for an easy way to resize a directory of images to a given set width and he开发者_运维技巧ight.

Does anyone know an easy way to do this in php or javascript? I have a directory that contains about 100 images and doing this in photoshop would just be highly inefficient.

Thanks


I would use a shell script for this. Would that work for you? If so, you can do this.

for i in *.jpg
do
convert $i -scale 50% $(basename $i .jpg)-scaled.jpg
done

The convert program is part of ImageMagick.


If you have ImageMagick installed on your system you could try the mogrify command:

<?php
chdir( 'dir/with/images' );
//using backticks to run system command
`mogrify -format png -resize 256x256 *.jpg`;


Do a loop through the directory, inside the loop use:


$img= new Imagick($srcpath);
$img->resizeImage($width,$height,Imagick::FILTER_BOX,1,true);

http://php.net/manual/en/function.imagick-resizeimage.php


Resize image base on ( min-width + ration ) + subdirectories

<?php
ini_set('max_execution_time', 0);
listFolderFiles('assets');
ini_set('memory_limit', '-1');

function listFolderFiles($dir){
    $Allow = array('image/jpeg','image/png');
    $Quality = 80;
    $MaxAllowedWidth = 1200;

    $ffs = scandir($dir);
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            $what = $dir.'/'.$ff;
            if ( $mime =  mime_content_type($what) ) {
                if ( in_array($mime, $Allow) ) {
                    $siz = getimagesize($what);
                    echo $what ." \n" . $mime. " - " . $siz[0] . 'x'. $siz[1];
                    echo "\n\n";
                    if ( $MaxAllowedWidth <= $siz[0] ) {
                        resizeitbyration($MaxAllowedWidth-1, $what, $Quality,$mime);
                        echo 'Ujebac' . "\n";
                    }
                }
            }
            if ( $mime == 'directory' ) listFolderFiles($what);
        }
    }
}

function resizeitbyration($newwidth, $source,$Quality, $mime) {
    $org = getimagesize($source);

    $ratio = $newwidth / $org[0];
    $newheight = $org[1] * $ratio;
    if ( $mime == 'image/png' ) $from = imagecreatefrompng($source);
    else $from = imagecreatefromjpeg($source);

    $new_image = imagecreatetruecolor($newwidth, $newheight);
    try {
    imagecopyresampled($new_image, $from, 0, 0, 0, 0, $newwidth, $newheight, $org[0], $org[1]);

    if ( $mime == 'image/png' ) imagepng($new_image, $source);
    else imagejpeg($new_image, $source, $Quality);
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜