| 1 |
<?php |
| 2 |
namespace Depicter\Media; |
| 3 |
|
| 4 |
/** |
| 5 |
* Media File Class |
| 6 |
* |
| 7 |
* @package Depicter\Media |
| 8 |
*/ |
| 9 |
class File |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Original width in pixels. |
| 13 |
* |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
protected $orig_w; |
| 17 |
|
| 18 |
/** |
| 19 |
* Original height in pixels. |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
protected $orig_h; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Path to the file to load |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $file = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Information about the file path |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $fileInfo = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor. |
| 42 |
* |
| 43 |
* @param string $path Path to the file or attachment id to load. |
| 44 |
*/ |
| 45 |
public function __construct( $path ){ |
| 46 |
if( is_numeric( $path ) ){ |
| 47 |
$path = get_attached_file( $path ); |
| 48 |
} |
| 49 |
|
| 50 |
$this->file = $path; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Sets initial values |
| 55 |
*/ |
| 56 |
public function reset(){ |
| 57 |
$this->orig_w = $this->getSize('width'); |
| 58 |
$this->orig_h = $this->getSize('height'); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get current image size |
| 63 |
* |
| 64 |
* @param string $dimension |
| 65 |
* |
| 66 |
* @return array|null |
| 67 |
*/ |
| 68 |
public function getSize( $dimension = null ){ |
| 69 |
if( ! $dimension ){ |
| 70 |
return getimagesize( $this->file ); |
| 71 |
} |
| 72 |
list( $width, $height, $type, $attr ) = getimagesize( $this->file ); |
| 73 |
|
| 74 |
if( isset( $$dimension ) ){ |
| 75 |
return $$dimension; |
| 76 |
} |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Retrieves information about a file path |
| 82 |
* |
| 83 |
* @param string $param Key of file info |
| 84 |
* |
| 85 |
* @return array|string |
| 86 |
*/ |
| 87 |
public function getFileInfo( $param = null ){ |
| 88 |
if( empty( $this->fileInfo ) ){ |
| 89 |
$this->fileInfo = pathinfo( $this->file ); |
| 90 |
} |
| 91 |
if( $param && !empty( $this->fileInfo[ $param ] ) ){ |
| 92 |
return $this->fileInfo[ $param ]; |
| 93 |
} |
| 94 |
return $this->fileInfo; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieves the file extension |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function getExtension(){ |
| 103 |
if( ! empty( $this->getFileInfo('extension' ) ) ){ |
| 104 |
return $this->getFileInfo('extension'); |
| 105 |
} |
| 106 |
return ''; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|