/[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.3.2.1, Sat May 13 20:56:36 2006 UTC revision 1.3.2.2, Mon Jun 26 19:04:34 2006 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: image_exact.module,v 1.3 2006/05/13 20:56:36 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_settings
17    */
18    
19    function image_exact_settings() {
20      if (!image_get_toolkit()) {
21        drupal_set_message('You must enable an image toolkit for this module to work!', 'error');
22      }
23      else {
24        $image_settings = l('image settings page', 'admin/settings/image');
25        $user_settings = l('user settings page', 'admin/settings/user');
26        $form['image_exact_nodes'] = array(
27          '#type' => 'fieldset',
28          '#title' => t('Image Node Settings'),
29          '#collapsible' => true,
30          '#collapsed' => false,
31        );
32        $form['image_exact_nodes']['image_exact_thumbs'] = array(
33          '#type' => 'checkbox',
34          '#title' => t('Use exact sizes for image-nodes'),
35          '#default_value' => variable_get('image_exact_thumbs', 1),
36          '#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)),
37        );
38        foreach (_image_get_sizes() as $count => $size) {
39          $options[$count] = $size['label'];
40        }
41        $form['image_exact_nodes']['image_exact_size'] = array(
42          '#type' => 'checkboxes',
43          '#title' => t('Specific Image Size Settings'),
44          '#default_value' => variable_get('image_exact_size', array(0)),
45          '#options' => $options,
46          '#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)),
47        );
48    
49        if (variable_get('image_exact_avatars', 0)) {
50          list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '85x85'));
51          $w = round($final_w / 2);
52          $h = round($final_h / 2);
53          $form['image_exact_warning'] = array(
54            '#title' => t('Exact Avatar Size'),
55            '#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))
56          );
57        }
58        $form['image_exact_avatars'] = array(
59          '#type' => 'checkbox',
60          '#title' => t('Use exact sizes for avatars?'),
61          '#default_value' => variable_get('image_exact_avatars', 0),
62          '#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)),
63        );
64      }
65      return $form;
66    }
67    
68    /*
69    * Implementation of hook_nodeapi
70    *
71    * Looks for images and if the setting is set will resize thumbs
72    */
73    
74    function image_exact_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
75      //Set thumbnail final dimensions here - use settings from image content type.
76      if ($node->type == 'image' && $op == 'validate' && variable_get('image_exact_thumbs', 1)) {
77        $sizes = _image_get_sizes();
78        foreach(variable_get('image_exact_size', array(0)) as $i) {
79    //     for ($i = 0; $i < 5; $i++) {
80          $source = file_create_path($node->images['_original']);
81          $destination = file_create_path($node->images[$sizes[$i]['label']]);
82          $final_w = $sizes[$i]['width'];
83          $final_h = $sizes[$i]['height'];
84          if ($final_w && $final_h) {
85            image_exact_resize($source, $destination, $final_w, $final_h);
86          }
87        }
88      }
89    }
90    
91    /*
92    * Implementation of hook_user
93    * Handles exact-sizes for avatars
94    */
95    
96    function image_exact_user($op, &$edit, &$user, $category = NULL) {
97      if ($op == 'validate' && variable_get('image_exact_avatars', 0)) {
98        list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '190x190'));
99        $w = round($final_w / 2);
100        $h = round($final_h / 2);
101       image_exact_resize($edit['picture'], $edit['picture'], $w, $h);
102      }
103    }
104    
105    function image_exact_resize($source, $destination, $final_w, $final_h) {
106      if (file_exists($source)) {
107        $source_info = image_get_info($source);
108        $source_ar = $source_info['width'] / $source_info['height'];
109        if ($source_info['width'] > $final_w && $source_info['height'] > $final_h) {
110        // only proceed if we've got a big enough source file... don't stretch a tiny one
111          $final_ar = $final_w / $final_h;
112          if($source_ar > $final_ar) { //Too wide!
113            $width = round($source_info['width'] / ($source_ar / $final_ar));
114            $x = round(($source_info['width'] - $width) / 2); // Start the crop at the halfway point to retain center
115            $y = 0;
116            $height = $source_info['height'];
117            image_crop($source,$destination,$x,$y,$width,$height);
118            // drupal_set_message("Crop: $x,$y,$width,$height",'message');
119          } elseif ($source_ar < $final_ar) { // Too tall!
120            $height = round($source_info['height'] * ($source_ar / $final_ar));
121            $y = round(($source_info['height'] - $height) / 2); //Start the crop at the halfway point to retain center
122            $x = 0;
123            $width = $source_info['width'];
124            image_crop($source,$destination,$x,$y,$width,$height);
125            // drupal_set_message("Crop: $x,$y,$width,$height",'message');
126          }
127        }
128        image_resize($destination, $destination, $final_w, $final_h);
129        // drupal_set_message("Resize: $final_w, $final_h",'message');
130        if(!file_exists($destination)) {
131          drupal_set_message("Image_exact: Image resize failed.","error");
132        }
133      } else {
134        drupal_set_message('image_exact: File does not exist.','error');
135      }
136    }
137    ?>

Legend:
Removed from v.1.3.2.1  
changed lines
  Added in v.1.3.2.2

  ViewVC Help
Powered by ViewVC 1.1.2