• Resolved diegosomar

    (@diegosomar)


    I want to share a tip to crop ONLY specific image sizes. I’m share here because this solutions is not the “better solution”, but works. Let’s to code:

    In my theme functions.php file:

    
    function sc50k_check_size_to_crop($size){
    	$allowed_sizes = array('thumbnail', 'woocommerce_thumbnail', 'woocommerce_single', 'woocommerce_gallery_thumbnail', 'shop_catalog', 'shop_single', 'shop_thumbnail');
    	if ( !in_array($size, $allowed_sizes) ){
    		return false;
    	} else {
    		return true;
    	}
    }
    

    The code above verify if the $size in the crop moment is an $allowed_size. In the code, I’m using the Woocommerce sizes. I just want to crop product image sizes. And ONLY the thumbnails.

    In the plugin file plugins/square-thumbnails/admin/class-square-thumbnails-admin.php, search for the foreach($meta['sizes'] as $size=>$m){. This foreach must to be:

    
    foreach($meta['sizes'] as $size=>$m){
            $kontinue = sc50k_check_size_to_crop($size);
            if ($kontinue) {
              //$m['file']=$path->dir.$m['file'];
              $result=$this->create_square($m);                    
              $meta[$size]['width']=$result->sqW;
              $meta[$size]['height']=$result->sqH;
            }
        }
    

    This is a little change in the plugin file. It verifies if the current size (crop moment) is allowed. If returns true, continue with the square crop. If false, skip.

    IMPORTANT: In the first plugin update, this change will be removed. That’s the reason to be a temporary solution. For each plugin update, this foreach code must be adjusted with the code above.

    Sorry my bad english, I’m brazilian. If anything is wrong, forgive me.

Viewing 1 replies (of 1 total)
  • I need the same feature, its very useful to select which file sizes to be generated as square. Please tell do you plan to add this feature? I thing its simple to be updated… You can just add add_filter or add_action so users to set some additional settings to file sizes=

    Its very useful!!!

    • This reply was modified 5 years, 9 months ago by toto.
    • This reply was modified 5 years, 9 months ago by toto.
Viewing 1 replies (of 1 total)
  • The topic ‘[Contribution] Solution (temporary) to crop specific image sizes’ is closed to new replies.