- <?php
- * @file
- * Page/form showing image styles in use.
- */
-
- * 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 theme_image_style()
- *
- * @ingroup image_example
- */
- function image_example_style_form($form, &$form_state) {
- $config = config('image_example.settings');
-
-
- 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)),
- );
- }
-
-
- $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/',
- );
-
-
-
- $form['image_example_style_name'] = array(
- '#title' => t('Image style'),
- '#type' => 'select',
- '#description' => t('Choose an image style to use when displaying this image.'),
-
-
-
-
- '#options' => image_style_options(TRUE),
- '#default_value' => $config->get('image_example_style_name'),
- );
-
-
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- );
-
- return $form;
- }
-
- * Verifies that the user supplied an image with the form..
- *
- * @ingroup image_example
- */
- function image_example_style_form_validate($form, &$form_state) {
- if (!isset($form_state['values']['image_example_image_fid']) || !is_numeric($form_state['values']['image_example_image_fid'])) {
- form_set_error('image_example_image_fid', t('Please select an image to upload.'));
- }
- }
-
- * Form Builder; Display a form for uploading an image.
- *
- * @ingroup image_example
- */
- function image_example_style_form_submit($form, &$form_state) {
- $config = config('image_example.settings');
-
-
-
-
-
-
- if ($form_state['values']['image_example_image_fid'] != 0) {
-
-
-
- $file = file_load($form_state['values']['image_example_image_fid']);
- $file->status = FILE_STATUS_PERMANENT;
- file_save($file);
-
-
-
- file_usage_add($file, 'image_example', 'sample_image', 1);
-
-
- $config->set('image_example_image_fid', $file->fid);
- backdrop_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'],
- )
- ));
- }
-
-
- elseif ($form_state['values']['image_example_image_fid'] == 0) {
-
- $fid = $config->get('image_example_image_fid');
- $file = $fid ? file_load($fid) : FALSE;
- if ($file) {
-
-
- file_usage_delete($file, 'image_example', 'sample_image', 1);
-
-
-
-
- file_delete($file->fid);
- }
-
-
-
-
- $config->set('image_example_image_fid', FALSE);
- backdrop_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename)));
- }
-
-
- $config->set('image_example_style_name', $form_state['values']['image_example_style_name']);
- $config->save();
- }
-
- * Theme function displays an image rendered using the specified style.
- *
- * @ingroup image_example
- */
- function theme_image_example_image($variables) {
- $image = $variables['image'];
- $style = $variables['style'];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $output = theme('image_style',
- array(
- 'style_name' => $style,
- 'uri' => $image->uri,
- )
- );
- $output .= '<p>' . t('This image is being displayed using the image style %style_name.', array('%style_name' => $style)) . '</p>';
- return $output;
- }