Arrays.php
7 years ago
Date.php
7 years ago
File.php
7 years ago
Html.php
7 years ago
Icon.php
7 years ago
Image.php
7 years ago
Media.php
7 years ago
Network.php
7 years ago
Post.php
7 years ago
Strings.php
7 years ago
Taxonomy.php
7 years ago
User.php
7 years ago
Image.php
327 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper; |
| 4 | |
| 5 | use DOMDocument; |
| 6 | |
| 7 | class Image { |
| 8 | |
| 9 | /** |
| 10 | * Resize image |
| 11 | * |
| 12 | * @param string $file |
| 13 | * @param int $max_w |
| 14 | * @param int $max_h |
| 15 | * @param bool $crop |
| 16 | * @param null|string $suffix |
| 17 | * @param null|string $dest_path |
| 18 | * @param int $jpeg_quality |
| 19 | * |
| 20 | * @return bool|string|\WP_Error |
| 21 | */ |
| 22 | public function resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { |
| 23 | $editor = wp_get_image_editor( $file ); |
| 24 | |
| 25 | if ( is_wp_error( $editor ) ) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | $editor->set_quality( $jpeg_quality ); |
| 30 | |
| 31 | $resized = $editor->resize( $max_w, $max_h, $crop ); |
| 32 | |
| 33 | if ( is_wp_error( $resized ) ) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | $dest_file = $editor->generate_filename( $suffix, $dest_path ); |
| 38 | $saved = $editor->save( $dest_file ); |
| 39 | |
| 40 | if ( is_wp_error( $saved ) ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return $dest_file; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param int[]|int $ids |
| 49 | * @param array|string $size |
| 50 | * |
| 51 | * @return string HTML Images |
| 52 | */ |
| 53 | public function get_images_by_ids( $ids, $size ) { |
| 54 | $images = array(); |
| 55 | |
| 56 | $ids = is_array( $ids ) ? $ids : array( $ids ); |
| 57 | foreach ( $ids as $id ) { |
| 58 | $images[] = $this->get_image_by_id( $id, $size ); |
| 59 | } |
| 60 | |
| 61 | return implode( $images ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param int $id |
| 66 | * @param string|array $size |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | public function get_image_by_id( $id, $size ) { |
| 71 | $image = false; |
| 72 | |
| 73 | if ( ! is_numeric( $id ) ) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // Is Image |
| 78 | if ( $attributes = wp_get_attachment_image_src( $id, $size ) ) { |
| 79 | $src = $attributes[0]; |
| 80 | |
| 81 | if ( is_array( $size ) ) { |
| 82 | $image = $this->markup_cover( $src, $size[0], $size[1], $id ); |
| 83 | } else { |
| 84 | $image = $this->markup( $src, $attributes[1], $attributes[2], $id ); |
| 85 | } |
| 86 | } // Is File, use icon |
| 87 | else if ( $attributes = wp_get_attachment_image_src( $id, $size, true ) ) { |
| 88 | $image = $this->markup( $attributes[0], $this->scale_size( $attributes[1], 0.8 ), $this->scale_size( $attributes[2], 0.8 ), $id, true ); |
| 89 | } |
| 90 | |
| 91 | return $image; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $size |
| 96 | * @param int $scale |
| 97 | * |
| 98 | * @return float |
| 99 | */ |
| 100 | private function scale_size( $size, $scale = 1 ) { |
| 101 | return round( absint( $size ) * $scale ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param string $url |
| 106 | * @param array|string $size |
| 107 | * |
| 108 | * @return string |
| 109 | */ |
| 110 | public function get_image_by_url( $url, $size ) { |
| 111 | $dimensions = array( 60, 60 ); |
| 112 | |
| 113 | if ( is_string( $size ) && ( $sizes = $this->get_image_sizes_by_name( $size ) ) ) { |
| 114 | $dimensions = array( $sizes['width'], $sizes['height'] ); |
| 115 | } else if ( is_array( $size ) ) { |
| 116 | $dimensions = $size; |
| 117 | } |
| 118 | |
| 119 | $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url ); |
| 120 | |
| 121 | if ( is_file( $image_path ) ) { |
| 122 | // try to resize image |
| 123 | if ( $resized = $this->resize( $image_path, $dimensions[0], $dimensions[1], true ) ) { |
| 124 | $src = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized ); |
| 125 | |
| 126 | $image = $this->markup( $src, $dimensions[0], $dimensions[1] ); |
| 127 | } else { |
| 128 | |
| 129 | $image = $this->markup( $url, $dimensions[0], $dimensions[1] ); |
| 130 | } |
| 131 | } // External image |
| 132 | else { |
| 133 | $image = $this->markup_cover( $image_path, $dimensions[0], $dimensions[1] ); |
| 134 | } |
| 135 | |
| 136 | return $image; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param mixed $images |
| 141 | * @param array|string $size |
| 142 | * @param bool $skip_image_check Skips image check. Useful when the url does not have an image extension like jpg or gif (e.g. gravatar). |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public function get_images( $images, $size = 'thumbnail', $skip_image_check = false ) { |
| 147 | $thumbnails = array(); |
| 148 | |
| 149 | foreach ( (array) $images as $value ) { |
| 150 | if ( $skip_image_check && $value && is_string( $value ) ) { |
| 151 | $thumbnails[] = $this->get_image_by_url( $value, $size ); |
| 152 | } else if ( ac_helper()->string->is_image( $value ) ) { |
| 153 | $thumbnails[] = $this->get_image_by_url( $value, $size ); |
| 154 | } // Media Attachment |
| 155 | else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) { |
| 156 | $thumbnails[] = $this->get_image_by_id( $value, $size ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return $thumbnails; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @param int|string $image ID of Url |
| 165 | * @param string $size |
| 166 | * @param bool $skip_image_check |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public function get_image( $image, $size = 'thumbnail', $skip_image_check = false ) { |
| 171 | return implode( $this->get_images( $image, $size, $skip_image_check ) ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @param string $name |
| 176 | * |
| 177 | * @return array Image sizes |
| 178 | */ |
| 179 | public function get_image_sizes_by_name( $name ) { |
| 180 | global $_wp_additional_image_sizes; |
| 181 | |
| 182 | $sizes = false; |
| 183 | |
| 184 | if ( is_scalar( $name ) && isset( $_wp_additional_image_sizes[ $name ] ) ) { |
| 185 | $sizes = $_wp_additional_image_sizes[ $name ]; |
| 186 | } |
| 187 | |
| 188 | return $sizes; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param int $attachment_id |
| 193 | * |
| 194 | * @return bool|string |
| 195 | */ |
| 196 | public function get_file_name( $attachment_id ) { |
| 197 | $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
| 198 | |
| 199 | if ( ! $file ) { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | return basename( $file ); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param int $attachment_id |
| 208 | * |
| 209 | * @return string File extension |
| 210 | */ |
| 211 | public function get_file_extension( $attachment_id ) { |
| 212 | return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION ); |
| 213 | } |
| 214 | |
| 215 | private function get_file_tooltip_attr( $media_id ) { |
| 216 | return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) ); |
| 217 | } |
| 218 | |
| 219 | private function markup_cover( $src, $width, $height, $media_id = null ) { |
| 220 | ob_start(); ?> |
| 221 | <span class="ac-image cpac-cover" data-media-id="<?php echo esc_attr( $media_id ); ?>" style="width:<?php echo esc_attr( $width ); ?>px;height:<?php echo esc_attr( $height ); ?>px;background-size:cover;background-image:url('<?php echo esc_attr( $src ); ?>');background-position:center;"<?php echo $this->get_file_tooltip_attr( $media_id ); ?>></span> |
| 222 | |
| 223 | <?php |
| 224 | return ob_get_clean(); |
| 225 | } |
| 226 | |
| 227 | private function markup( $src, $width, $height, $media_id = null, $add_extension = false ) { |
| 228 | $class = false; |
| 229 | |
| 230 | if ( $media_id && ! wp_attachment_is_image( $media_id ) ) { |
| 231 | $class = ' ac-icon'; |
| 232 | } |
| 233 | |
| 234 | ob_start(); ?> |
| 235 | <span class="ac-image<?php echo $class; ?>" data-media-id="<?php echo esc_attr( $media_id ); ?>"<?php echo $this->get_file_tooltip_attr( $media_id ); ?>> |
| 236 | <img style="max-width:<?php echo esc_attr( $width ); ?>px;max-height:<?php echo esc_attr( $height ); ?>px;" src="<?php echo esc_attr( $src ); ?>" alt=""> |
| 237 | |
| 238 | <?php if ( $add_extension ) : ?> |
| 239 | <span class="ac-extension"><?php echo esc_attr( $this->get_file_extension( $media_id ) ); ?></span> |
| 240 | <?php endif; ?> |
| 241 | |
| 242 | </span> |
| 243 | |
| 244 | <?php |
| 245 | return ob_get_clean(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Return dimensions and file type |
| 250 | * @see filesize |
| 251 | * |
| 252 | * @param string $url |
| 253 | * |
| 254 | * @return false|array |
| 255 | */ |
| 256 | public function get_local_image_info( $url ) { |
| 257 | $path = $this->get_local_image_path( $url ); |
| 258 | |
| 259 | if ( ! $path ) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | return getimagesize( $path ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @param string $url |
| 268 | * |
| 269 | * @return false|string |
| 270 | */ |
| 271 | public function get_local_image_path( $url ) { |
| 272 | $path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url ); |
| 273 | |
| 274 | if ( ! file_exists( $path ) ) { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | return $path; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @param string $url |
| 283 | * |
| 284 | * @return false|int |
| 285 | */ |
| 286 | public function get_local_image_size( $url ) { |
| 287 | $path = $this->get_local_image_path( $url ); |
| 288 | |
| 289 | if ( ! $path ) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | return filesize( $path ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @param string $string |
| 298 | * |
| 299 | * @return array |
| 300 | */ |
| 301 | public function get_image_urls_from_string( $string ) { |
| 302 | if ( ! $string ) { |
| 303 | return array(); |
| 304 | } |
| 305 | |
| 306 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 307 | return array(); |
| 308 | } |
| 309 | |
| 310 | $dom = new DOMDocument; |
| 311 | @$dom->loadHTML( $string ); |
| 312 | $dom->preserveWhiteSpace = false; |
| 313 | |
| 314 | $urls = array(); |
| 315 | |
| 316 | $images = $dom->getElementsByTagName( 'img' ); |
| 317 | |
| 318 | foreach ( $images as $img ) { |
| 319 | |
| 320 | /** @var \DOMElement $img */ |
| 321 | $urls[] = $img->getAttribute( 'src' ); |
| 322 | } |
| 323 | |
| 324 | return $urls; |
| 325 | } |
| 326 | |
| 327 | } |