PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/box/plugins/admin-columns-pro/codepress-admin-columns/classes/Helper/Image.php

https://bitbucket.org/paulafaria/navaranda-wp
PHP | 329 lines | 170 code | 66 blank | 93 comment | 37 complexity | aab69bf51dbfe90ef2e1bd256c9ad08f MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. class AC_Helper_Image {
  6. /**
  7. * Resize image
  8. *
  9. * @param string $file
  10. * @param int $max_w
  11. * @param int $max_h
  12. * @param bool $crop
  13. * @param null|string $suffix
  14. * @param null|string $dest_path
  15. * @param int $jpeg_quality
  16. *
  17. * @return bool|string|WP_Error
  18. */
  19. public function resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
  20. $editor = wp_get_image_editor( $file );
  21. if ( is_wp_error( $editor ) ) {
  22. return false;
  23. }
  24. $editor->set_quality( $jpeg_quality );
  25. $resized = $editor->resize( $max_w, $max_h, $crop );
  26. if ( is_wp_error( $resized ) ) {
  27. return false;
  28. }
  29. $dest_file = $editor->generate_filename( $suffix, $dest_path );
  30. $saved = $editor->save( $dest_file );
  31. if ( is_wp_error( $saved ) ) {
  32. return false;
  33. }
  34. return $dest_file;
  35. }
  36. /**
  37. * @param int[]|int $ids
  38. * @param array|string $size
  39. *
  40. * @return string HTML Images
  41. */
  42. public function get_images_by_ids( $ids, $size ) {
  43. $images = array();
  44. $ids = is_array( $ids ) ? $ids : array( $ids );
  45. foreach ( $ids as $id ) {
  46. $images[] = $this->get_image_by_id( $id, $size );
  47. }
  48. return implode( $images );
  49. }
  50. /**
  51. * @param int $id
  52. * @param string|array $size
  53. *
  54. * @return string
  55. */
  56. public function get_image_by_id( $id, $size ) {
  57. $image = false;
  58. if ( ! is_numeric( $id ) ) {
  59. return false;
  60. }
  61. // Is Image
  62. if ( $attributes = wp_get_attachment_image_src( $id, $size ) ) {
  63. $src = $attributes[0];
  64. if ( is_array( $size ) ) {
  65. $image = $this->markup_cover( $src, $size[0], $size[1], $id );
  66. } else {
  67. $image = $this->markup( $src, $attributes[1], $attributes[2], $id );
  68. }
  69. } // Is File, use icon
  70. else if ( $attributes = wp_get_attachment_image_src( $id, $size, true ) ) {
  71. $image = $this->markup( $attributes[0], $this->scale_size( $attributes[1], 0.8 ), $this->scale_size( $attributes[2], 0.8 ), $id, true );
  72. }
  73. return $image;
  74. }
  75. /**
  76. * @param $size
  77. * @param int $scale
  78. *
  79. * @return float
  80. */
  81. private function scale_size( $size, $scale = 1 ) {
  82. return round( absint( $size ) * $scale );
  83. }
  84. /**
  85. * @param string $url
  86. * @param array|string $size
  87. *
  88. * @return string
  89. */
  90. public function get_image_by_url( $url, $size ) {
  91. $dimensions = array( 80, 80 );
  92. if ( is_string( $size ) && ( $sizes = $this->get_image_sizes_by_name( $size ) ) ) {
  93. $dimensions = array( $sizes['width'], $sizes['height'] );
  94. } else if ( is_array( $size ) ) {
  95. $dimensions = $size;
  96. }
  97. $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
  98. if ( is_file( $image_path ) ) {
  99. // try to resize image
  100. if ( $resized = $this->resize( $image_path, $dimensions[0], $dimensions[1], true ) ) {
  101. $src = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized );
  102. $image = $this->markup( $src, $dimensions[0], $dimensions[1] );
  103. } else {
  104. $image = $this->markup( $url, $dimensions[0], $dimensions[1] );
  105. }
  106. } // External image
  107. else {
  108. $image = $this->markup_cover( $image_path, $dimensions[0], $dimensions[1] );
  109. }
  110. return $image;
  111. }
  112. /**
  113. * @param mixed $images
  114. * @param array|string $size
  115. * @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).
  116. *
  117. * @return array
  118. */
  119. public function get_images( $images, $size = 'thumbnail', $skip_image_check = false ) {
  120. $thumbnails = array();
  121. foreach ( (array) $images as $value ) {
  122. if ( $skip_image_check && $value && is_string( $value ) ) {
  123. $thumbnails[] = $this->get_image_by_url( $value, $size );
  124. } else if ( ac_helper()->string->is_image( $value ) ) {
  125. $thumbnails[] = $this->get_image_by_url( $value, $size );
  126. } // Media Attachment
  127. else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
  128. $thumbnails[] = $this->get_image_by_id( $value, $size );
  129. }
  130. }
  131. return $thumbnails;
  132. }
  133. /**
  134. * @param int|string $image ID of Url
  135. * @param string $size
  136. *
  137. * @return string
  138. */
  139. public function get_image( $image, $size = 'thumbnail', $skip_image_check = false ) {
  140. return implode( $this->get_images( $image, $size, $skip_image_check ) );
  141. }
  142. /**
  143. * @param string $name
  144. *
  145. * @return array Image sizes
  146. */
  147. public function get_image_sizes_by_name( $name ) {
  148. global $_wp_additional_image_sizes;
  149. $sizes = false;
  150. if ( is_scalar( $name ) && isset( $_wp_additional_image_sizes[ $name ] ) ) {
  151. $sizes = $_wp_additional_image_sizes[ $name ];
  152. }
  153. return $sizes;
  154. }
  155. /**
  156. * @param int $attachment_id
  157. *
  158. * @return bool|string
  159. */
  160. public function get_file_name( $attachment_id ) {
  161. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  162. if ( ! $file ) {
  163. return false;
  164. }
  165. return basename( $file );
  166. }
  167. /**
  168. * @param int $attachment_id
  169. *
  170. * @return string File extension
  171. */
  172. public function get_file_extension( $attachment_id ) {
  173. return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION );
  174. }
  175. // Helpers
  176. private function get_file_tooltip_attr( $media_id ) {
  177. return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) );
  178. }
  179. private function markup_cover( $src, $width, $height, $media_id = null ) {
  180. ob_start(); ?>
  181. <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>
  182. <?php
  183. return ob_get_clean();
  184. }
  185. private function markup( $src, $width, $height, $media_id = null, $add_extension = false ) {
  186. $class = false;
  187. if ( $media_id && ! wp_attachment_is_image( $media_id ) ) {
  188. $class = ' ac-icon';
  189. }
  190. ob_start(); ?>
  191. <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 ); ?>>
  192. <img style="max-width:<?php echo esc_attr( $width ); ?>px;max-height:<?php echo esc_attr( $height ); ?>px;" src="<?php echo esc_attr( $src ); ?>">
  193. <?php if ( $add_extension ) : ?>
  194. <span class="ac-extension"><?php echo esc_attr( $this->get_file_extension( $media_id ) ); ?></span>
  195. <?php endif; ?>
  196. </span>
  197. <?php
  198. return ob_get_clean();
  199. }
  200. /**
  201. * Return dimensions and file type
  202. *
  203. * @see filesize
  204. *
  205. * @param string $url
  206. *
  207. * @return false|array
  208. */
  209. public function get_local_image_info( $url ) {
  210. $path = $this->get_local_image_path( $url );
  211. if ( ! $path ) {
  212. return false;
  213. }
  214. return getimagesize( $path );
  215. }
  216. /**
  217. * @param string $url
  218. *
  219. * @return false|string
  220. */
  221. public function get_local_image_path( $url ) {
  222. $path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
  223. if ( ! file_exists( $path ) ) {
  224. return false;
  225. }
  226. return $path;
  227. }
  228. /**
  229. * @param string $url
  230. *
  231. * @return false|int
  232. */
  233. public function get_local_image_size( $url ) {
  234. $path = $this->get_local_image_path( $url );
  235. if ( ! $path ) {
  236. return false;
  237. }
  238. return filesize( $path );
  239. }
  240. /**
  241. * @param string $string
  242. *
  243. * @return array
  244. */
  245. public function get_image_urls_from_string( $string ) {
  246. if ( ! $string ) {
  247. return array();
  248. }
  249. if ( ! class_exists( 'DOMDocument' ) ) {
  250. return array();
  251. }
  252. $dom = new DOMDocument;
  253. @$dom->loadHTML( $string );
  254. $dom->preserveWhiteSpace = false;
  255. $urls = array();
  256. $images = $dom->getElementsByTagName( 'img' );
  257. foreach ( $images as $img ) {
  258. /** @var DOMElement $img */
  259. $urls[] = $img->getAttribute( 'src' );
  260. }
  261. return $urls;
  262. }
  263. }