-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
104 lines (93 loc) · 2.03 KB
/
Copy pathImage.php
File metadata and controls
104 lines (93 loc) · 2.03 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* @package maximalist\GetImages
*/
namespace maximalist\GetImages;
/**
* Loads image data and save it to file system
*
* @since 0.2
*
* @property string $url Image URL
* @property string $name Image file name
* @property string $data Image data
*
* @method void load()
* @method void save( string $path )
* @method string getData()
* @method string getType()
* @method string getName()
*
* @uses finfo to get image file type
*
* @throws Exception
*
* @todo Name cleanup and uniqueness check
*/
class Image {
private $url;
private $name = null;
private $data = null;
/**
* @param string $url Image URL
*/
function __construct( string $url ) {
if( filter_var( $url, FILTER_VALIDATE_URL ) === false )
throw new \Exception( "Invalid URL" );
$this->url = $url;
}
/**
* Load image data from URL
*/
function load() {
$this->data = file_get_contents( $this->url );
if( $this->data === false )
throw new \Exception( "Can't load ".$this->url );
$this->name = basename( $this->url );
}
/**
* Save image data to file
*
* @param string $path Path to store image file
*/
function save( string $path ) {
// if( is_file( $path ) )
// throw new \Exception( "File with the target directory name '".$path."' exist" );
if( !is_dir( $path ) && !mkdir( $path, 0777, true ) )
throw new \Exception( "Can't create target directory ".$path );
if( $this->data === null )
$this->load();
$name = $path.DIRECTORY_SEPARATOR.$this->name;
if( file_put_contents( $name, $this->data ) === false )
throw new \Exception( "Can't write ".$name );
}
/**
* Get image data
*
* @return string Image data
*/
function getData() {
if( $this->data === null )
$this->load();
return $this->data;
}
/**
* Get mime type
*
* @return string Mime type
*/
function getType() {
if( $this->data === null )
$this->load();
$finfo = new \finfo( FILEINFO_MIME );
return $finfo->buffer( $this->data );
}
/**
* Get file name
*
* @return string File name
*/
function getName() {
return $this->name;
}
}