3

I am trying to crop a 1000 x 648 image to 400 x 400. I use this code in functions.php

add_theme_support( 'post-thumbnails' );
add_image_size('shop-size', 400, 400, array(center,center) );
add_image_size('shop-size2', 401, 400, true );

then i go to the regenerate plugin and run it. While I run it I open up wp-content uploads to check out what's going on.

I watch as it creates a 400x259 image that is scaled. The regenerate plugin even says it is going to do a 400x400 cropped to fit for shop-size, and then proceeds not to.

I checked if gd is loaded with this and it returns 'gd loaded'

<?php if (extension_loaded('gd')) 
     { 
         echo "gd loaded"; 
     } else { 
         echo "not loaded"; 
} ?>

I also tried hooking it into after_theme_setup like this:

function add_custom_sizes() {
    add_image_size( 'map-size', 199, 199, array('center','center') );
    add_image_size('shop-size', 599, 599, array('center','center') );
    add_image_size('discover-size', 749, 620, array('center','center'));
    add_image_size( 'map-size1', 198, 199, true );
    add_image_size('shop-size1', 598, 599, true );
    add_image_size('discover-size1', 748, 620, true);
}

add_action('after_setup_theme','add_custom_sizes'); 

however as i regenerate it still makes a 198x165 instead of 198x199 (from 768 x 641)

The parent is called 'rise' by thrive themes. I contacted them but they said they don't have support for dev questions.

The parent only uses add_image_size once, i tried removing this but I still had the scaling instead of cropping issue.

I've included the rise files below. I have a hunch they do some kind of custom scaling thing? And i will have to over-ride this some how?

thrive image optimization file - https://pastebin.com/1QEa6YJv thrive functions - https://pastebin.com/pAqBt285

Can anyone help me figure out what i'm doing wrong? Thanks for any help.

  • array(center,center) is an array of two constants called center. These need to be strings: array('center','center'). Not sure if that's exactly the problem, but it stuck out. – Jacob Peattie Oct 2 '18 at 4:55
  • @JacobPeattie hey thanks for responding! i tried your suggestion but same thing happens, it scales instead of cropping – Tintinabulator Zea Oct 2 '18 at 5:19
  • And how about second image size? Does it create it correctly? – Krzysiek Dróżdż Oct 2 '18 at 5:25
  • 'wordpress.stackexchange.com/questions/35833/…' says you need to hook the function for adding images sizes on 'after_theme_support'. would mind to give a try ?? – VayuDev Oct 2 '18 at 6:59
  • 1
    I have three suggestions: (1) In Thrive options, there is thrive_get_theme_options( 'image_optimization_type' ). Can you reproduce the issue with this option turned Off? (2) Also, can you test this with another theme activated? (Just to make sure server settings are ok). (3) Is WP_DEBUG set to true in wp-config.php? It should be, just in case we are missing something here. – dboris Oct 23 '18 at 13:49
1

from your question i under that you want fixed size cropped images when you upload image, why don't you use wp_get_image_editor. I used it in my project where i wanted cropped images of fixed size so i did this code.

$path = $newPath['basedir'].'/newImgas/';
$cropfile = uniqid() . '.png';
$cropfilename = $path.$cropfile;
$cropImage = home_url(). '/wp-content/uploads/newImgas/'.$cropfile;
$c_image = wp_get_image_editor($newImage);
if ( ! is_wp_error($c_image) ) {
  $c_image->resize( 600, 600, TRUE);
  $c_image->save( $cropfilename );
}

You can find example here. Please reply me if it isn't what you wanted.

1

I think your problem could be, that you need to set the high priority (try 101, or even 999 if it solves):

add_action('after_setup_theme', 'add_custom_sizes',  101 ); 
1

The fact that GD is loaded doesn't mean it is actually used for handling the images. From the pastebin code you give I conclude that your theme comes bundled with the Kraken image optimizer (as a separate plugin or bundled with the theme in some other way).

Since using add_image_size only places the data in an array, it is up to the image handling function to actually use it. Normally this is done by the function wp_save_image, which allows for any image editor to be used (in the line where it says: $img = wp_get_image_editor).

So, probably the Kraken editor is ignoring the crop setting. You'll have to find the plugin settings and modify them, if your theme lets you. Kraken is a paid scheme, so there may be limitations.

Alternatively, you could use the wp_image_editors filter to change the image editor back to GD, but as I don't know how the theme uses Kraken I'm not sure what the effect of doing this would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.