Support » Plugin: WebP Converter for Media » Manually change to webp

  • Resolved nicmare

    (@nicmare)


    I have a lot of images. So i just want to convert some of them to webp. Would be nice to prevent automatically converting and having an option in media listing to convert some images by hand. thank you.

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Hi @nicmare,

    Thank you for your message.

    Tell me please, why do you want to convert only selected ones? And what are the images, what is the relationship?

    i have a woocommerce shop where customers can configure products by selecting variations. then woocommerce merges each PNG file to one final image preview. So we really need to keep the png files here. it wont work with webp. therefore a lot of unused webp files would be created on server. we are talking about 1000 of png images… so i need some kind of function to exclude those images from converting to webp.
    but i really like to use that webp feature on landingpages or front page for promotions. if you want, i can show you the shop for better understanding.

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Are you able to decide after the image size name whether you want to convert the image or not?

    These are the default sizes and custom sizes added by the function: https://developer.wordpress.org/reference/functions/add_image_size/

    i don’t know what you mean. we have a custom image size yes.
    what you think about a CUSTOM suffix or prefix your plugin takes into account? for instance “dctw_myimageblabla.png”. then it wont converted. dctw = dont convert to webp. this way i could upload future png files with this prefix and you are looking with substr for this prefix.

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    In WordPress you have the ability to create image sizes:
    https://codex.wordpress.org/Post_Thumbnails

    We could do it so that sizes are skipped. Are you sure that the prefix method will always work?

    All the images you upload are viewed, so their compression is important. WebP files are very light, so in my opinion this is not a problem.

    yes i know. we have some custom image sizes. but i rather prefer a string in filename to bypass conversion. these images are not important for visibility. i think you should take a look to understand: https://yourcolor.de/produkt/samsung-galaxy-buds-black/
    the used png images behind will never see any visitor.

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    But if someone changes color then the new image is loaded, so then using WebPe benefits.

    i am not sure. because the image preview is rendered out of three parts. its ONE png files merged out of three png files you understand? case, bud left, bud right. in the end you get that ONE rendered image:
    https://yourcolor.de/wp-admin/admin-ajax.php?prodid=1866&jckpc-img-size=large&attribute_pa_farbe=barbie-glossy&attribute_pa_kopfhoerer-rechts=pastel-green-glossy&attribute_pa_kopfhoerer-links=pastel-green-glossy&action=jckpc_generate_image

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    I understand you. I’ve made some changes to the plugin that should help you.

    Please go to the path: /wp-content/plugins/webp-converter-for-media/app/Regenerate/Paths.php and replace this file with the next code:

    <?php
    
      namespace WebpConverter\Regenerate;
    
      class Paths
      {
        /* ---
          Functions
        --- */
    
        public function getPaths()
        {
          $sizes = get_intermediate_image_sizes();
          $posts = get_posts([
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'fields'         => 'ids',
          ]);
    
          $list = $this->parseImages($posts, $sizes);
          wp_send_json_success($list);
        }
    
        private function parseImages($posts, $sizes)
        {
          $settings = apply_filters('webpc_get_values', []);
          $upload   = wp_upload_dir();
          $list     = [];
          if (!$posts) return $list;
    
          foreach ($posts as $postId) {
            $metadata = wp_get_attachment_metadata($postId);
            if (!isset($metadata['file'])
              || !in_array(pathinfo($metadata['file'], PATHINFO_EXTENSION), $settings['extensions'])) continue;
    
            $paths = $this->parseImageSizes($postId, $metadata['file'], $sizes, $upload);
            $paths = apply_filters('webpc_attachment_paths', $paths, $postId);
    
            if (!$paths) continue;
            $list[] = $paths;
          }
    
          $list = array_filter($list);
          return $list;
        }
    
        private function parseImageSizes($postId, $path, $sizes, $upload)
        {
          $list   = [];
          $source = str_replace('\\', '/', implode('/', [$upload['basedir'], $path]));
          if (apply_filters('webpc_convert_available', true, $source, null) === true) {
            $list['_source'] = $source;
          }
    
          foreach ($sizes as $key => $size) {
            $src = wp_get_attachment_image_src($postId, $size);
            $url = str_replace($upload['baseurl'], $upload['basedir'], $src[0]);
            $url = str_replace('\\', '/', $url);
    
            if ((apply_filters('webpc_convert_available', true, $url, $key) !== true)
              || in_array($url, $list)) continue;
            $list[$size] = $url;
          }
          return $list;
        }
      }

    Then go to the path: /wp-content/plugins/webp-converter-for-media/app/Media/Upload.php and replace this file with the next code:

    <?php
    
      namespace WebpConverter\Media;
      use WebpConverter\Convert as Convert;
    
      class Upload
      {
        public function __construct()
        {
          add_filter('wp_update_attachment_metadata', [$this, 'initAttachmentConvert'], 10, 2);
        }
    
        /* ---
          Functions
        --- */
    
        public function initAttachmentConvert($data, $attachmentId)
        {
          if (!$data || !isset($data['file']) || !isset($data['sizes'])) return $data;
          $path  = $this->getAttachmentDirectory($data['file']);
          $sizes = [];
    
          $source = $path . basename($data['file']);
          if (apply_filters('webpc_convert_available', true, $source, null) === true) {
            $sizes['_source'] = $source;
          }
    
          foreach ($data['sizes'] as $key => $size) {
            $url = $path . $size['file'];
            if ((apply_filters('webpc_convert_available', true, $url, $key) !== true)
              || in_array($url, $sizes)) continue;
            $sizes[$key] = $url;
          }
    
          $sizes = apply_filters('webpc_attachment_paths', $sizes, $attachmentId);
          $this->convertSizes($sizes);
          return $data;
        }
    
        private function getAttachmentDirectory($path)
        {
          $upload = wp_upload_dir();
          $source = rtrim($upload['basedir'], '/\\') . '/' . rtrim(dirname($path), '/\\') . '/';
          $source = str_replace('\\', '/', $source);
          return $source;
        }
    
        private function convertSizes($paths)
        {
          $settings = apply_filters('webpc_get_values', []);
    
          if ($settings['method'] === 'gd') $convert = new Convert\Gd();
          else if ($settings['method'] === 'imagick') $convert = new Convert\Imagick();
          if (!isset($convert)) return false;
    
          foreach ($paths as $path) {
            if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $settings['extensions'])) continue;
    
            $response = $convert->convertImage($path, $settings['quality']);
            if (!$response['success']) $this->addErrorToLog($response['message']);
          }
        }
    
        private function addErrorToLog($message)
        {
          error_log(sprintf(
            'WebP Converter: %s',
            $message
          ));
        }
      }

    Now add the following code to your theme (this is just an example, adapt it to your needs):

    add_filter('webpc_convert_available', function($status, $path)
    {
      $filename = basename($path);
      if (strpos($filename, 'prefix_') === 0) return false;
      return $status;
    }, 10, 2);

    The webpc_convert_available filter lets you decide if you want to convert a file from a given path.

    Tell me please, will it help you? If so, I will add it to the official version of the plugin.

    can not test yet. WEBP is yet not available but code looks good! is see a filter with strpos and this should work well. thank you!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @nicmare, why WEBP is yet not available? Could you test it at home now before I release the update?

    give me 10 min

    edited

    • This reply was modified 2 months, 3 weeks ago by nicmare.

    i installed imagemagick on localhost and changed plugin settings to imagemagick. unfortunately the plugin seems not to work at all. debug.log :
    [29-Nov-2019 09:41:39 UTC] WebP Converter: Server configuration: Imagick does not support WebP format.

    i then tried WEBP convertion with ewww plugin. this one works without warnings. getting nice webp files which works.

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Please read the plugin requirements for plugin configuration. You will find these in the FAQ.

Viewing 15 replies - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.