PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/envira-gallery/src/Functions/image.php

https://bitbucket.org/dgadwa/alter-ego
PHP | 233 lines | 147 code | 46 blank | 40 comment | 27 complexity | 746acd820c4a2732f3d72e496e754319 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. // Exit if accessed directly.
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. /**
  7. * isImage function.
  8. *
  9. * @access public
  10. * @param mixed $url
  11. * @return void
  12. */
  13. function envira_is_image( $url ) {
  14. $p = strrpos( $url, ".");
  15. if ( $p === false ){
  16. return false;
  17. }
  18. $extension = strtolower( trim( substr( $url, $p ) ) );
  19. $img_extensions = array('.gif', '.jpg', '.jpeg', '.png', '.tiff', '.tif');
  20. if ( in_array( $extension, $img_extensions ) ){
  21. return true;
  22. }
  23. return false;
  24. }
  25. /**
  26. * get_image_sizes function.
  27. *
  28. * @access public
  29. * @return void
  30. */
  31. function envira_get_image_sizes( $wordpress_only = false ){
  32. if ( ! $wordpress_only ) {
  33. $sizes = array(
  34. array(
  35. 'value' => 'default',
  36. 'name' => __( 'Default', 'envira-gallery' ),
  37. )
  38. );
  39. }
  40. global $_wp_additional_image_sizes;
  41. $wp_sizes = get_intermediate_image_sizes();
  42. foreach ( (array) $wp_sizes as $size ) {
  43. if ( isset( $_wp_additional_image_sizes[$size] ) ) {
  44. $width = absint( $_wp_additional_image_sizes[$size]['width'] );
  45. $height = absint( $_wp_additional_image_sizes[$size]['height'] );
  46. } else {
  47. $width = absint( get_option( $size . '_size_w' ) );
  48. $height = absint( get_option( $size . '_size_h' ) );
  49. }
  50. if ( ! $width && ! $height ) {
  51. $sizes[] = array(
  52. 'value' => $size,
  53. 'name' => ucwords( str_replace( array( '-', '_' ), ' ', $size ) ),
  54. );
  55. } else {
  56. $sizes[] = array(
  57. 'value' => $size,
  58. 'name' => ucwords( str_replace( array( '-', '_' ), ' ', $size ) ) . ' (' . $width . ' &#215; ' . $height . ')',
  59. 'width' => $width,
  60. 'height' => $height,
  61. );
  62. }
  63. }
  64. // Add Option for full image
  65. $sizes[] = array(
  66. 'value' => 'full',
  67. 'name' => __( 'Original Image', 'envira-gallery' ),
  68. );
  69. // Add Random option
  70. if ( ! $wordpress_only ) {
  71. $sizes[] = array(
  72. 'value' => 'envira_gallery_random',
  73. 'name' => __( 'Random', 'envira-gallery' ),
  74. );
  75. }
  76. return apply_filters( 'envira_gallery_image_sizes', $sizes );
  77. }
  78. function envira_get_shortcode_image_sizes(){
  79. global $_wp_additional_image_sizes;
  80. $sizes = array();
  81. foreach ( get_intermediate_image_sizes() as $_size ) {
  82. if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
  83. if ( (bool) get_option( "{$_size}_crop" ) === true ){
  84. continue;
  85. }
  86. $sizes[ $_size ]['name'] = $_size;
  87. $sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" );
  88. $sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" );
  89. $sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" );
  90. } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
  91. if( $_wp_additional_image_sizes[ $_size ]['crop'] === true ){
  92. continue;
  93. }
  94. $sizes[ $_size ] = array(
  95. 'name' => $_size,
  96. 'width' => $_wp_additional_image_sizes[ $_size ]['width'],
  97. 'height' => $_wp_additional_image_sizes[ $_size ]['height'],
  98. 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'],
  99. );
  100. }
  101. }
  102. return $sizes;
  103. }
  104. /**
  105. * Helper method to return common information about an image.
  106. *
  107. * @since 1.0.0
  108. *
  109. * @param array $args List of resizing args to expand for gathering info.
  110. * @return WP_Error|string Return WP_Error on error, array of data on success.
  111. */
  112. function envira_get_image_info( $args ) {
  113. // Unpack arguments.
  114. list( $url, $width, $height, $crop, $align, $quality, $retina, $data ) = $args;
  115. // Return an error if no URL is present.
  116. if ( empty( $url ) ) {
  117. return new WP_Error( 'envira-gallery-error-no-url', __( 'No image URL specified for cropping.', 'envira-gallery' ) );
  118. }
  119. // Get the image file path.
  120. $urlinfo = parse_url( $url );
  121. $wp_upload_dir = wp_upload_dir();
  122. // Interpret the file path of the image.
  123. if ( preg_match( '/\/[0-9]{4}\/[0-9]{2}\/.+$/', $urlinfo['path'], $matches ) ) {
  124. $file_path = $wp_upload_dir['basedir'] . $matches[0];
  125. } else {
  126. $content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : '/wp-content/';
  127. $pathinfo = parse_url( $url );
  128. $uploads_dir = is_multisite() ? '/files/' : $content_dir;
  129. $file_path = trailingslashit( $wp_upload_dir['basedir'] ) . basename ( $urlinfo['path'] );
  130. $file_path = preg_replace( '/(\/\/)/', '/', $file_path );
  131. }
  132. // Attempt to stream and import the image if it does not exist based on URL provided.
  133. if ( ! file_exists( $file_path ) ) {
  134. return new WP_Error( 'envira-gallery-error-no-file', __( 'No file could be found for the image URL specified.', 'envira-gallery' ) );
  135. }
  136. error_log( $file_path );
  137. // Get original image size.
  138. $size = @getimagesize( $file_path );
  139. // If no size data obtained, return an error.
  140. if ( !is_array( $size ) ) {
  141. return new WP_Error( 'envira-gallery-error-no-size', __( 'The dimensions of the original image could not be retrieved for cropping.', 'envira-gallery' ) );
  142. }
  143. // Set original width and height.
  144. list( $orig_width, $orig_height, $orig_type ) = $size;
  145. // Generate width or height if not provided.
  146. if ( $width && ! $height ) {
  147. $height = floor( $orig_height * ($width / $orig_width) );
  148. } else if ( $height && ! $width ) {
  149. $width = floor( $orig_width * ($height / $orig_height) );
  150. } else if ( ! $width && ! $height ) {
  151. return new WP_Error( 'envira-gallery-error-no-size', __( 'The dimensions of the original image could not be retrieved for cropping.', 'envira-gallery' ) );
  152. }
  153. // Allow for different retina image sizes.
  154. $retina = $retina ? 2 : 1;
  155. // Destination width and height variables
  156. $dest_width = $width * $retina;
  157. $dest_height = $height * $retina;
  158. // Some additional info about the image.
  159. $info = pathinfo( $file_path );
  160. $dir = $info['dirname'];
  161. $ext = $info['extension'];
  162. $name = wp_basename( $file_path, ".$ext" );
  163. // Suffix applied to filename
  164. $suffix = "${dest_width}x${dest_height}";
  165. // Set alignment information on the file.
  166. if ( $crop ) {
  167. $suffix .= ( $align ) ? "_${align}" : "_c";
  168. }
  169. // Get the destination file name
  170. $dest_file_name = "${dir}/${name}-${suffix}.${ext}";
  171. error_log($dest_file_name);
  172. // Return the info.
  173. $info = array(
  174. 'dir' => $dir,
  175. 'name' => $name,
  176. 'ext' => $ext,
  177. 'suffix' => $suffix,
  178. 'orig_width' => $orig_width,
  179. 'orig_height' => $orig_height,
  180. 'orig_type' => $orig_type,
  181. 'dest_width' => $dest_width,
  182. 'dest_height' => $dest_height,
  183. 'file_path' => $file_path,
  184. 'dest_file_name' => $dest_file_name,
  185. );
  186. return $info;
  187. }