1

我正在尝试创建一个新的 Imagick 实例:

  $original = new Imagick($array);

我在两种不同的情况下这样做。一种方法总是有效,另一种方法总是失败,但它们使用相同的确切数组(数组并不总是相同,但为了便于解释,我展示了一个恰好使用相同数组的示例)。这是var_dump每个数组的一个:

工作数组:

  array(3) { [0]=> string(20) "image_files/bbb0.jpg" [1]=> string(20) "image_files/bbb1.jpg" [2]=> string(20) "image_files/bbb2.jpg" } 

失败的数组:

  array(3) { [0]=> string(20) "image_files/bbb0.jpg" [1]=> string(20) "image_files/bbb1.jpg" [2]=> string(20) "image_files/bbb2.jpg" } 

如您所见,它们是相同的,那么为什么我的 PHP 会死在

$new = new Imagick($array);

我没有看到的第二个数组有什么不同吗?

编辑:这是构造失败的数组的代码:

$n = $_GET["n"];
$city = preg_replace("/[0-9]/", "", $n);
$num = preg_replace("/".$city."/","",$n);

// create an array to hold directory list
$results = array();
// create a handler for the directory
$directory = '../image_files';
$handler = opendir($directory);

while ($file = readdir($handler)) {
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
    // check with regex that the file format is what we're expecting and not something else
    if (preg_match("/^".$city."[1-9][0-9]\.jpg$/i",$file)) {
    if (preg_match("/^".$city.$num."\.jpg$/i",$file)) { 
        unlink("../image_files/".$file);
    } else {
        $results[] = "../image_files/" . $file;
    }
    } else if (preg_match("/^".$city."[0-9]\.jpg$/i",$file)) {
    if (preg_match("/^".$city.$num."\.jpg$/i",$file)) { 
        unlink("../image_files/".$file);
    } else {
        $results[] = "../image_files/" . $file;
    }
    } 
    }
}

sort($results);

$i = 0;
$newResults = array();
foreach( $results as $key => $value ) {
$old = $value;
//echo "old: " . $old . " ";
if (preg_match("/[1-9][0-9]/",$value)) {
    $newstr = preg_replace("/[1-9][0-9]/", $i."temp", $value);
} else if (preg_match("/[0-9]/",$value)) {
    $newstr = preg_replace("/[0-9]/", $i."temp", $value);
}

$newResults[] = $newstr;
//echo "new: " . $newstr . "<br>";
rename($old,$newstr);
    $i++;
}

// create an array to hold directory list
$results = array();
// create a handler for the directory
$directory = '../image_files';
$handler = opendir($directory);

while ($file = readdir($handler)) {
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
    $old = $file;
    $new = preg_replace("/temp/", "", $file);
        rename("../image_files/".$old,"../image_files/".$new);
    }
}

$finalResults = array();

foreach( $newResults as $key => $value ) {
    $newstr = str_replace("../", "", $value);
    $newstr = str_replace("temp","",$newstr);
    $finalResults[] = $newstr; 
}

sort($finalResults);

createMontage($finalResults,"-a",$city);
4

1 回答 1

0

问题出在 PHP 文件的目录结构中。

正在从基本目录创建“工作数组”。“失败数组”是从名为actions.

您正在运行的脚本存在于何处并不重要,重要的是加载该脚本的内容(即,它是通过include还是从根目录加载的require?或者是直接调用的www.example.com/scripts/script.php)。这将决定如何构建其他文件的路径。

于 2013-02-13T18:05:11.223 回答