name = 'image_crop';
/*
* label (string) Multiple words, can include spaces, visible when selecting a field type
*/
$this->label = __('Image with user-crop', 'acf-image_crop');
/*
* category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
*/
$this->category = 'content';
/*
* defaults (array) Array of default settings which are merged into the field object. These are used later in settings
*/
$this->defaults = array(
'force_crop' => 'no',
'crop_type' => 'hard',
'preview_size' => 'medium',
'save_format' => 'id',
'save_in_media_library' => 'yes',
'target_size' => 'thumbnail',
'library' => 'all',
'retina_mode' => 'no'
);
$this->options = get_option( 'acf_image_crop_settings' );
// add ajax action to be able to retrieve full image size via javascript
add_action( 'wp_ajax_acf_image_crop_get_image_size', array( &$this, 'crop_get_image_size' ) );
add_action( 'wp_ajax_acf_image_crop_perform_crop', array( &$this, 'perform_crop' ) );
// add filter to media query function to hide cropped images from media library
add_filter('ajax_query_attachments_args', array($this, 'filterMediaQuery'));
// Register extra fields on the media settings page on admin_init
add_action('admin_init', array($this, 'registerSettings'));
/*
* l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via:
* var message = acf._e('image_crop', 'error');
*/
$this->l10n = array(
'error' => __('Error! Please enter a higher value', 'acf-image_crop'),
);
// do not delete!
acf_field::__construct();
//parent::__construct();
}
// AJAX handler for retieving full image dimensions from ID
public function crop_get_image_size()
{
$img = wp_get_attachment_image_src( $_POST['image_id'], 'full');
if($img){
echo json_encode( array(
'url' => $img[0],
'width' => $img[1],
'height' => $img[2]
) );
}
exit;
}
/*
* render_field_settings()
*
* Create extra settings for your field. These are visible when editing a field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field_settings( $field ) {
/*
* acf_render_field_setting
*
* This function will create a setting for your field. Simply pass the $field parameter and an array of field settings.
* The array of settings does not require a `value` or `prefix`; These settings are found from the $field array.
*
* More than one setting can be added by copy/paste the above code.
* Please note that you must also have a matching $defaults value for the field name (font_size)
*/
// crop_type
acf_render_field_setting( $field, array(
'label' => __('Crop type','acf-image_crop'),
'instructions' => __('Select the type of crop the user should perform','acf-image_crop'),
'type' => 'select',
'name' => 'crop_type',
'layout' => 'horizontal',
'class' => 'crop-type-select',
'choices' => array(
'hard' => __('Hard crop', 'acf-image_crop'),
'min' => __('Minimal dimensions', 'acf-image_crop')
)
));
// target_size
$sizes = acf_get_image_sizes();
$sizes['custom'] = __('Custom size', 'acf-image_crop');
acf_render_field_setting( $field, array(
'label' => __('Target size','acf-image_crop'),
'instructions' => __('Select the target size for this field' . ($this->getOption('retina_mode') ? '
Retina mode is enabled - user will be required to select an image twice this size' : ''),'acf-image_crop'),
'type' => 'select',
'name' => 'target_size',
'class' => 'target-size-select',
'choices' => $sizes
));
// width - conditional: target_size == 'custom'
acf_render_field_setting( $field, array(
'label' => __('Custom target width','acf-image_crop'),
'instructions' => __('Leave blank for no restriction (does not work with hard crop option)','acf-image_crop'),
'type' => 'number',
'name' => 'width',
'class' => 'custom-target-width custom-target-dimension'
));
// height - conditional: target_size == 'custom'
acf_render_field_setting( $field, array(
'label' => __('Custom target height','acf-image_crop'),
'instructions' => __('Leave blank for no restriction (does not work with hard crop option)','acf-image_crop'),
'type' => 'number',
'name' => 'height',
'class' => 'custom-target-height custom-target-dimension'
));
// preview_size
acf_render_field_setting( $field, array(
'label' => __('Preview size','acf-image_crop'),
'instructions' => __('Select the preview size for this field','acf-image_crop'),
'type' => 'select',
'name' => 'preview_size',
'choices' => acf_get_image_sizes()
));
// force_crop
acf_render_field_setting( $field, array(
'label' => __('Force crop','acf-image_crop'),
'instructions' => __('Force the user to crop the image as soon at it is selected','acf-image_crop'),
'type' => 'radio',
'layout' => 'horizontal',
'name' => 'force_crop',
'choices' => array('yes' => 'Yes', 'no' => 'No')
));
// save_in_media_library
acf_render_field_setting( $field, array(
'label' => __('Save cropped image to media library','acf-image_crop'),
'instructions' => __('If the cropped image is not saved in the media library, "Image URL" is the only available return value.','acf-image_crop'),
'type' => 'radio',
'layout' => 'horizontal',
'name' => 'save_in_media_library',
'class' => 'save-in-media-library-select',
'choices' => array('yes' => 'Yes', 'no' => 'No')
));
// retina mode
$retina_instructions = __('Require and crop double the size set for this image. Enable this if you are using plugins like WP Retina 2x.','acf-image_crop');
if($this->getOption('retina_mode')){
$retina_instructions .= ' ' . __('NB. You currently have enabled retina mode globally for all fields through settings, which will override this setting.','acf-image_crop');
}
acf_render_field_setting( $field, array(
'label' => __('Retina/@2x mode ','acf-image_crop'),
'instructions' => $retina_instructions,
'type' => 'radio',
'layout' => 'horizontal',
'name' => 'retina_mode',
'choices' => array('yes' => 'Yes', 'no' => 'No')
));
// return_format
acf_render_field_setting( $field, array(
'label' => __('Return Value','acf-image_crop'),
'instructions' => __('Specify the returned value on front end','acf-image_crop'),
'type' => 'radio',
'name' => 'save_format',
'layout' => 'horizontal',
'class' => 'return-value-select',
'choices' => array(
'url' => __("Image URL",'acf'),
'id' => __("Image ID",'acf'),
'object' => __("Image Object",'acf')
)
));
// library
acf_render_field_setting( $field, array(
'label' => __('Library','acf'),
'instructions' => __('Limit the media library choice','acf'),
'type' => 'radio',
'name' => 'library',
'layout' => 'horizontal',
'choices' => array(
'all' => __('All', 'acf'),
'uploadedTo' => __('Uploaded to post', 'acf')
)
));
}
/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field (array) the $field being rendered
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field( $field ) {
// enqueue
acf_enqueue_uploader();
// get data from value
//$data = json_decode($field['value']);
$imageData = $this->get_image_data($field);
$url = '';
$orignialImage = null;
if($imageData->original_image){
$originalImage = wp_get_attachment_image_src($imageData->original_image, 'full');
$url = $imageData->preview_image_url;
}
$width = 0;
$height = 0;
if($field['target_size'] == 'custom'){
$width = $field['width'];
$height = $field['height'];
}
else{
global $_wp_additional_image_sizes;
$s = $field['target_size'];
if (isset($_wp_additional_image_sizes[$s])) {
$width = intval($_wp_additional_image_sizes[$s]['width']);
$height = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$width = get_option($s.'_size_w');
$height = get_option($s.'_size_h');
}
}
// Retina mode
if($this->getOption('retina_mode') || $field['retina_mode'] == 'yes'){
$width = $width * 2;
$height = $height * 2;
}
// vars
$div_atts = array(
'class' => 'acf-image-uploader acf-cf acf-image-crop',
'data-crop_type' => $field['crop_type'],
'data-target_size' => $field['target_size'],
'data-width' => $width,
'data-height' => $height,
'data-force_crop' => $field['force_crop'] == 'yes' ? 'true' : 'false',
'data-save_to_media_library' => $field['save_in_media_library'],
'data-save_format' => $field['save_format'],
'data-preview_size' => $field['preview_size'],
'data-library' => $field['library']
);
$input_atts = array(
'type' => 'hidden',
'name' => $field['name'],
'value' => htmlspecialchars($field['value']),
'data-name' => 'value-id',
'data-original-image' => $imageData->original_image,
'data-cropped-image' => json_encode($imageData->cropped_image),
'class' => 'acf-image-value'
);
// has value?
if($imageData->original_image){
$url = $imageData->preview_image_url;
$div_atts['class'] .= ' has-value';
}
?>