Image Size Control

Elementor image size group control displays input fields to define one of the default image sizes (thumbnail, medium, medium_large, large) or custom image dimensions.

The control is defined in Group_Control_Image_Size class which extends Group_Control_Base class.

Note that when using group controls, the type should be set using the Group_Control_Image_Size::get_type() method.

Arguments

Name Type Default Description
type string image-size The type of the control.
label string The label that appears above of the field.
description string The description that appears below the field.
show_label bool true Whether to display the label.
label_block bool false Whether to display the label in a separate line.
separator string default Set the position of the control separator. Available values are defaultbeforeafter and nonedefault will position the separator depending on the control type. before / after will position the separator before/after the control. none will hide the separator.
include array Image sizes to include.
exclude array Image sizes to exclude.
default string The default image size.

Return Value

(array) An array containing the image size values.

[
	{name}_size => (string),
	{name}_custom_dimension => (string),
]

Usage

<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

	protected function _register_controls() {

		$this->start_controls_section(
			'content_section',
			[
				'label' => __( 'Content', 'plugin-name' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'image',
			[
				'label' => __( 'Choose Image', 'elementor' ),
				'type' => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$this->add_control(
			Group_Control_Image_Size::get_type(),
			[
				'name' => 'thumbnail', // // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `thumbnail_size` and `thumbnail_custom_dimension`.
				'exclude' => [ 'custom' ],
				'include' => [],
				'default' => 'large',
			]
		);

		$this->end_controls_section();

	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		echo Group_Control_Image_Size::get_attachment_image_html( $settings, 'thumbnail', 'image' );
	}

	protected function _content_template() {
		?>
		<#
		var image = {
			id: settings.image.id,
			url: settings.image.url,
			size: settings.thumbnail_size,
			dimension: settings.thumbnail_custom_dimension,
			model: view.getEditModel()
		};
		var image_url = elementor.imagesManager.getImageUrl( image );
		#>
		<img src="{{{ image_url }}}" />
		<?php
	}

}