/[drupal]/contributions/modules/image_exact/image_exact.module
ViewVC logotype

Diff of /contributions/modules/image_exact/image_exact.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4.2.1, Sun May 27 20:19:50 2007 UTC revision 1.4.2.2, Tue May 29 05:27:44 2007 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: image_exact.module,v 1.3.2.2 2006/06/26 19:04:34 joshk Exp $
3    
4    /**
5     * Implementation of hook_help().
6     */
7    
8    function image_exact_help($section) {
9      switch ($section) {
10        case 'admin/modules#description':
11          return t("Implements exact-size image thumbnail and avatars via cropping, based on max h/w from the image content type settings.");
12      }
13    }
14    
15    /*
16    * Implementation of hook_menu
17    */
18    function image_exact_menu($may_cache) {
19      $items[] = array(
20        'path' => 'admin/settings/image_exact',
21        'title' => t('Image Exact'),
22        'description' => t('Configure the Image Exact module.'),
23        'callback' => 'drupal_get_form',
24        'callback arguments' => array('image_exact_admin_settings'),
25        'access' => user_access('administer site configuration'),
26        'type' => MENU_NORMAL_ITEM, // optional
27      );
28      return $items;
29    }
30    
31    function image_exact_admin_settings() {
32      if (!image_get_toolkit()) {
33        drupal_set_message('You must enable an image toolkit for this module to work!', 'error');
34      }
35      else {
36        $image_settings = l('image settings page', 'admin/settings/image');
37        $user_settings = l('user settings page', 'admin/user/settings');
38        $form['image_exact_nodes'] = array(
39          '#type' => 'fieldset',
40          '#title' => t('Image Node Settings'),
41          '#collapsible' => true,
42          '#collapsed' => false,
43        );
44        $form['image_exact_nodes']['image_exact_thumbs'] = array(
45          '#type' => 'checkbox',
46          '#title' => t('Use exact sizes for image-nodes'),
47          '#default_value' => variable_get('image_exact_thumbs', 1),
48          '#description' => t('If checked, this will force images of the size(s) checked below (as defined on the !link) to be exactly the size specified. Otherwise, the settings below will be ignored.', array('!link' => $image_settings)),
49        );
50        foreach (_image_get_sizes() as $count => $size) {
51          $options[$count] = $size['label'];
52        }
53        $form['image_exact_nodes']['image_exact_size'] = array(
54          '#type' => 'checkboxes',
55          '#title' => t('Specific Image Size Settings'),
56          '#default_value' => variable_get('image_exact_size', array(0)),
57          '#options' => $options,
58          '#description' => t('Each image size checked will be cropped and resized to the specified size defined in the !link. Note that existing images will not be resized until viewing that specific image edit tab, and possibly refreshing the browser.', array('!link' => $image_settings)),
59        );
60    
61        if (variable_get('image_exact_avatars', 0)) {
62          list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '85x85'));
63          $w = round($final_w / 2);
64          $h = round($final_h / 2);
65          $form['image_exact_warning'] = array(
66            '#title' => t('Exact Avatar Size'),
67            '#value' => t('NOTICE: your exact-size avatars will be sized at @w pixels wide and @h pixels tall. To change this enter DOUBLE your desired amount on the !link', array('!link' => $user_settings, '@w' => $w, '@h' => $h))
68          );
69        }
70        $form['image_exact_avatars'] = array(
71          '#type' => 'checkbox',
72          '#title' => t('Use exact sizes for avatars?'),
73          '#default_value' => variable_get('image_exact_avatars', 0),
74          '#description' => t('Because of how the avatar system works, you need to enter a value in the !link that is DOUBLE what you want the real avatar size to be. If you want 85x85 exact, check the above box, then change the setting on the !link to 170x170', array('!link' => $user_settings)),
75        );
76      }
77      return system_settings_form($form);
78    }
79    
80    /*
81    * Implementation of hook_nodeapi
82    *
83    * Looks for images and if the setting is set will resize thumbs
84    */
85    
86    function image_exact_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
87      //Set thumbnail final dimensions here - use settings from image content type.
88      if ($node->type == 'image' && $op == 'validate' && variable_get('image_exact_thumbs', 1)) {
89        $sizes = _image_get_sizes();
90        foreach(variable_get('image_exact_size', array(0)) as $i) {
91    //     for ($i = 0; $i < 5; $i++) {
92          $source = file_create_path($node->images['_original']);
93          $destination = file_create_path($node->images[$sizes[$i]['label']]);
94          $final_w = $sizes[$i]['width'];
95          $final_h = $sizes[$i]['height'];
96          if ($final_w && $final_h) {
97            image_exact_resize($source, $destination, $final_w, $final_h);
98          }
99        }
100      }
101    }
102    
103    /*
104    * Implementation of hook_user
105    * Handles exact-sizes for avatars
106    */
107    
108    function image_exact_user($op, &$edit, &$user, $category = NULL) {
109      if ($op == 'validate' && variable_get('image_exact_avatars', 0)) {
110        list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '190x190'));
111        $w = round($final_w / 2);
112        $h = round($final_h / 2);
113       image_exact_resize($edit['picture'], $edit['picture'], $w, $h);
114      }
115    }
116    
117    function image_exact_resize($source, $destination, $final_w, $final_h) {
118      if (file_exists($source)) {
119        $source_info = image_get_info($source);
120        $source_ar = $source_info['width'] / $source_info['height'];
121        if ($source_info['width'] > $final_w && $source_info['height'] > $final_h) {
122        // only proceed if we've got a big enough source file... don't stretch a tiny one
123          $final_ar = $final_w / $final_h;
124          if($source_ar > $final_ar) { //Too wide!
125            $width = round($source_info['width'] / ($source_ar / $final_ar));
126            $x = round(($source_info['width'] - $width) / 2); // Start the crop at the halfway point to retain center
127            $y = 0;
128            $height = $source_info['height'];
129            image_crop($source,$destination,$x,$y,$width,$height);
130            // drupal_set_message("Crop: $x,$y,$width,$height",'message');
131          } elseif ($source_ar < $final_ar) { // Too tall!
132            $height = round($source_info['height'] * ($source_ar / $final_ar));
133            $y = round(($source_info['height'] - $height) / 2); //Start the crop at the halfway point to retain center
134            $x = 0;
135            $width = $source_info['width'];
136            image_crop($source,$destination,$x,$y,$width,$height);
137            // drupal_set_message("Crop: $x,$y,$width,$height",'message');
138          }
139        }
140        image_resize($destination, $destination, $final_w, $final_h);
141        // drupal_set_message("Resize: $final_w, $final_h",'message');
142        if(!file_exists($destination)) {
143          drupal_set_message("Image_exact: Image resize failed.","error");
144        }
145      } else {
146        drupal_set_message('image_exact: File does not exist.','error');
147      }
148    }
149    ?>

Legend:
Removed from v.1.4.2.1  
changed lines
  Added in v.1.4.2.2

  ViewVC Help
Powered by ViewVC 1.1.2