Guest User

Untitled

a guest
Jul 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <?php
  2. // Only dependency that's used
  3.  
  4. // TODO: Implement filter for the_content();
  5.  
  6. use WebPConvert\WebPConvert;
  7.  
  8. add_filter('wp_generate_attachment_metadata', function($meta) {
  9.  
  10. $path = wp_upload_dir(); // get upload directory
  11. $file = $path['basedir'].'/'.$meta['file']; // Get full size image
  12.  
  13. $files[] = $file; // Set up an array of image size urls
  14.  
  15. foreach ($meta['sizes'] as $size) {
  16. $files[] = $path['path'].'/'.$size['file'];
  17. }
  18.  
  19. foreach ($files as $file) { // iterate through each image size
  20. list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
  21. $image = wp_load_image($file);
  22.  
  23. switch ($orig_type) {
  24. case IMAGETYPE_PNG:
  25. case IMAGETYPE_JPEG:
  26. $success = WebPConvert::convert($file, $file.'.webp', array(
  27. 'quality' => 90,
  28. // more options available!
  29. ));
  30. break;
  31. }
  32. }
  33. return $meta;
  34.  
  35. });
  36.  
  37.  
  38. add_action('delete_attachment', function ($id){
  39. if(wp_attachment_is_image($id)){
  40. $path = wp_upload_dir(); // get upload directory
  41. $images = get_images($id);
  42. foreach ($images as $i) {
  43. // Need to check if webp version exists, unable to convert all files only PNG and JPG
  44. if (file_exists(ABSPATH . '..' . wp_make_link_relative( $i ).'.webp')){
  45. unlink(ABSPATH . '..' . wp_make_link_relative( $i ).'.webp');
  46. }
  47. }
  48. }
  49. });
  50.  
  51. add_filter('post_thumbnail_html', function ($html, $post_id, $post_thumbnail_id, $size, $attr) {
  52. $id = get_post_thumbnail_id(); // gets the id of the current post_thumbnail (in the loop)
  53. $src = wp_get_attachment_image_src($id, $size); // gets the image url specific to the passed in size (aka. custom image size)
  54. $alt = get_the_title($id); // gets the post thumbnail title
  55. $class = $attr['class']; // gets classes passed to the post thumbnail, defined here for easier function access
  56. if (empty($src)){
  57. $src = wp_get_attachment_image_src($id, 'full'); // gets the image url specific to the passed in size (aka. custom image size)
  58. }
  59.  
  60. $html = '<picture class="'.$class.'">';
  61. // Check if webp version exists, not converting Gif images
  62. if (file_exists(ABSPATH . '..' . wp_make_link_relative( $src[0] ).'.webp')){
  63. $html .= '<source srcset="'.$src[0].'.webp" type="image/webp">';
  64. }
  65. $html .= '<source srcset="'.$src[0].'" type="image/jpeg">';
  66. $html .= '<img src="'.$src[0].'" alt="fallback">';
  67. $html .= '</picture>';
  68.  
  69.  
  70. return $html;
  71. }, 99, 5);
  72.  
  73. function get_images($id) {
  74. if ( !wp_attachment_is_image( $id ) )
  75. return;
  76.  
  77. $links = array();
  78. $sizes = get_intermediate_image_sizes();
  79. $sizes[] = 'full';
  80. foreach ( $sizes as $size ) {
  81. $image = wp_get_attachment_image_src( get_the_ID(), $size );
  82. if ( !empty( $image ) && ( true == $image[3] || 'full' == $size ) )
  83. $links[] = $image[0];
  84. }
  85.  
  86. return $links;
  87. }
  88. ?>
Add Comment
Please, Sign In to add comment