how to automatically copy an entire directory as thumbnails using phpthumb (or some other library)

狂风中的少年 提交于 2019-12-25 08:01:01

问题


I would like all images dropped in a directory to be copied into a separate directory as thumbnails that i can then view on my site. Right now i'm researching phpthumb, but have also downloaded wideimage and zen photo.

Thanks if you can find an exact duplicate to this, near matches may also be helpful.

Here is the script that only copies a single files:

require_once '../ThumbLib.inc.php';
//include the thumb library 
$thumb = PhpThumbFactory::create('test.jpg');
//create a new image from the targeted jpegs
$thumb->adaptiveResize(100, 100);
//resize 
$thumb->save('test.png', 'png');
//save as 'test' with the file type png
//echo "<img src='$thumb' class='thumb'>";
$thumb->show();
//print the thumbnail to the screen

回答1:


Try

$dir = "photos" ;
$destination = "thumb" ;
$images = scandir($dir);

foreach ( $images as $image ) {

    if(is_file($image))
    {
        $ext = pathinfo ( $dir . DIRECTORY_SEPARATOR . $image, PATHINFO_EXTENSION );
        $thumb = PhpThumbFactory::create ( $dir . DIRECTORY_SEPARATOR . $image );
        $thumb->adaptiveResize ( 100, 100 );
        $thumb->save ( $destination . DIRECTORY_SEPARATOR . $image, $ext );
        $thumb->show ();
    }
}


来源:https://stackoverflow.com/questions/10339863/how-to-automatically-copy-an-entire-directory-as-thumbnails-using-phpthumb-or-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!