我有一个调整图像大小的功能,这需要他们的名字。现在我要做的只是将一个脚本放在一个目录中,运行一次,它应该在所有这些图像上运行该函数。
在另一篇文章中,我找到了有关DirectoryIterator的一些信息,但您不能让目录为空以类似于当前文件夹。我该怎么做呢?
以下代码适用于指定文件夹(因此不是当前文件夹)
<?php
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path){
$s_path = trim($s_path);
$o_path = trim($o_path);
$save = $s_path . $save;
$file = $o_path . $file;
$attrib = getimagesize($file);
$width = $attrib[0];
$height = $attrib[1];
if(($width>$t_w) || ($height>$t_h)){
$r1 = $t_w/$width;
$r2 = $t_h/$height;
if($r1<$r2){
$size = $t_w/$width;
}else{
$size = $t_h/$height;
}
}else{
$size=1;
}
$modwidth = $width * $size;
$modheight = $height * $size;
$tn = imagecreatetruecolor($modwidth, $modheight);
switch($attrib['mime']){
case "image/gif":
$image = imagecreatefromgif($file);
break;
case "image/jpeg":
$image = imagecreatefromjpeg($file);
break;
case "image/png":
$image = imagecreatefrompng($file);
break;
}
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100);
return;
}
$dir = new DirectoryIterator("files/");
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$fullname = $fileinfo->getFilename();
Resize_Image($fullname,$fullname,1366,767,'files/','files/');
}
}
?>