| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('WPO_Image_Utils')) : |
| 6 |
|
| 7 |
class WPO_Image_Utils { |
| 8 |
|
| 9 |
/** |
| 10 |
* Get image paths to resized attachment images. |
| 11 |
* |
| 12 |
* @param int $attachment_id |
| 13 |
* @return array |
| 14 |
*/ |
| 15 |
public static function get_attachment_files($attachment_id) { |
| 16 |
$attachment_images = array(); |
| 17 |
$upload_dir = wp_get_upload_dir(); |
| 18 |
|
| 19 |
// get sizes info from attachment meta data. |
| 20 |
$meta = wp_get_attachment_metadata($attachment_id); |
| 21 |
if (!is_array($meta) || !array_key_exists('sizes', $meta)) return $attachment_images; |
| 22 |
|
| 23 |
$image_sizes = array_keys($meta['sizes']); |
| 24 |
|
| 25 |
// build list of resized images. |
| 26 |
foreach ($image_sizes as $size) { |
| 27 |
$image = image_get_intermediate_size($attachment_id, $size); |
| 28 |
|
| 29 |
if (is_array($image)) { |
| 30 |
$file = trailingslashit($upload_dir['basedir']) . $image['path']; |
| 31 |
if (is_file($file) && !in_array($file, $attachment_images)) { |
| 32 |
$attachment_images[$size] = $file; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return $attachment_images; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns an array of allowed extensions |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public static function get_allowed_extensions() { |
| 46 |
return array('gif', 'jpeg', 'jpg', 'png'); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns given file extension |
| 51 |
* |
| 52 |
* @param string $file |
| 53 |
* |
| 54 |
* @return mixed |
| 55 |
*/ |
| 56 |
public static function get_extension($file) { |
| 57 |
$file_type = wp_check_filetype($file); |
| 58 |
return $file_type['ext']; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Tests if given extension is present in allowed extensions array |
| 63 |
* |
| 64 |
* @param string $ext Extension to check |
| 65 |
* @param array $allowed_extensions Array of allowed extensions |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public static function is_supported_extension($ext, $allowed_extensions) { |
| 70 |
return in_array($ext, $allowed_extensions); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
endif; |
| 75 |
|