1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class AMP_Image_Dimension_Extractor { |
4
|
|
|
static $callbacks_registered = false; |
|
|
|
|
5
|
|
|
|
6
|
|
|
static public function extract( $url ) { |
|
|
|
|
7
|
|
|
if ( ! self::$callbacks_registered ) { |
8
|
|
|
self::register_callbacks(); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
$url = self::normalize_url( $url ); |
12
|
|
|
if ( false === $url ) { |
13
|
|
|
return false; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
return apply_filters( 'amp_extract_image_dimensions', false, $url ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function normalize_url( $url ) { |
20
|
|
|
if ( empty( $url ) ) { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ( 0 === strpos( $url, 'data:' ) ) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ( 0 === strpos( $url, '//' ) ) { |
29
|
|
|
return set_url_scheme( $url, 'http' ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$parsed = parse_url( $url ); |
33
|
|
|
if ( ! isset( $parsed['host'] ) ) { |
34
|
|
|
$path = ''; |
35
|
|
|
if ( isset( $parsed['path'] ) ) { |
36
|
|
|
$path .= $parsed['path']; |
37
|
|
|
} |
38
|
|
|
if ( isset( $parsed['query'] ) ) { |
39
|
|
|
$path .= '?' . $parsed['query']; |
40
|
|
|
} |
41
|
|
|
$url = site_url( $path ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $url; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private static function register_callbacks() { |
48
|
|
|
self::$callbacks_registered = true; |
49
|
|
|
|
50
|
|
|
add_filter( 'amp_extract_image_dimensions', array( __CLASS__, 'extract_from_attachment_metadata' ), 10, 2 ); |
51
|
|
|
add_filter( 'amp_extract_image_dimensions', array( __CLASS__, 'extract_by_downloading_image' ), 999, 2 ); // Run really late since this is our last resort |
52
|
|
|
|
53
|
|
|
do_action( 'amp_extract_image_dimensions_callbacks_registered' ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function extract_from_attachment_metadata( $dimensions, $url ) { |
57
|
|
|
if ( is_array( $dimensions ) ) { |
58
|
|
|
return $dimensions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$url = strtok( $url, '?' ); |
62
|
|
|
$attachment_id = attachment_url_to_postid( $url ); |
63
|
|
|
if ( empty( $attachment_id ) ) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$metadata = wp_get_attachment_metadata( $attachment_id ); |
68
|
|
|
if ( ! $metadata ) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return array( $metadata['width'], $metadata['height'] ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function extract_by_downloading_image( $dimensions, $url ) { |
76
|
|
|
if ( is_array( $dimensions ) ) { |
77
|
|
|
return $dimensions; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$url_hash = md5( $url ); |
81
|
|
|
$transient_name = sprintf( 'amp_img_%s', $url_hash ); |
82
|
|
|
$transient_expiry = 30 * DAY_IN_SECONDS; |
83
|
|
|
$transient_fail = 'fail'; |
84
|
|
|
|
85
|
|
|
$dimensions = get_transient( $transient_name ); |
86
|
|
|
|
87
|
|
|
if ( is_array( $dimensions ) ) { |
88
|
|
|
return $dimensions; |
89
|
|
|
} elseif ( $transient_fail === $dimensions ) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Very simple lock to prevent stampedes |
94
|
|
|
$transient_lock_name = sprintf( 'amp_lock_%s', $url_hash ); |
95
|
|
|
if ( false !== get_transient( $transient_lock_name ) ) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
set_transient( $transient_lock_name, 1, MINUTE_IN_SECONDS ); |
99
|
|
|
|
100
|
|
|
// Note to other developers: please don't use this class directly as it may not stick around forever... |
101
|
|
|
if ( ! class_exists( 'FastImage' ) ) { |
102
|
|
|
require_once( AMP__ROOT__ . '/includes/lib/class-fastimage.php' ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// TODO: look into using curl+stream (https://github.com/willwashburn/FasterImage) |
|
|
|
|
106
|
|
|
$image = new FastImage( $url ); |
107
|
|
|
$dimensions = $image->getSize(); |
108
|
|
|
|
109
|
|
|
if ( ! is_array( $dimensions ) ) { |
110
|
|
|
set_transient( $transient_name, $transient_fail, $transient_expiry ); |
111
|
|
|
delete_transient( $transient_lock_name ); |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
set_transient( $transient_name, $dimensions, $transient_expiry ); |
116
|
|
|
delete_transient( $transient_lock_name ); |
117
|
|
|
return $dimensions; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.