名前を必要とする画像のサイズを変更する機能があります。今私がやりたいことは、スクリプトをディレクトリに配置して一度実行するだけで、それらすべての画像に対してその関数を実行する必要があります。
別の投稿で、 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/');
}
}
?>