image_example.pages.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 chosen 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 function takes on paramater, a boolean flag
  45. // signifying whether 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. //
  76. // If fid is not 0 we have a valid file.
  77. if ($form_state['values']['image_example_image_fid'] != 0) {
  78. // The new file's status is set to 0 or temporary and in order to ensure
  79. // that the file is not removed after 6 hours we need to change it's status
  80. // to 1. Save the ID of the uploaded image for later use.
  81. $file = file_load($form_state['values']['image_example_image_fid']);
  82. $file->status = FILE_STATUS_PERMANENT;
  83. file_save($file);
  84. // When a module is managing a file, it must manage the usage count.
  85. // Here we increment the usage count with file_usage_add().
  86. file_usage_add($file, 'image_example', 'sample_image', 1);
  87. // Save the fid of the file so that the module can reference it later.
  88. variable_set('image_example_image_fid', $file->fid);
  89. 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.',
  90. array(
  91. '@image_name' => $file->filename,
  92. '@fid' => $file->fid,
  93. '@style' => $form_state['values']['image_example_style_name'],
  94. )
  95. ));
  96. }
  97. // If the file was removed we need to remove the module's reference to the
  98. // removed file's fid, and remove the file.
  99. elseif ($form_state['values']['image_example_image_fid'] == 0) {
  100. // Retrieve the old file's id.
  101. $fid = variable_get('image_example_image_fid', FALSE);
  102. $file = $fid ? file_load($fid) : FALSE;
  103. if ($file) {
  104. // When a module is managing a file, it must manage the usage count.
  105. // Here we decrement the usage count with file_usage_delete().
  106. file_usage_delete($file, 'image_example', 'sample_image', 1);
  107. // The file_delete() function takes a file object and checks to see if
  108. // the file is being used by any other modules. If it is the delete
  109. // operation is cancelled, otherwise the file is deleted.
  110. file_delete($file);
  111. }
  112. // Either way the module needs to update it's reference since even if the
  113. // file is in use by another module and not deleted we no longer want to
  114. // use it.
  115. variable_set('image_example_image_fid', FALSE);
  116. drupal_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename)));
  117. }
  118. // Save the name of the image style chosen by the user.
  119. variable_set('image_example_style_name', $form_state['values']['image_example_style_name']);
  120. }
  121. /**
  122. * Theme function displays an image rendered using the specified style.
  123. *
  124. * @ingroup image_example
  125. */
  126. function theme_image_example_image($variables) {
  127. $image = $variables['image'];
  128. $style = $variables['style'];
  129. // theme_image_style() is the primary method for displaying images using
  130. // one of the defined styles. The $variables array passed to the theme
  131. // contains the following two important values:
  132. // - 'style_name': the name of the image style to use when displaying the
  133. // image.
  134. // - 'path': the $file->uri of the image to display.
  135. //
  136. // When given a style and an image path the function will first determine
  137. // if a derivative image already exists, in which case the existing image
  138. // will be displayed. If the derivative image does not already exist the
  139. // function returns an <img> tag with a specially crafted callback URL
  140. // as the src attribute for the tag. When accessed, the callback URL will
  141. // generate the derivative image and serve it to the browser.
  142. $output = theme('image_style',
  143. array(
  144. 'style_name' => $style,
  145. 'path' => $image->uri,
  146. 'getsize' => FALSE,
  147. )
  148. );
  149. $output .= '<p>' . t('This image is being displayed using the image style %style_name.', array('%style_name' => $style)) . '</p>';
  150. return $output;
  151. }