/Site/wp-content/plugins/essential-grid/includes/aq_resizer.class.php

https://gitlab.com/rubengrb/ws.vidrialum · PHP · 214 lines · 108 code · 44 blank · 62 comment · 43 complexity · 71abaf93a684cdb82bbd71bcaadeb803 MD5 · raw file

  1. <?php
  2. /**
  3. * Title : Aqua Resizer
  4. * Description : Resizes WordPress images on the fly
  5. * Version : 1.2.0
  6. * Author : Syamil MJ
  7. * Author URI : http://aquagraphite.com
  8. * License : WTFPL - http://sam.zoy.org/wtfpl/
  9. * Documentation : https://github.com/sy4mil/Aqua-Resizer/
  10. *
  11. * @param string $url - (required) must be uploaded using wp media uploader
  12. * @param int $width - (required)
  13. * @param int $height - (optional)
  14. * @param bool $crop - (optional) default to soft crop
  15. * @param bool $single - (optional) returns an array if false
  16. * @uses wp_upload_dir()
  17. * @uses image_resize_dimensions()
  18. * @uses wp_get_image_editor()
  19. *
  20. * @return str|array
  21. */
  22. if( !defined( 'ABSPATH') ) exit();
  23. if(!class_exists('Ess_Aq_Resize')) {
  24. class Ess_Aq_Resize
  25. {
  26. /**
  27. * The singleton instance
  28. */
  29. static private $instance = null;
  30. /**
  31. * No initialization allowed
  32. */
  33. private function __construct() {}
  34. /**
  35. * No cloning allowed
  36. */
  37. private function __clone() {}
  38. /**
  39. * For your custom default usage you may want to initialize an Aq_Resize object by yourself and then have own defaults
  40. */
  41. static public function getInstance() {
  42. if(self::$instance == null) {
  43. self::$instance = new self;
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * Run, forest.
  49. */
  50. public function process( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) {
  51. // Validate inputs.
  52. if ( ! $url || ( ! $width && ! $height ) ) return false;
  53. // Caipt'n, ready to hook.
  54. if ( true === $upscale ) add_filter( 'image_resize_dimensions', array($this, 'aq_upscale'), 10, 6 );
  55. // Define upload path & dir.
  56. $upload_info = wp_upload_dir();
  57. $upload_dir = $upload_info['basedir'];
  58. $upload_url = $upload_info['baseurl'];
  59. $http_prefix = "http://";
  60. $https_prefix = "https://";
  61. /* if the $url scheme differs from $upload_url scheme, make them match
  62. if the schemes differe, images don't show up. */
  63. if(!strncmp($url,$https_prefix,strlen($https_prefix))){ //if url begins with https:// make $upload_url begin with https:// as well
  64. $upload_url = str_replace($http_prefix,$https_prefix,$upload_url);
  65. }
  66. elseif(!strncmp($url,$http_prefix,strlen($http_prefix))){ //if url begins with http:// make $upload_url begin with http:// as well
  67. $upload_url = str_replace($https_prefix,$http_prefix,$upload_url);
  68. }
  69. // Check if $img_url is local.
  70. if ( false === strpos( $url, $upload_url ) ) return false;
  71. // Define path of image.
  72. $rel_path = str_replace( $upload_url, '', $url );
  73. $img_path = $upload_dir . $rel_path;
  74. // Check if img path exists, and is an image indeed.
  75. if ( ! file_exists( $img_path ) or ! getimagesize( $img_path ) ) return false;
  76. // Get image info.
  77. $info = pathinfo( $img_path );
  78. $ext = $info['extension'];
  79. list( $orig_w, $orig_h ) = getimagesize( $img_path );
  80. // Get image size after cropping.
  81. $dims = image_resize_dimensions( $orig_w, $orig_h, $width, $height, $crop );
  82. $dst_w = $dims[4];
  83. $dst_h = $dims[5];
  84. // Return the original image only if it exactly fits the needed measures.
  85. if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {
  86. $img_url = $url;
  87. $dst_w = $orig_w;
  88. $dst_h = $orig_h;
  89. } else {
  90. // Use this to check if cropped image already exists, so we can return that instead.
  91. $suffix = "{$dst_w}x{$dst_h}";
  92. $dst_rel_path = str_replace( '.' . $ext, '', $rel_path );
  93. $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
  94. if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) {
  95. // Can't resize, so return false saying that the action to do could not be processed as planned.
  96. return false;
  97. }
  98. // Else check if cache exists.
  99. elseif ( file_exists( $destfilename ) && getimagesize( $destfilename ) ) {
  100. $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
  101. }
  102. // Else, we resize the image and return the new resized image url.
  103. else {
  104. $editor = wp_get_image_editor( $img_path );
  105. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
  106. return false;
  107. $resized_file = $editor->save();
  108. if ( ! is_wp_error( $resized_file ) ) {
  109. $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path'] );
  110. $img_url = $upload_url . $resized_rel_path;
  111. } else {
  112. return false;
  113. }
  114. }
  115. }
  116. // Okay, leave the ship.
  117. if ( true === $upscale ) remove_filter( 'image_resize_dimensions', array( $this, 'aq_upscale' ) );
  118. // Return the output.
  119. if ( $single ) {
  120. // str return.
  121. $image = $img_url;
  122. } else {
  123. // array return.
  124. $image = array (
  125. 0 => $img_url,
  126. 1 => $dst_w,
  127. 2 => $dst_h
  128. );
  129. }
  130. return $image;
  131. }
  132. /**
  133. * Callback to overwrite WP computing of thumbnail measures
  134. */
  135. function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
  136. if ( ! $crop ) return null; // Let the wordpress default function handle this.
  137. // Here is the point we allow to use larger image size than the original one.
  138. $aspect_ratio = $orig_w / $orig_h;
  139. $new_w = $dest_w;
  140. $new_h = $dest_h;
  141. if ( ! $new_w ) {
  142. $new_w = intval( $new_h * $aspect_ratio );
  143. }
  144. if ( ! $new_h ) {
  145. $new_h = intval( $new_w / $aspect_ratio );
  146. }
  147. $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
  148. $crop_w = round( $new_w / $size_ratio );
  149. $crop_h = round( $new_h / $size_ratio );
  150. $s_x = floor( ( $orig_w - $crop_w ) / 2 );
  151. $s_y = floor( ( $orig_h - $crop_h ) / 2 );
  152. return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
  153. }
  154. }
  155. }
  156. if(!function_exists('ess_aq_resize')) {
  157. /**
  158. * This is just a tiny wrapper function for the class above so that there is no
  159. * need to change any code in your own WP themes. Usage is still the same :)
  160. */
  161. function ess_aq_resize( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) {
  162. $aq_resize = Ess_Aq_Resize::getInstance();
  163. $img = $aq_resize->process( $url, $width, $height, $crop, $single, $upscale );
  164. if($single){
  165. return ($img == '') ? $url : $img;
  166. }else{
  167. return (empty($img)) ? $url : $img;
  168. }
  169. }
  170. }