Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. * @param string $url - (required) must be uploaded using wp media uploader
  6. * @param int $width - (required)
  7. * @param int $height - (optional)
  8. * @param bool $crop - (optional) default to soft crop
  9. * @param bool $single - (optional) returns an array if false
  10. * @uses wp_upload_dir()
  11. * @uses wp_get_image_editor()
  12. *
  13. * @return str|array
  14. */
  15.  
  16. function xxxxxx_aq_resize( $url, $width, $height = null, $crop = null, $single = true ) {
  17.  
  18. //validate inputs
  19. if(!$url OR !$width ) return false;
  20.  
  21. //define upload path & dir
  22. $upload_info = wp_upload_dir();
  23. $upload_dir = $upload_info['basedir'];
  24. $upload_url = $upload_info['baseurl'];
  25.  
  26. //check if $img_url is local
  27. if(strpos( $url, $upload_url ) === false) return false;
  28.  
  29. //define path of image
  30. $rel_path = str_replace( $upload_url, '', $url);
  31. $img_path = $upload_dir . $rel_path;
  32.  
  33. //check if img path exists, and is an image indeed
  34. if( !file_exists($img_path) OR !getimagesize($img_path) ) return false;
  35.  
  36. //get image info
  37. $info = pathinfo($img_path);
  38. $ext = $info['extension'];
  39. list($orig_w,$orig_h) = getimagesize($img_path);
  40.  
  41. //get image size after cropping
  42. $dims = image_resize_dimensions($orig_w, $orig_h, $width, $height, $crop);
  43. $dst_w = $dims[4];
  44. $dst_h = $dims[5];
  45.  
  46. //use this to check if cropped image already exists, so we can return that instead
  47. $suffix = "{$dst_w}x{$dst_h}";
  48. $dst_rel_path = str_replace( '.'.$ext, '', $rel_path);
  49. $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
  50.  
  51. if(!$dst_h) {
  52. //can't resize, so return original url
  53. $img_url = $url;
  54. $dst_w = $orig_w;
  55. $dst_h = $orig_h;
  56. }
  57. //else check if cache exists
  58. elseif(file_exists($destfilename) && getimagesize($destfilename)) {
  59. $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
  60. }
  61. //else, we resize the image and return the new resized image url
  62. else {
  63.  
  64. // Note: This pre-3.5 fallback check will edited out in subsequent version
  65. if(function_exists('wp_get_image_editor')) {
  66.  
  67. $editor = wp_get_image_editor($img_path);
  68.  
  69. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
  70. return false;
  71.  
  72. $resized_file = $editor->save();
  73.  
  74. if(!is_wp_error($resized_file)) {
  75. $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path']);
  76. $img_url = $upload_url . $resized_rel_path;
  77. } else {
  78. return false;
  79. }
  80.  
  81. }
  82.  
  83. }
  84.  
  85. //return the output
  86. if($single) {
  87. //str return
  88. $image = $img_url;
  89. } else {
  90. //array return
  91. $image = array (
  92. 0 => $img_url,
  93. 1 => $dst_w,
  94. 2 => $dst_h
  95. );
  96. }
  97.  
  98. return $image;
  99. } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement