class-wpml-media-sizes.php
3.04 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Media_Sizes {
/**
* @param array $img
*
* @return null|string
*/
public function get_size_from_class( array $img ) {
if ( array_key_exists( 'attributes', $img ) && array_key_exists( 'class', $img['attributes'] ) ) {
$classes = explode( ' ', $img['attributes']['class'] );
foreach ( $classes as $class ) {
if ( strpos( $class, 'size-' ) === 0 ) {
$class_parts = explode( '-', $class );
if ( count( $class_parts ) >= 2 ) {
unset( $class_parts[0] );
return implode( '-', $class_parts );
}
}
}
}
return null;
}
/**
* @param array $img
*
* @return null|string
*/
public function get_size_from_attributes( array $img ) {
if (
array_key_exists( 'attributes', $img )
&& array_key_exists( 'width', $img['attributes'] )
&& array_key_exists( 'height', $img['attributes'] )
) {
$width = $img['attributes']['width'];
$height = $img['attributes']['height'];
$size_name = $this->get_image_size_name( $width, $height );
if ( $size_name ) {
return $size_name;
}
}
return null;
}
/**
* @param array $img
*
* @return null|string
*/
public function get_attachment_size( array $img ) {
$size = null;
if ( array_key_exists( 'size', $img ) ) {
$size = $img['size'];
}
if ( ! $size ) {
$size = $this->get_size_from_class( $img );
}
if ( ! $size ) {
$size = $this->get_size_from_attributes( $img );
}
if ( ! $size ) {
$size = $this->get_size_from_url( $img );
}
return $size;
}
/**
* @param string $width
* @param string $height
*
* @return null|string
*/
private function get_image_size_name( $width, $height ) {
global $_wp_additional_image_sizes;
foreach ( get_intermediate_image_sizes() as $size ) {
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
if ( $width == $_wp_additional_image_sizes[ $size ]['width'] && $height == $_wp_additional_image_sizes[ $size ]['height'] ) {
return $size;
}
} elseif ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
if ( $width == get_option( "{$size}_size_w" ) && $height == get_option( "{$size}_size_h" ) ) {
return $size;
}
}
}
return null;
}
/**
* @param array $img
*
* @return null|string
*/
private function get_size_from_url( array $img ) {
$size = null;
if ( isset( $img['attributes']['src'], $img['attachment_id'] ) ) {
$size = $this->get_image_size_from_url( $img['attributes']['src'], $img['attachment_id'] );
}
return $size;
}
/**
* @param $url
* @param $attachment_id
*
* @return null|string
*/
public function get_image_size_from_url( $url, $attachment_id ) {
$size = null;
$thumb_file_name = basename( $url );
$attachment_meta_data = wp_get_attachment_metadata( $attachment_id );
if ( isset( $attachment_meta_data['sizes'] ) ) {
foreach ( $attachment_meta_data['sizes'] as $key => $size_array ) {
if ( $thumb_file_name === $size_array['file'] ) {
$size = $key;
break;
}
}
}
return $size;
}
}