PHP code example of cloudinary-labs / cloudinary-laravel
1. Go to this page and download the library: Download cloudinary-labs/cloudinary-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cloudinary-labs / cloudinary-laravel example snippets
/**
* Using the Cloudinary Facade
*/
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;
// access the admin api
(https://cloudinary.com/documentation/admin_api)
Cloudinary::admin();
// access the search api
(https://cloudinary.com/documentation/search_api)
Cloudinary::search();
// access the upload api
(https://cloudinary.com/documentation/image_upload_api_reference)
Cloudinary::uploadApi();
// Upload an Image File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::upload($request->file('file')->getRealPath())->getSecurePath();
// Upload a Video File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::uploadVideo($request->file('file')->getRealPath())->getSecurePath();
// Upload any File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::uploadFile($request->file('file')->getRealPath())->getSecurePath();
// get url from a file
$url = Cloudinary::getUrl($publicId);
/**
* This package also exposes a helper function you can use if you are not a fan of Facades
* Shorter, expressive, fluent using the
* cloudinary() function
*/
// access the admin api
(https://cloudinary.com/documentation/admin_api)
cloudinary()->admin();
// access the search api
(https://cloudinary.com/documentation/search_api)
cloudinary()->search();
// access the upload api
(https://cloudinary.com/documentation/image_upload_api_reference)
cloudinary()->uploadApi();
// Upload an image file to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->upload($request->file('file')->getRealPath())->getSecurePath();
// Upload a video file to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadVideo($request->file('file')->getRealPath())->getSecurePath();
// Upload any file to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadFile($request->file('file')->getRealPath())->getSecurePath();
// Upload an existing remote file to Cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadFile($remoteFileUrl)->getSecurePath();
// get url from a file
$url = cloudinary()->getUrl($publicId);
/**
* You can also skip the Cloudinary Facade or helper method and laravel-ize your uploads by simply calling the
* storeOnCloudinary() method on the file itself
*/
// Store the uploaded file on Cloudinary
$result = $request->file('file')->storeOnCloudinary();
// Store the uploaded file on Cloudinary
$result = $request->file->storeOnCloudinary();
// Store the uploaded file in the "lambogini" directory on Cloudinary
$result = $request->file->storeOnCloudinary('lambogini');
// Store the uploaded file in the "lambogini" directory on Cloudinary with the filename "prosper"
$result = $request->file->storeOnCloudinaryAs('lambogini', 'prosper');
$result->getPath(); // Get the url of the uploaded file; http
$result->getSecurePath(); // Get the url of the uploaded file; https
$result->getSize(); // Get the size of the uploaded file in bytes
$result->getReadableSize(); // Get the size of the uploaded file in bytes, megabytes, gigabytes or terabytes. E.g 1.8 MB
$result->getFileType(); // Get the type of the uploaded file
$result->getFileName(); // Get the file name of the uploaded file
$result->getOriginalFileName(); // Get the file name of the file before it was uploaded to Cloudinary
$result->getPublicId(); // Get the public_id of the uploaded file
$result->getExtension(); // Get the extension of the uploaded file
$result->getWidth(); // Get the width of the uploaded file
$result->getHeight(); // Get the height of the uploaded file
$result->getTimeUploaded(); // Get the time the file was uploaded
/**
* You can also check if a file exists
*/
$url = Storage::disk('cloudinary')->fileExists($publicId);
namespace App;
use Illuminate\Database\Eloquent\Model;
use CloudinaryLabs\CloudinaryLaravel\MediaAlly;
class Page extends Model
{
use MediaAlly;
...
}
/**
* How to attach a file to a Model by model creation
*/
$page = Page::create($this->request->input());
$page->attachMedia($file); // Example of $file is $request->file('file');
/**
* How to attach a file to a Model by retrieving model records
*/
$page = Page::find(2);
$page->attachMedia($file); // Example of $file is $request->file('file');
/**
* How to retrieve files that were attached to a Model
*/
$filesBelongingToSecondPage = Page::find(2)->fetchAllMedia();
/**
* How to retrieve the first file that was attached to a Model
*/
$fileBelongingToSecondPage = Page::find(2)->fetchFirstMedia();
/**
* How to retrieve the last file that was attached to a Model
*/
$fileBelongingToSecondPage = Page::find(2)->fetchLastMedia();
/**
* How to replace/update files attached to a Model
*/
$page = Page::find(2);
$page->updateMedia($file); // Example of $file is $request->file('file');
/**
* How to detach a file from a Model
*/
$page = Page::find(2);
$page->detachMedia($file) // Example of $file is $request->file('file');
/**
* How to resize an image to a specific width and height, and crop it using 'fill' mode
*/
$options = [
'transformation' => [
[
'width' => 200, // Desired width
'height' => 200, // Desired height
'crop' => 'fill', // Crop mode (you can change this to 'fit' or other modes)
],
],
]
$page->attachMedia($file, $options); // Example of $file is $request->file('file');
/**
* How to crop an image to a specific width and height.
*/
$options = [
'transformation' => [
[
'width' => 200, // Desired width
'height' => 200, // Desired height
'crop' => 'crop', // Crop mode
],
],
]
$page->attachMedia($file, $options); // Example of $file is $request->file('file');
/**
* How to rotate an image by a specific degree.
*/
$options = [
'transformation' => [
[
'angle' => 45, // Rotation angle
],
],
]
$page->attachMedia($file, $options); // Example of $file is $request->file('file');
/**
* How to apply a filter to an image.
*/
$options = [
'transformation' => [
[
'effect' => 'grayscale', // Filter effect
],
],
]
$page->attachMedia($file, $options); // Example of $file is $request->file('file');
/**
* How to overlay text on an image.
*/
$options = [
'transformation' => [
[
'overlay' => [
'font_family' => 'arial',
'font_size' => 24,
'text' => 'Hello World',
],
],
],
]
$page->attachMedia($file, $options); // Example of $file is $request->file('file');
<x-cld-image public-id="prosper" width="300" height="300"></x-cld-image> // Blade Image Component for displaying images
<x-cld-video public-id="awesome"></x-cld-video> // Blade Video Component for displaying videos
...
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;
...
return [
/*
|--------------------------------------------------------------------------
| Cloudinary Configuration
|--------------------------------------------------------------------------
|
| An HTTP or HTTPS URL to notify your application (a webhook) when the process of uploads, deletes, and any API
| that accepts notification_url has completed.
|
|
*/
'notification_url' => env('CLOUDINARY_NOTIFICATION_URL'),
/*
|--------------------------------------------------------------------------
| Cloudinary Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Cloudinary settings. Cloudinary is a cloud hosted
| media management service for all file uploads, storage, delivery and transformation needs.
|
|
*/
'cloud_url' => env('CLOUDINARY_URL'),
/**
* Upload Preset From Cloudinary Dashboard
*
*/
'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET'),
/**
* Route to get cloud_image_url from Blade Upload Widget
*/
'upload_route' => env('CLOUDINARY_UPLOAD_ROUTE'),
/**
* Controller action to get cloud_image_url from Blade Upload Widget
*/
'upload_action' => env('CLOUDINARY_UPLOAD_ACTION'),
];