image-uploadingcakephp-3.0cloudinary

Cakephp 3 Image Upload to Cloudinary via API


I was about to upload images to cloudinary - a Image CDN look cloudinary.com, It supports all languages and frameworks including Cakephp 3, but for cakephp 3 the steps we're not included in their site. Can anyone possibly say me steps for uploading images in ease?


Solution

  • As per their site, I'm providing procedures for uploading.

    Base Documentation:

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

    Step 1: Installation

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

    Add the above to your composer.json located in your project folder.

    You can use composer to update it and fetch dependencies. Run the following in composer after navigating to your project folder.

    php composer.phar update
    

    Step 2: Installation in Cake PHP.

    Open your AppController and add the following in the initialize function

    It apparently looks like the following:

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

    In the above you can find the cloudinary configuration, replace with your own credentials.

    To fetch credentials, follow the link below and sign in,

    https://cloudinary.com/users/login

    Step 3: Image Upload Procedure

    <?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(); 
    
    ?>
    

    Use the above code in your view file. (you can modify as you need)

    In your controller, you can use in the following way,

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

    You can save the $imagefilename in your database, in here the complete url is saved and repopulated.