[英]search all the files and directories to create thumbnails in php
我想搜索文件並從jpg文件創建圖像。 有代碼:
$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);
}
問題是:例如,當我們有這些文件時:
已加載文件image1.jpg和image2.jpg,但未加載image3.jpg-好像FOLDER 1.1被視為文件。 我該如何解決?
$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
}
}
這將為您提供目錄和所有子目錄中的所有文件,然后您可以根據需要調整每個圖像的大小。 我自己總是使用imagmagick,因為我認為結果通常要比GD更好,但顯然GD更常見。
我將使用兩個單獨的函數:
然后,您可以分別測試和調整這兩個功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.