Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP显示目录中的所有图像_Php_Zend Framework2 - Fatal编程技术网

使用PHP显示目录中的所有图像

使用PHP显示目录中的所有图像,php,zend-framework2,Php,Zend Framework2,我试图在视图中显示所有已调整大小的图像。尽管目录中不止一个图像,但它只显示一个图像。这是我的密码: namespace Application\Model; class Images { /** * Gets the files from the specified directory * @return array */ public static function getFilesFromDir() { $iterat

我试图在视图中显示所有已调整大小的图像。尽管目录中不止一个图像,但它只显示一个图像。这是我的密码:

namespace Application\Model;

class Images 
{
    /**
     * Gets the files from the specified directory
     * @return array
     */
    public static function getFilesFromDir()
    {
        $iterator = new \DirectoryIterator($_SERVER['DOCUMENT_ROOT'] . '/test_image/');

        $holder = array();

        foreach ($iterator as $finfo) {
            if ($finfo->isFile()) {
                $holder[$finfo->getPath()] = $finfo->getFilename();
            }
        }

        return $holder;
    }


    /**
     * Resizes images in a directory
     */
    public static function resizeImages() 
    {
        $percent = 0.5; // scales the image to half its original size


        foreach (self::getFilesFromDir() as $key => $value) {
            $img_size = getimagesize($key . '/' . $value);

            // get the original height and width of the image
            $o_width = $img_size[0];
            $o_height = $img_size[1];

            // set the new width
            $new_width = 500; // 500 pixels, can changed if need be

            if ($o_width != $new_width) {
                $new_height_calc = $new_width / $o_width;

                $new_height = $new_height_calc * $o_height;

                // now create the resized image
                $new_image = imagecreatetruecolor($new_width, $new_height);

                $image = imagecreatefromjpeg($key . '/' . $value);

                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $o_width, $o_height);

                imagejpeg($new_image, $_SERVER['DOCUMENT_ROOT'] . '/test_image/resized_images/' . $value, 100);
            }
        }
    }

    /**
     * gets the resized images located in resized images directory
     * @return array
     */
    public static function getResizedImages()
    {
        $iterator = new \DirectoryIterator($_SERVER['DOCUMENT_ROOT'] . '/test_image/resized_images/');

        $holder = array();

        foreach ($iterator as $finfo) {
            if ($finfo->isFile()) {
                $holder[] = $finfo->getFilename();
            }
        }

        return $holder;
    }
}
在控制器中:

 public function indexAction()
 {
    Images::resizeImages();

    $images = function() {
        foreach (Images::getResizedImages() as $value) {
            $img[] = $value;
        }

        return array_values($img);
    };

    return new ViewModel(array('images' => $images()));
}
任何帮助都将不胜感激

谢谢

为简洁起见

getFilesFromDir()
方法的内部循环中,您正在重置数组值,因为它的键/索引不是唯一的。 而不是
$holder[$finfo->getPath()]=$finfo->getFilename()使用
$holder[$finfo->getPath()][]=$finfo->getFilename()
创建多维数组。
结果将是一个数组,其中第一个键包含路径,然后是一个文件数组。

调整大小的图像是否有效?IsFile仅在以下条件下返回;如果文件存在并且是常规文件(不是链接或目录),则返回TRUE,否则返回FALSEyes,它们是.jpeg文件,但页面上只显示一个图像(test_image目录中有两个,因此我有点不确定为什么这两个都没有调整大小并放在调整大小的_image目录中。根据更新的帖子,您是否覆盖了数组键?
$holder[$finfo->getPath()]
getFilesFromDir
如果有帮助的话,这里有一个图像:路径将是相同的,但其键上的数组值会被覆盖,因此基本上您会保留一个数组键,因为它的键是相同的。要使其唯一,您可以执行类似于
$holder[$finfo->getPath()]=$finfo->getFilename()的操作
这样,您就有了一个多维数组,其中包含每个目录的多个文件