0

名前を必要とする画像のサイズを変更する機能があります。今私がやりたいことは、スクリプトをディレクトリに配置して一度実行するだけで、それらすべての画像に対してその関数を実行する必要があります。

別の投稿で、 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/');
    }
}
?>
4

1 に答える 1

1

これを解決するには2つの方法があります。

  1. PHP の「マジック定数」の 1 つを使用__PATH__して、その現在のファイルへのパスを表示できます。ただし、すべての PHP インストールにこれが組み込まれているわけではありません。

  2. この関数getcwd()は、現在見ているディレクトリを返しますが、ファイルが存在する場所ではない可能性があります。これを試すことができます:

     <?php
     chdir( dirname( __FILE__ ) );
     echo getcwd();
     ?>
    

ディレクトリをプルしたら、そのままスクリプトにフィードできます。お役に立てれば。

于 2013-07-21T09:37:31.033 に答える