cloudinary / cloudinary_cake_php
Cloudinary Cake PHP SDK
Requires
- php: >=5.2.0
- ext-curl: *
- ext-json: *
- cloudinary/cloudinary_php: >=1.0.8
This package is auto-updated.
Last update: 2024-09-04 23:17:43 UTC
README
Cloudinary CakePHP 插件提供无缝集成 Cloudinary 服务与 CakePHP 框架,以简单高效地管理应用图像
探索PhotoAlbumCake 示例了解用法。
要求
- PHP 5.3 或更高版本
- CakePHP 2.x
安装
Composer
-
为 myapp 创建一个新目录
mkdir myapp cd myapp
-
使用 composer 安装 CakePHP(基于CakePHP 书籍)
-
设置 Composer 并获取 CakePHP
echo '{}' > composer.json composer config vendor-dir Vendor composer config repositories.0 pear 'http://pear.cakephp.org' composer require 'pear-cakephp/cakephp:>=2.4.0'
-
烘焙一个新的项目
Vendor/bin/cake bake project .
-
您可以在
webroot/index.php
中定义CAKE_CORE_INCLUDE_PATH
为相对路径,如烹饪书籍中所建议,添加以下代码define( 'CAKE_CORE_INCLUDE_PATH', ROOT . DS . APP_DIR . '/Vendor/pear-pear.cakephp.org/CakePHP' );
-
将以下行添加到
Config/bootstrap.php
// Load composer autoload. require APP . '/Vendor/autoload.php'; // Auto load CloudinaryCake plugin \CloudinaryCakeLoader::load();
-
-
安装 Cloudinary CakePHP
composer require 'cloudinary/cloudinary_cake_php:>=1.0.0'
-
使用
CLOUDINARY_URL
环境变量或Config/CloudinaryPrivate.php
配置文件配置 Cloudinary
手册
-
创建一个 CakePHP 项目
-
从这里下载 cloudinary_php
-
将 cloudinary_php 存档提取到
vendors
库中 -
从这里下载 cloudinary_cake_php
-
将 cloudinary_cake_php 存档提取到
vendors
库中 -
配置 cloudinary
- 环境变量 -
export CLOUDINARY_URL = "cloudinary://API_KEY:API_SECRET@CLOUD_NAME"
(检查 Cloudinary 控制台中的设置) - 使用
vendors/cloudinary_php/samples/PhotoAlbumCake/Config/CloudinaryPrivate.php.sample
创建app/Config/CloudinaryPrivate.php
- 环境变量 -
-
通过添加以下行到
app/Config/bootstrap.php
来加载 cloudinary 插件// Load plugin CakePlugin::load('CloudinaryCake', array('bootstrap' => true, 'routes' => false, 'path' => ROOT . DS 'vendors' . DS 'cloudinary_php' . DS . 'cake_plugin' . DS . 'CloudinaryCake' . DS)); // required when using `CloudinaryPrivate.php` for cloudinary configuration Configure::load('CloudinaryPrivate'); \Cloudinary::config(Configure::read('cloudinary'));
用法
CloudinaryBehavior
CloudinaryBehavior 为 CakePHP 模型添加 Cloudinary 支持。它可以帮助在模型的简单文本字段中存储云存储图像的引用。
设置
假设您有一个具有 cloudinaryIdentifier
文本字段以存储云存储图像引用的 Photo
模型 - 您可以将以下代码添加到模型中
Models/photo.php
:
[...]
class Photo extends AppModel {
public $actsAs = array('CloudinaryCake.Cloudinary' => array('fields' => array('cloudinaryIdentifier')));
[...]
}
用法
这会使您能够将 cloudinaryIdentifier
作为 CloudinaryField 访问。以下是一个示例控制器代码 -
Controller/PhotosController.php
:
class PhotosController extends AppController {
[...]
// set the specified Photo's image to the default one
public function set_default_image($id) {
$options = array('conditions' => array('Photo.' . $this->Photo->primaryKey => $id));
$photo = $this->Photo->find('first', $options);
$photo['Photo']['cloudinaryIdentifier']->upload(DEFAULT_IMAGE_PATH);
$this->Photo->save($photo);
}
[...]
// Creates a new image from post data. Sets $image_url to the cloudinary url of the image with the given transformation.
public function add() {
$this->Photo->create();
$success = $this->Photo->save($this->request->data);
if ($success) {
$image_url = $this->Photo->data['Photo']['cloudinaryIdentifier']->url(array(
"width" => 100, "height" => 100, "crop" => "fill"));
}
$this->set('photo', $this->Photo->data);
}
[...]
}
CloudinaryHelper
CloudinaryHelper 是 CakePHP InputHelper 的扩展。它可以用来加载 cloudinary_js、展示图像、创建带有图像输入的表单等。
设置
您可以使用两种方法加载 CloudinaryHelper
Controller/PhotosController.php
:
[...]
class PhotosController extends AppController {
// Replace the FormHelper with CloudinaryHelper (recommended - accessible as $this->Form)
public $helpers = array('Html', 'Form' => array('className' => 'CloudinaryCake.Cloudinary'));
// Add CloudinaryHelper in addition to the default FormHelper (accessible as $this->Cloudinary instead of $this->Form)
//public $helpers = array('Html', 'Form', 'CloudinaryCake.Cloudinary');
[...]
}
用法
然后在控制器的任何视图中使用它
View/Layouts/default.ctp
:
[...]
<head>
[...]
# Include cloudinary_js dependencies (requires jQuery)
echo $this->Form->cloudinary_includes();
# Setup cloudinary_js using the current cloudinary_php configuration
echo cloudinary_js_config();
[...]
</head>
[...]
View/Photos/add.ctp
:
[...]
<span><?php echo __('Current Photo:'); ?></span>
<?php echo $this->Form->cl_image_tag($photo['Photo']['cloudinaryIdentifier'],
array("width" => 60, "height" => 60, "crop" => "thumb", "gravity" => "face")); ?>
<?php echo $this->Form->create('Photo', array('type' => 'file')); ?>
<legend><?php echo __('Edit Photo'); ?></legend>
<?php
echo $this->Form->input('id');
# Backend upload:
echo $this->Form->input('cloudinaryIdentifier');
# Direct upload:
#echo $this->Form->input('cloudinaryIdentifier', array("type" => "direct_upload"));
?>
<?php echo $this->Form->end(__('Submit')); ?>
[...]