【问题标题】:How to implement Imagine image library in Codeigniter如何在 Codeigniter 中实现 Imagine 图像库
【发布时间】:2015-01-27 09:55:17
【问题描述】:

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

现在我走了多远,我下载了 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
    }   
}

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

【问题讨论】:

    标签: php codeigniter php-imagine


    【解决方案1】:

    无论如何,经过一天的寻找,我终于找到了一种方法,感谢 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');
        }
    }
    

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

    希望有人会觉得这很有用。

    【讨论】:

    • 在哪里可以创建“创建文件并命名为imagine_autoload.php”
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多