如何调整此脚本以使用 Glob 而不是 1 个文件

How to adjust this script to use Glob instead of 1 file

我在阅读 Whosebug 上的其他帖子的基础上创建了这个脚本。该脚本从原始图像创建缩略图并将其添加到文件夹中。源图像位于我的本地服务器上,因此 Glob 可以工作,但我不确定如何调整此脚本,以便它 运行 对文件夹 (Glob) 中的所有文件执行此功能。我意识到这可能会占用大量内存,但我只需要在几个文件夹上执行几次,我就会完成它。

以防万一你质疑我包含 $height2 的原因,这是我想出的一个小技巧,我们在创建缩略图的同时保持纵横比,然后只保存顶部 250 像素,以便拇指为 250 x 250 但未扭曲(拉伸)。效果很好。

我只需要对源文件夹中的所有图像进行调整即可。

任何帮助都会很棒。请记住,我是一名前端开发人员(html 和 css),并不擅长 PHP。仅此脚本就让我永远无法工作哈哈。

如果有人能帮我调整一个完整的文件夹,那就太好了。

foreach(glob('SourceFolder/*.jpg', GLOB_NOSORT) as $url); {

Thumbnail ($url, "DestinationFolder/*.jpg");

function Thumbnail($url, $filename, $width = 250, $height = true, $height2 = 250) {

// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));

// calculate resized ratio
// Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
$height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;

// create image 
$output = ImageCreateTrueColor($width, $height2);
ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));

// save image
ImageJPEG($output, $filename, 100); 
} 
}   

阅读在线 PHP 手册后,我能够自行调整代码。

最终代码:

foreach (glob("SourceFolder/*.jpg") as $url) {

$destdir = "Destination/" . basename($url, null);

$width = 250;
$height = true;

 // download and create gd image
 $image = ImageCreateFromString(file_get_contents($url));

 // calculate resized ratio
 // Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
 $height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;

 // create image 
 $output = ImageCreateTrueColor($width, $height);

 ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));

 // save image
 ImageJPEG($output, $destdir, 100); 

}

此解决方案使用 opendir 和 readdir。我发现这种方法可以 处理包含更多文件的文件夹。

// Set desired height and width
$desiredWidth = 250;
$desiredHeight = true;
// Set source and destination folders
$srcFolder = "SourceFolder";
$destFolder = "Destination";
// This is more memory efficient than using glob()
$dh = opendir($srcFolder);
if ( ! $dh ) {
    die('Unable to open folder ' . $srcFolder);
}
while($filename = readdir($dh) ) {
    // only process jpg files.
    if ( preg_match('/.jpg$/', $filename) ) {
        $inputFile = sprintf('%s/%s', $srcFolder, $filename);
        $outputFile = sprintf('%s/%s', $destFolder, $filename);
        echo "file: $filename\n";
        echo "input: $inputFile\n";
        echo "output: $outputFile\n";
        $inputImage = @ImageCreateFromJpeg($inputFile);
        if ( $inputImage ) {
            // calculate resized ratio
            // Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
            $height = $desiredHeight === true ? (ImageSY($inputImage) * $desiredWidth / ImageSX($inputImage)) : $desiredHeight;

            // create image 
            $outputImage = ImageCreateTrueColor($desiredWidth, $height);

            ImageCopyResampled($outputImage, $inputImage, 0, 0, 0, 0, $desiredWidth, $height, ImageSX($inputImage), ImageSY($inputImage));

            // save image
            ImageJPEG($outputImage, $outputFile, 100);
            ImageDestroy($inputImage);
            ImageDestroy($outputImage);
        } else {
            echo "Could not process file\n";
        }

    }
}
closedir($dh);