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 Drupal's image handling system.

See also

theme_image_style()

Related topics

1 string reference to 'image_example_style_form'
image_example_menu in image_example/image_example.module
Implements hook_menu().

File

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

Code

function image_example_style_form($form, &$form_state) {

  // If there is already an uploaded image display the image here.
  if ($image_fid = variable_get('image_example_image_fid', FALSE)) {
    $image = file_load($image_fid);
    $style = variable_get('image_example_style_name', 'thumbnail');
    $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 chosen below.'),
    '#default_value' => variable_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 function takes on paramater, a boolean flag
    // signifying whether or not the array should include a <none> option.
    '#options' => image_style_options(TRUE),
    '#default_value' => variable_get('image_example_style_name', ''),
  );

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