新的Imagick()不接受某个阵列

时间:2013-02-13 08:59:04

标签: php arrays imagick

我正在尝试创建一个新的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);

1 个答案:

答案 0 :(得分:0)

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

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

你运行的脚本在哪里存在并不重要,加载那个脚本是什么问题(即它是通过include还是require从根目录加载的?或者是被调用的直接www.example.com/scripts/script.php)。这将决定如何构建其他文件的路径。