Crop image from get_theme_mod Customizer field

I’m using the following code to allow a user to upload a custom image (in addition to header image) through the customizer. Is there a way to crop the image when displaying it?

$wp_customize->add_setting( 'intro-img',
array (
'default' => 'http://example.com/image.png',
)
);

$wp_customize->add_control(
new WP_Customize_Image_Control(
    $wp_customize,
    'intro-img',
    array(
        'label' => 'Intro Image',
        'section' => 'section_one',
        'settings' => 'intro-img'

    )
    )
);

and displaying with:

<?php echo get_theme_mod( 'intro-img', 'http://example.com/image.png' ); ?>

1 Answer
1

With your method it would be very tricky, if it’s possible at all. What you could do is use a custom header with flexible dimensions. That would allow the user to upload the header image and then crop it how ever they needed easily.

// Register Theme Features
function custom_theme_features()  {

// Add theme support for Custom Header
$header_args = array(
    'default-image'          => 'http://example.com/image.png',
    'width'                  => 0,
    'height'                 => 0,
    'flex-width'             => true,
    'flex-height'            => true,
    'random-default'         => false,
    'header-text'            => false,
    'default-text-color'     => '',
    'uploads'                => true,

);
add_theme_support( 'custom-header', $header_args );
}

// Hook into the 'after_setup_theme' action
add_action( 'after_setup_theme', 'custom_theme_features' );

Leave a Comment