【问题标题】:Cakephp 3 Image Upload to Cloudinary via APICakephp 3 图像通过 API 上传到 Cloudinary
【发布时间】:2015-12-25 17:53:48
【问题描述】:

我正要上传图片到 cloudinary - 一个图片 CDN 看起来 cloudinary.com,它支持所有语言和框架,包括 Cakephp 3,但对于 cakephp 3,我们不包含在他们的站点中的步骤。谁能告诉我轻松上传图片的步骤?

【问题讨论】:

    标签: image-uploading cakephp-3.0 cloudinary


    【解决方案1】:

    根据他们的网站,我正在提供上传程序。

    基础文档:

    http://cloudinary.com/documentation/php_integration#getting_started_guide

    第 1 步: 安装

    {
      "require": {
        "cloudinary/cloudinary_php": "dev-master"
      }
    }
    

    将以上内容添加到位于项目文件夹中的 composer.json。

    您可以使用 composer 来更新它并获取依赖项。导航到您的项目文件夹后,在 composer 中运行以下命令。

    php composer.phar update
    

    第 2 步: 在 Cake PHP 中安装。

    打开您的 AppController 并在 initialize 函数中添加以下内容

    它显然如下所示:

    public function initialize() {
            parent::initialize();
            $this->loadComponent('Flash');
    
            \Cloudinary::config(array( 
            "cloud_name" => "sample", 
            "api_key" => "874837483274837", 
            "api_secret" => "a676b67565c6767a6767d6767f676fe1" 
           ));
    
    }
    

    在上面你可以找到 cloudinary 配置,用你自己的凭据替换。

    要获取凭据,请点击下面的链接并登录,

    https://cloudinary.com/users/login

    第 3 步: 图片上传流程

    <?php 
    
    echo $this->Form->create('upload_form', ['enctype' => 'multipart/form-data']);
    echo $this->Form->input('upload', ['type' => 'file']);
    echo $this->Form->button('Change Image', ['class' => 'btn btn-primary']);
    echo $this->Form->end(); 
    
    ?>
    

    在您的视图文件中使用上述代码。 (可以根据需要修改)

    在你的控制器中,你可以通过以下方式使用,

    if (!empty($this->request->data['upload']['name'])) {
                $file = $this->request->data['upload']; //put the data into a var for easy use
                $cloudOptions = array("width" => 1200, "height" => 630, "crop" => "crop");
                $cloudinaryAPIReq = \Cloudinary\Uploader::upload($file["tmp_name"], $cloudOptions);
                $imageFileName = $cloudinaryAPIReq['url'];
            }
    

    您可以将 $imagefilename 保存在数据库中,在这里保存并重新填充完整的 url。

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2016-02-02
      • 2020-05-02
      • 2019-04-17
      • 2021-03-20
      • 2015-02-21
      • 2017-07-12
      • 2014-11-25
      • 1970-01-01
      相关资源
      最近更新 更多