image_example.pages.inc

  1. examples
    1. 7 image_example/image_example.pages.inc
    2. 8 image_example/image_example.pages.inc

Page/form showing image styles in use.

Functions & methods

NameDescription
image_example_style_formForm for uploading and displaying an image using selected style.
image_example_style_form_submitForm Builder; Display a form for uploading an image.
image_example_style_form_validateVerifies that the user supplied an image with the form..
theme_image_example_imageTheme function displays an image rendered using the specified style.

File

image_example/image_example.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Page/form showing image styles in use.
  5. */
  6. /**
  7. * Form for uploading and displaying an image using selected style.
  8. *
  9. * This page provides a form that allows the user to upload an image and choose
  10. * a style from the list of defined image styles to use when displaying the
  11. * the image. This serves as an example of integrating image styles with your
  12. * module and as a way to demonstrate that the styles and effects defined by
  13. * this module are available via Drupal's image handling system.
  14. *
  15. * @see theme_image_style()
  16. *
  17. * @ingroup image_example
  18. */
  19. function image_example_style_form($form, &$form_state) {
  20. // If there is already an uploaded image display the image here.
  21. if ($image_fid = variable_get('image_example_image_fid', FALSE)) {
  22. $image = file_load($image_fid);
  23. $style = variable_get('image_example_style_name', 'thumbnail');
  24. $form['image'] = array(
  25. '#markup' => theme('image_example_image', array('image' => $image, 'style' => $style)),
  26. );
  27. }
  28. // Use the #managed_file FAPI element to upload an image file.
  29. $form['image_example_image_fid'] = array(
  30. '#title' => t('Image'),
  31. '#type' => 'managed_file',
  32. '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
  33. '#default_value' => variable_get('image_example_image_fid', ''),
  34. '#upload_location' => 'public://image_example_images/',
  35. );
  36. // Provide a select field for choosing an image style to use when displaying
  37. // the image.
  38. $form['image_example_style_name'] = array(
  39. '#title' => t('Image style'),
  40. '#type' => 'select',
  41. '#description' => t('Choose an image style to use when displaying this image.'),
  42. // The image_style_options() function returns an array of all available
  43. // image styles both the key and the value of the array are the image
  44. // style's name. The fucntion takes on paramater, a boolean flag
  45. // signifying wether or not the array should include a <none> option.
  46. '#options' => image_style_options(TRUE),
  47. '#default_value' => variable_get('image_example_style_name', ''),
  48. );
  49. // Submit Button.
  50. $form['submit'] = array(
  51. '#type' => 'submit',
  52. '#value' => t('Save'),
  53. );
  54. return $form;
  55. }
  56. /**
  57. * Verifies that the user supplied an image with the form..
  58. *
  59. * @ingroup image_example
  60. */
  61. function image_example_style_form_validate($form, &$form_state) {
  62. if (!isset($form_state['values']['image_example_image_fid']) || !is_numeric($form_state['values']['image_example_image_fid'])) {
  63. form_set_error('image_example_image_fid', t('Please select an image to upload.'));
  64. }
  65. }
  66. /**
  67. * Form Builder; Display a form for uploading an image.
  68. *
  69. * @ingroup image_example
  70. */
  71. function image_example_style_form_submit($form, &$form_state) {
  72. // When using the #managed_file form element the file is automatically
  73. // uploaded an saved to the {file} table. The value of the corresponding
  74. // form element is set to the {file}.fid of the new file.
  75. // If fid is not 0 we have a valid file.
  76. if ($form_state['values']['image_example_image_fid'] != 0) {
  77. // The new file's status is set to 0 or temporary and in order to ensure
  78. // that the file is not removed after 6 hours we need to change it's status
  79. // to 1. Save the ID of the uploaded image for later use.
  80. $file = file_load($form_state['values']['image_example_image_fid']);
  81. $file->status = FILE_STATUS_PERMANENT;
  82. file_save($file);
  83. // When a module is managing a file, it must manage the usage count.
  84. // Here we increment the usage count with file_usage_add().
  85. file_usage_add($file, 'image_example', 'sample_image', 1);
  86. // Save the fid of the file so that the module can reference it later.
  87. variable_set('image_example_image_fid', $file->fid);
  88. drupal_set_message(t('The image @image_name was uploaded and saved with an ID of @fid and will be displayed using the style @style.', array('@image_name' => $file->filename, '@fid' => $file->fid, '@style' => $form_state['values']['image_example_style_name'])));
  89. }
  90. // If the file was removed we need to remove the module's reference to the
  91. // removed file's fid, and remove the file.
  92. elseif ($form_state['values']['image_example_image_fid'] == 0) {
  93. // Retrieve the old file's id.
  94. $fid = variable_get('image_example_image_fid', FALSE);
  95. $file = $fid ? file_load($fid) : FALSE;
  96. if ($file) {
  97. // When a module is managing a file, it must manage the usage count.
  98. // Here we decrement the usage count with file_usage_delete().
  99. file_usage_delete($file, 'image_example', 'sample_image', 1);
  100. // The file_delete() function takes a file object and checks to see if
  101. // the file is being used by any other modules. If it is the delete
  102. // operation is cancelled, otherwise the file is deleted.
  103. file_delete($file);
  104. }
  105. // Either way the module needs to update it's reference since even if the
  106. // file is in use by another module and not deleted we no longer want to
  107. // use it.
  108. variable_set('image_example_image_fid', FALSE);
  109. drupal_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename)));
  110. }
  111. // Save the name of the image style choosen by the user.
  112. variable_set('image_example_style_name', $form_state['values']['image_example_style_name']);
  113. }
  114. /**
  115. * Theme function displays an image rendered using the specified style.
  116. *
  117. * @ingroup image_example
  118. */
  119. function theme_image_example_image($variables) {
  120. $image = $variables['image'];
  121. $style = $variables['style'];
  122. // theme_image_style() is the primary method for displaying images using
  123. // one of the defined styles. The $variables array passed to the theme
  124. // contains the following two important values:
  125. // 'style_name': the name of the image style to use when displaying the
  126. // image.
  127. // 'path': the $file->uri of the image to display.
  128. // When given a style and an image path the function will first determine
  129. // if a derivative image already exists, in which case the existing image
  130. // will be displayed. If the derivative image does not already exist the
  131. // function returns an <img> tag with a specially crafted callback URL
  132. // as the src attribute for the tag. When accessed, the callback URL will
  133. // generate the derivative image and serve it to the browser.
  134. $output = theme('image_style', array('style_name' => $style, 'path' => $image->uri, 'getsize' => FALSE));
  135. $output .= '<p>' . t('This image is being displayed using the image style %style_name.', array('%style_name' => $style)) . '</p>';
  136. return $output;
  137. }
Login or register to post comments