1 image_example.pages.inc image_example_style_form($form, &$form_state)

Form for uploading and displaying an image using selected style.

This page provides a form that allows the user to upload an image and choose a style from the list of defined image styles to use when displaying the the image. This serves as an example of integrating image styles with your module and as a way to demonstrate that the styles and effects defined by this module are available via Backdrop's image handling system.

See also

theme_image_style()

Related topics

File

modules/examples/image_example/image_example.pages.inc, line 21
Page/form showing image styles in use.

Code

function image_example_style_form($form, &$form_state) {
  $config = config('image_example.settings');

  // If there is already an uploaded image display the image here.
  if ($image_fid = $config->get('image_example_image_fid')) {
    $image = file_load($image_fid);
    $style = $config->get('image_example_style_name');
    $form['image'] = array(
      '#markup' => theme('image_example_image', array('image' => $image, 'style' => $style)),
    );
  }

  // Use the #managed_file FAPI element to upload an image file.
  $form['image_example_image_fid'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
    '#default_value' => $config->get('image_example_image_fid'),
    '#upload_location' => 'public://image_example_images/',
  );

  // Provide a select field for choosing an image style to use when displaying
  // the image.
  $form['image_example_style_name'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#description' => t('Choose an image style to use when displaying this image.'),
    // The image_style_options() function returns an array of all available
    // image styles both the key and the value of the array are the image
    // style's name. The fucntion takes on paramater, a boolean flag
    // signifying wether or not the array should include a <none> option.
    '#options' => image_style_options(TRUE),
    '#default_value' => $config->get('image_example_style_name'),
  );

  // Submit Button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}