[英]search all the files and directories to create thumbnails in php
I want to search through files and create images from jpg files. 我想搜索文件并从jpg文件创建图像。 There's the code:
有代码:
$dir2 = opendir($direction2);
$dir = opendir($direction);
function create_fcat($direction, $width, $direction2, $dir) {
while(false !== ($fn = readdir($dir))) {
$n_dir = $direction.'/'.$fn;
if (is_dir($n_dir) && $fn != '.' && $fn != '..') {
if ($handle = opendir($n_dir)) {
create_fcat($fn, $width, $direction2, $handle);
}
} elseif ($fn != '.' && $fn != '..') {
$ext = strtolower(substr($fn,strlen($fn)-3));
if ($ext == 'jpg') {
if ($img = imagecreatefromjpeg( $direction.'/'.$fn )) {
$width_original = imagesx( $img );
$height_original = imagesy( $img );
if ($width_original > $height_original) {
$new_width = $width;
$new_height = floor( $height_original * ( $width / $width_original ) );
} else {
$new_height = $width;
$new_width = floor( $width_original * ( $width / $height_original ) );
}
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_original, $height_original );
imagejpeg( $tmp_img, $direction2.'/large/'.$fn );
} else {
echo '<p>The file cannot be loaded.</p>';
}
}
}
}
closedir($dir);
}
The problem is: when for example we have these files: 问题是:例如,当我们有这些文件时:
Files image1.jpg and image2.jpg are loaded, but image3.jpg is not - it looks like FOLDER 1.1 is treated as file. 已加载文件image1.jpg和image2.jpg,但未加载image3.jpg-好像FOLDER 1.1被视为文件。 How can I fix it?
我该如何解决?
$path = '/your/top/level/dir';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($objects as $fileinfo) {
if ($fileinfo->isFile()) {
echo $fileinfo->getFilename() . "\n";
$fullpath = $fileinfo->getPathname(); // full path to image for resizing
}
}
This would give you all the files in a directory and all sub-directories, you can then resize each image as you wish. 这将为您提供目录和所有子目录中的所有文件,然后您可以根据需要调整每个图像的大小。 I always use imagmagick for this myself as i think the results are generally better than with GD but obviously GD is more commonly installed.
我自己总是使用imagmagick,因为我认为结果通常要比GD更好,但显然GD更常见。
I would use two separate functions: 我将使用两个单独的函数:
Then you can test and tweak both functions separately. 然后,您可以分别测试和调整这两个功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.