forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudinaryField.php
More file actions
54 lines (45 loc) · 1.49 KB
/
Copy pathCloudinaryField.php
File metadata and controls
54 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Manages access to a cloudinary image as a field
*/
require_once 'Cloudinary.php';
require_once 'Uploader.php';
require_once 'PreloadedFile.php';
class CloudinaryField {
private $_identifier = NULL;
private $verifyUpload = false;
public function __construct($identifier = "") {
$this->_identifier = $identifier;
}
public function __toString() {
return explode("#", $this->identifier())[0];
}
public function identifier() {
return $this->_identifier;
}
public function url($options = array()) {
if (!$this->_identifier) {
// TODO: Error?
return;
}
return cloudinary_url($this, $options);
}
public function upload($file, $options = array()) {
$options['return_error'] = false;
$ret = \Cloudinary\Uploader::upload($file, $options);
$preloaded = new \Cloudinary\PreloadedFile(\Cloudinary::signed_preloaded_image($ret));
if ($this->verifyUpload && !$preloaded.is_valid()) {
throw new \Exception("Error! Couldn't verify cloudinary response!");
}
$this->_identifier = $preloaded->extended_identifier();
}
public function delete() {
$options['return_error'] = false;
$ret = \Cloudinary\Uploader::destroy($this->_identifier);
unset($this->_identifier);
}
public function verify() {
$preloaded = new \Cloudinary\PreloadedFile($this->_identifier);
return $preloaded->is_valid();
}
}