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

Diff of /contributions/modules/image_filter/image_filter.module

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

revision 1.27, Sun Jun 7 14:36:41 2009 UTC revision 1.28, Thu Aug 6 11:54:28 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: image_filter.module,v 1.26 2007/10/20 20:17:15 davidhull Exp $  // $Id: image_filter.module,v 1.27 2009/06/07 14:36:41 raintonr Exp $
3    
4  function image_filter_short_tip_translated() {  function image_filter_short_tip_translated() {
5    return t(    return t(
# Line 94  function image_filter_attr_value($text, Line 94  function image_filter_attr_value($text,
94   * Actually execute filter on given text.   * Actually execute filter on given text.
95   */   */
96  function image_filter_process($text) {  function image_filter_process($text) {
   
97    // Find all the image codes and loop over them, replacing each with an img tag.    // Find all the image codes and loop over them, replacing each with an img tag.
98    preg_match_all("/\[image:(\d+)(\s*,)?\s*(.*?)\]/i", $text, $matches, PREG_SET_ORDER);    preg_match_all("/\[image:(\d+)(\s*,)?\s*(.*?)\]/i", $text, $matches, PREG_SET_ORDER);
99    foreach ($matches as $match) {    foreach ($matches as $match) {
# Line 124  function image_filter_process($text) { Line 123  function image_filter_process($text) {
123      $width = image_filter_attr_value($args['width'], IMAGE_FILTER_INTEGER);      $width = image_filter_attr_value($args['width'], IMAGE_FILTER_INTEGER);
124      $height = image_filter_attr_value($args['height'], IMAGE_FILTER_INTEGER);      $height = image_filter_attr_value($args['height'], IMAGE_FILTER_INTEGER);
125      $size = image_filter_attr_value($args['size'], IMAGE_FILTER_WORD);      $size = image_filter_attr_value($args['size'], IMAGE_FILTER_WORD);
     if ($size == 'original') { $size = '_original'; }  
     if (!$width && !$height && strlen($size) == 0) { $size = 'thumbnail'; }  
126    
127      $img = NULL;      $img = NULL;
128    
129  //    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, f.filename, f.filepath FROM {files} f, {node} n WHERE n.status = 1 AND f.filemime LIKE 'image/%%' AND f.nid = n.nid AND n.nid = %d" . (strlen($size) != 0 ? " AND f.filename IN ('" . db_escape_string($size) . "','_original')" : '')), $match[1]);  //    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, f.filename, f.filepath FROM {files} f, {node} n WHERE n.status = 1 AND f.filemime LIKE 'image/%%' AND f.nid = n.nid AND n.nid = %d" . (strlen($size) != 0 ? " AND f.filename IN ('" . db_escape_string($size) . "','_original')" : '')), $match[1]);
130        /* TODO: handle fieldname and delta for imagefields */
131      $node = node_load($match[1], NULL, TRUE);      $node = node_load($match[1], NULL, TRUE);
132      // Loop over the files found for this image and select the best match.  
133      //while ($i = db_fetch_object($result)) {      if (empty($node->images)) {
134      foreach( $node->images as $key => $img_path ) {        /* This is not an image node (as defined by the image.module). */
135        $i->filepath = $img_path;        if (module_exists('imagecache') && module_exists('imagefield')) {
136        $i->filename = $key;          /* So look for imagefield */
137        $i->nid = $node->nid;          $fields = content_fields(NULL, $node->type);
138        $i->title = $node->title;          foreach ($fields as $field_name => $field) {
139        $i->size = getimagesize(file_create_path($i->filepath));            if ($field['widget']['module'] == 'imagefield') {
140        if (! $img) {              if (!empty($node->$field_name)) {
141          $img = $i;                $node_field = $node->$field_name;
142        }                $filepath = $node_field[0]['filepath'];
143        // If we find a match for the desired image size label, take it.                $img->filepath = $filepath;
144        if ((strlen($size) != 0) && ($i->filename == $size)) {  
145          $img = $i;                /* Get requested size */
146          break;                $preset = imagecache_preset_by_name($size);
147        }                if (empty($preset)) {
148        // If we find a better match for the desired image width or height, tentatively take it.                  /* Requested doesn't exits so fetch them all */
149        if ($width && $i->size[0] >= $width && $i->size[1] >= $height) {                  $request_size = $size;
150          if ($i->size[0] < $img->size[0] || $img->size[0] < $width) {                  $presets = imagecache_presets();
151            $img = $i;                } else {
152                    $presets[$preset['presetid']] = $preset;
153                  }
154    
155                  /* Find smallest preset - this will be the named on if that was found */
156                  /* TODO: have configurable default */
157                  $src = $filepath;
158                  foreach ($presets as $preset) {
159                    $dst = imagecache_create_path($preset['presetname'], $filepath);
160                    imagecache_build_derivative($preset['actions'], $src, $dst);
161                    $image = image_get_info($dst);
162                    if (empty($width) ||
163                        $image ['width'] * $image['height'] < $width * $height) {
164                      $size = $preset['presetname'];
165                      $width = $image ['width'];
166                      $height = $image ['height'];
167                    }
168                  }
169    
170                  $img->filename = $size;
171                  $img->fileurl = imagecache_create_url($size, $filepath);
172                  $img->nid = $node->nid;
173                  $img->title = $node->title;
174    
175                  /* Warn the user if preset wasn't found */
176                  if ($size != $request_size && !empty($request_size)) {
177                    drupal_set_message(
178                      t('Preset %request_size unavailable - using %size.',
179                        array('%request_size' => $request_size, '%size' => $size)), 'warning');
180                  }
181                }
182              }
183          }          }
184        }        }
185        if ($height && $i->size[1] >= $height && $i->size[0] >= $width) {      } else {
186          if ($i->size[1] < $img->size[1] || $img->size[1] < $height) {        /* Find most appropriate image size*/
187          if ($size == 'original') { $size = '_original'; }
188          if (!$width && !$height && strlen($size) == 0) { $size = 'thumbnail'; }
189          // Loop over the files found for this image and select the best match.
190          foreach($node->images as $key => $img_path) {
191            $i->filepath = $img_path;
192            $i->filename = $key;
193            $i->fileurl = file_create_url($img_path);
194            $i->nid = $node->nid;
195            $i->title = $node->title;
196            $i->size = getimagesize(file_create_path($i->filepath));
197            if (! $img) {
198              $img = $i;
199            }
200            // If we find a match for the desired image size label, take it.
201            if ((strlen($size) != 0) && ($i->filename == $size)) {
202            $img = $i;            $img = $i;
203              break;
204            }
205            // If we find a better match for the desired image width or height, tentatively take it.
206            if ($width && $i->size[0] >= $width && $i->size[1] >= $height) {
207              if ($i->size[0] < $img->size[0] || $img->size[0] < $width) {
208                $img = $i;
209              }
210            }
211            if ($height && $i->size[1] >= $height && $i->size[0] >= $width) {
212              if ($i->size[1] < $img->size[1] || $img->size[1] < $height) {
213                $img = $i;
214              }
215          }          }
216        }        }
217      }      }
218    
219      // If we found a matching image, replace the image code with an <img> tag.      // If we found a matching image, replace the image code with an <img> tag.
220      if ($img) {      if ($img) {
       $img->fileurl = file_create_url($img->filepath);  
   
221        if (! $width && ! $height) {        if (! $width && ! $height) {
222          $img->width = $img->size[0];          $img->width = $img->size[0];
223          $img->height = $img->size[1];          $img->height = $img->size[1];

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28

  ViewVC Help
Powered by ViewVC 1.1.2