2015-09-28 69 views

回答

3

根據他們的網站,我提供上傳程序。

基本文檔:

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

第1步: 安裝

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

添加上述您composer.json位於項目文件夾中。

您可以使用作曲家來更新它並獲取依賴關係。導航到項目文件夾後,在作曲家中運行以下內容。

php composer.phar update 

步驟2: 安裝在蛋糕PHP。

打開你的AppController,並添加在以下初始化功能

它顯然如下所示:

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']; 
     } 

可以節省$映像文件名稱在你的數據庫,在這裏完整的URL被保存並重新填充。