1

我正在尝试将 Imagine 图像库添加到 Codeigniter 应用程序中,但这是我第一次遇到我不明白的“命名空间”和“使用”概念。大多数库通常需要一个包含、要求等...以使它们与您的框架一起工作,但我很难尝试实现这种“命名空间”、“使用”方法。

现在我走了多远,我下载了 Lib,将 Imagine 文件夹放在 CI 的库文件夹中,在那里我有另一个图像 Lib 调用 wideimage,我可以毫无问题地适应 CI。我尝试有创意并加载库文件,就像在 CI 中加载任何常规库文件一样。

这就是问题开始的地方,我开始收到很多错误,例如找不到类得到错误行,我立即认为它可能需要具有该类的其他文件现在可以解决它一直存在的一些错误给我,但就像每次加载新的 lib 文件时都会出现另一个错误,即使存在具有主类的文件,一些错误也不会消失。

现在我确实找到了一篇关于如何设置 SPL 自动加载的文章,我得到了以下代码

set_include_path('/application/libraries/Imagine' . PATH_SEPARATOR . get_include_path());
function imagineLoader($class) {
    $path = $class;
    $path = str_replace('\\', DIRECTORY_SEPARATOR, $path) . '.php';
    if (file_exists($path)) {
        include $path;
    }
}
spl_autoload_register('\imagineLoader');

但从来没有让它工作,在这种情况下,它给了我一个 CI 类或文件找不到的错误。

现在再次问我的问题,有没有办法将这个 Imagine 图像库实现到 Codeigniter 中?

通过常规库加载器或通过自动加载文件加载它?

像这样的东西;

class Test extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library('imagine');
    }

    public function index()
    {
        // Do some cool things with the image lib functions
    }   
}

好吧,我非常感谢任何人对此的帮助,同时我将继续浏览网络,看看是否能找到答案。

4

1 回答 1

3

无论如何,经过一天的寻找,我终于找到了一种方法,并且感谢 simplepie 提要解析器,我知道我过去曾看到过类似的代码。

无论如何,我实际上查看了加载类以使用 simplepie 的 simplepie autoloader.php 文件,我只是对代码进行了一些小调整,以便能够在 Codeigniter 中使用 Imagine Lib。

所以这里是步骤和代码:

1 下载 Imagine 库: https ://github.com/avalanche123/Imagine

2 将 Imagine 文件夹复制到您的 CI 应用程序库文件夹中:application/libraries

3 创建一个文件并将其命名为imagine_autoload.php

4 将以下代码添加到imagine_autoload.php 文件中

set_include_path('/application/libraries/Imagine' . PATH_SEPARATOR . get_include_path());

function imagineLoader($class) {
    $path = $class;
    $path = str_replace('\\', DIRECTORY_SEPARATOR, $path) . '.php';
    if (strpos($class, 'Imagine') !== 0)
    {
        return;
    }
    include_once $path;
}

spl_autoload_register('\imagineLoader');

现在到控制器上有趣的部分,请执行以下操作:

    require_once('./application/libraries/imagine_autoloader.php');
    // Here you can use the imagine imagine tools
    use Imagine\Image\Box;
    use Imagine\Image\Point;

    class Testing extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index($offset = 0)
    {    
        $imagine = new \Imagine\Gd\Imagine();

        $image = $imagine->open('/path/to/image.jpg');
        $thumbnail = $image->thumbnail(new Imagine\Image\Box(100, 100));
        $thumbnail->save('/path/to/thumnail_image.jpg');
    }
}

现在去看看你保存新图像的位置,你会发现一个重新调整大小的新图像。

希望有人会发现这很有用。

于 2015-01-27T10:50:02.607 回答