/[drupal]/contributions/modules/imagecache_actions/canvasactions.inc
ViewVC logotype

Diff of /contributions/modules/imagecache_actions/canvasactions.inc

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

revision 1.1.4.18, Sat Sep 5 02:11:58 2009 UTC revision 1.1.4.19, Fri Oct 30 14:03:54 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: canvasactions.inc,v 1.1.4.17 2009/08/31 04:12:20 dman Exp $  // $Id: canvasactions.inc,v 1.1.4.18 2009/09/05 02:11:58 dman Exp $
3  /**  /**
4   * @file Helper functions for the text2canvas action for imagecache   * @file Helper functions for the text2canvas action for imagecache
5   *   *
# Line 172  function theme_canvasactions_definecanva Line 172  function theme_canvasactions_definecanva
172   *   $image->resource handle on the image object   *   $image->resource handle on the image object
173   */   */
174  function canvasactions_definecanvas_image(&$image, $action = array()) {  function canvasactions_definecanvas_image(&$image, $action = array()) {
   if ($image->toolkit != 'imageapi_gd') {  
     drupal_set_message("Unable to define canvas with {$image->toolkit}");  
     return FALSE;  
   }  
175    
176    // May be given either exact or relative dimensions.    // May be given either exact or relative dimensions.
177    if ($action['exact']['width'] || $action['exact']['width']) {    if ($action['exact']['width'] || $action['exact']['width']) {
# Line 197  function canvasactions_definecanvas_imag Line 193  function canvasactions_definecanvas_imag
193      $targetsize['left'] = $action['relative']['leftdiff'];      $targetsize['left'] = $action['relative']['leftdiff'];
194      $targetsize['top'] = $action['relative']['topdiff'];      $targetsize['top'] = $action['relative']['topdiff'];
195    }    }
196    
197      // convert from hex (as it is stored in the UI)
198      if ($action['RGB']['HEX'] && $deduced = hex_to_rgb($action['RGB']['HEX'])) {
199        $action['RGB'] = array_merge($action['RGB'], $deduced);
200      }
201    
202      // All the maths is done, now defer to the api toolkits;
203      $action['targetsize'] = $targetsize;
204    
205      $success = imageapi_toolkit_invoke('definecanvas', $image, array($action));
206      if ($success) {
207        $image->info['width'] = $targetsize['width'];
208        $image->info['height'] = $targetsize['height'];
209      }
210      return $success;
211    
212    /*
213    
214    $newcanvas = imagecreatetruecolor($targetsize['width'], $targetsize['height']);    $newcanvas = imagecreatetruecolor($targetsize['width'], $targetsize['height']);
   $RGB = $action['RGB'];  
215    
   // convert from hex (as it is stored in the UI)  
216    if ($RGB['HEX'] && $deduced = hex_to_rgb($RGB['HEX'])) {    if ($RGB['HEX'] && $deduced = hex_to_rgb($RGB['HEX'])) {
     $RGB = array_merge($RGB, $deduced);  
217      $background = imagecolorallocate($newcanvas, $RGB['red'], $RGB['green'], $RGB['blue']);      $background = imagecolorallocate($newcanvas, $RGB['red'], $RGB['green'], $RGB['blue']);
218    }    }
219    else {    else {
# Line 214  function canvasactions_definecanvas_imag Line 224  function canvasactions_definecanvas_imag
224      imagesavealpha($image->resource, TRUE);      imagesavealpha($image->resource, TRUE);
225    }    }
226    imagefilledrectangle($newcanvas, 0, 0, $targetsize['width'], $targetsize['height'], $background);    imagefilledrectangle($newcanvas, 0, 0, $targetsize['width'], $targetsize['height'], $background);
   
227  #  imagealphablending($newcanvas, TRUE);  #  imagealphablending($newcanvas, TRUE);
228    
229    if ($action['under']) {    if ($action['under']) {
# Line 228  function canvasactions_definecanvas_imag Line 237  function canvasactions_definecanvas_imag
237        'toolkit' => $image->toolkit,        'toolkit' => $image->toolkit,
238      );      );
239      imageapi_image_overlay($canvas_object, $image, $targetsize['left'], $targetsize['top'], 100, TRUE);      imageapi_image_overlay($canvas_object, $image, $targetsize['left'], $targetsize['top'], 100, TRUE);
   
 #    require_once('watermark.inc');  
 #    $watermark = new watermark();  
 #    $image->resource = $watermark->create_watermark($newcanvas, $image->resource, $targetsize['left'], $targetsize['top'], 100);  
 #    imagesavealpha($image->resource, TRUE);  
240    }    }
241    else {    else {
242      $image->resource = $newcanvas ;      $image->resource = $newcanvas ;
243    }    }
244    */
245    $image->info['width'] = $targetsize['width'];    $image->info['width'] = $targetsize['width'];
246    $image->info['height'] = $targetsize['height'];    $image->info['height'] = $targetsize['height'];
247    return TRUE;    return TRUE;
248  }  }
249    
250    /**
251     * Draw a color (or transparency) behind an image
252     *
253     * $targetsize is an array expected to contain a width,height and a left,top
254     * offset.
255     */
256    function imageapi_gd_image_definecanvas(&$image, $action = array()) {
257      $targetsize = $action['targetsize'];
258      $RGB = $action['RGB'];
259    
260      $newcanvas = imagecreatetruecolor($targetsize['width'], $targetsize['height']);
261    
262      if ($RGB['HEX']) {
263        $background = imagecolorallocate($newcanvas, $RGB['red'], $RGB['green'], $RGB['blue']);
264      }
265      else {
266        // No color, attempt transparency, assume white
267        $background = imagecolorallocatealpha($newcanvas, 255, 255, 255, 127);
268        imagesavealpha($newcanvas, TRUE);
269        imagealphablending($newcanvas, FALSE);
270        imagesavealpha($image->resource, TRUE);
271      }
272      imagefilledrectangle($newcanvas, 0, 0, $targetsize['width'], $targetsize['height'], $background);
273    #  imagealphablending($newcanvas, TRUE);
274    
275      if ($action['under']) {
276        $canvas_object = (object) array(
277          'resource' => $newcanvas,
278          'info' => array(
279            'width' => $targetsize['width'],
280            'height' => $targetsize['height'],
281            'mime_type' => 'image/png',
282          ),
283          'toolkit' => $image->toolkit,
284        );
285        imageapi_image_overlay($canvas_object, $image, $targetsize['left'], $targetsize['top'], 100, TRUE);
286      }
287      else {
288        $image->resource = $newcanvas ;
289      }
290      return TRUE;
291    }
292    
293    /**
294     * Draw a color (or transparency) behind an image
295     *
296     * $targetsize is an array expected to contain a width,height and a left,top
297     * offset.
298     */
299    function imageapi_imagemagick_image_definecanvas(&$image, $action = array()) {
300      $targetsize = $action['targetsize'];
301      $RGB = $action['RGB'];
302    
303      $crop = " -background '#{$RGB['HEX']}' -crop   '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!' ";
304      $draw = '';
305    
306      $draw = " -draw '";
307      $draw .= " fill '#{$RGB['HEX']}' polygon 0,0 {$targetsize['width']},0 {$targetsize['width']},{$targetsize['height']} 0,{$targetsize['height']} ";
308      $draw .= " ' ";
309    #  $draw .= " -composite ";
310      $draw .= " +repage ";
311    
312      $compose = " $crop $draw ";
313      $image->ops[] = $compose;
314      return TRUE;
315    
316    }
317    
318    
319  ////////////////////////////////////////////////  ////////////////////////////////////////////////
320    
321  /**  /**
# Line 297  function canvasactions_canvas2file_form( Line 370  function canvasactions_canvas2file_form(
370   */   */
371  function theme_canvasactions_canvas2file($element) {  function theme_canvasactions_canvas2file($element) {
372    $data = $element['#value'];    $data = $element['#value'];
373    return "xpos:{$data['xpos']} , ypos:{$data['ypos']} alpha:{$data['alpha']}%. file path:{$data['path']}, dimensions:{$data['dimensions']}" ;  
374      $filepath = $data['path'];
375      if (!file_exists($filepath) ) {
376        $filepath = file_create_path($data['path']);
377      }
378      $file_url = url($filepath);
379      return "xpos:{$data['xpos']} , ypos:{$data['ypos']} alpha:{$data['alpha']}%. file path:<a href='$file_url'>{$data['path']}</a>, dimensions:{$data['dimensions']}" ;
380  }  }
381    
382  /**  /**
# Line 353  function canvasactions_canvas2file_image Line 432  function canvasactions_canvas2file_image
432    
433    // crop both before processing to avoid unwanted processing.    // crop both before processing to avoid unwanted processing.
434    imagecache_crop_image($underlay, $crop_rules);    imagecache_crop_image($underlay, $crop_rules);
435    // Actually this failes because imagecache_crop fills it with solid color when 'cropping' to a larger size.    // Actually this fails because imagecache_crop fills it with solid color when 'cropping' to a larger size.
436    #imagecache_crop_image($image, $crop_rules);    #imagecache_crop_image($image, $crop_rules);
437    
438    // This func modifies the underlay image by ref, placing the current canvas on it    // This func modifies the underlay image by ref, placing the current canvas on it
# Line 603  function canvasactions_roundedcorners_fo Line 682  function canvasactions_roundedcorners_fo
682    return $form;    return $form;
683  }  }
684    
685    function canvasactions_roundedcorners_image(&$image, $action = array()) {
686      $independent_corners = !empty($action['independent_corners_set']['independent_corners']);
687      if (!$independent_corners) {
688        // set the independant corners to all be the same.
689        $corners = array('tl', 'tr', 'bl', 'br');
690        foreach ($corners as $key) {
691          // Use the all-the-same radius setting.
692          $action['independent_corners_set']['radii'][$key] = $action['radius'];
693        }
694      }
695    
696      return imageapi_toolkit_invoke('roundedcorners', $image, array($action));
697    }
698    
699  /**  /**
700   * Trim rounded corners off an image, using an anti-aliasing algorithm.   * Trim rounded corners off an image, using an anti-aliasing algorithm.
701   *   *
# Line 617  function canvasactions_roundedcorners_fo Line 710  function canvasactions_roundedcorners_fo
710   * @param $image   * @param $image
711   * @param $action   * @param $action
712   */   */
713  function canvasactions_roundedcorners_image(&$image, $action = array()) {  function imageapi_gd_image_roundedcorners(&$image, $action = array()) {
   if ($image->toolkit != 'imageapi_gd') {  
     drupal_set_message("Unable to create rounded corners with {$image->toolkit}");  
     // Pretend we did anyway, just return the untrimmed version.  
     return TRUE;  
   }  
   
714    // Read settings.    // Read settings.
715    $width = $image->info['width'];    $width = $image->info['width'];
716    $height = $image->info['height'];    $height = $image->info['height'];
# Line 750  function _canvasactions_roundedcorners_p Line 837  function _canvasactions_roundedcorners_p
837  }  }
838    
839    
840    
841    /**
842     * imageapi_roundedcorners
843     */
844    function imageapi_imagemagick_image_roundedcorners(&$image, $action = array()) {
845      // Based on the imagemagick documentation.
846      // http://www.imagemagick.org/Usage/thumbnails/#rounded
847      // Create arc cut-outs, then mask them.
848      // Draw black triangles and white circles.
849      // draw circle is center: x,y, and a point on the perimeter
850      $corners = array('tl', 'tr', 'bl', 'br');
851      $radii = $action['independent_corners_set']['radii'];
852      $width = $image->info['width'];
853      $height = $image->info['height'];
854      $tl = $radii['tl'];
855      $tr = $radii['tr'];
856      $bl = $radii['bl'];
857      $br = $radii['br'];
858    
859      $drawmask = " -draw '";
860      if ($tl) {
861        $drawmask .= " fill black polygon 0,0 0,{$tl} {$tl},0 ";
862        $drawmask .= " fill white circle {$tl},{$tl} {$tl},0 ";
863      }
864      if ($tr) {
865        $right = $width-$tr;
866        $drawmask .= " fill black polygon {$right},0 {$width},0 {$width},{$tr} ";
867        $drawmask .= " fill white circle {$right},{$tr} {$right},0 ";
868      }
869      if ($bl) {
870        $bottom = $height-$bl;
871        $drawmask .= " fill black polygon 0,{$bottom} 0,{$height} {$bl},{$height} ";
872        $drawmask .= " fill white circle {$bl},{$bottom} 0,{$bottom} ";
873      }
874      if ($br) {
875        $bottom = $height-$br;
876        $right = $width-$br;
877        $drawmask .= " fill black polygon {$right},{$height} {$width},{$bottom} {$width},{$height} ";
878        $drawmask .= " fill white circle {$right},{$bottom} {$width},{$bottom} ";
879      }
880      $drawmask .= " ' ";
881    
882      $compose = " \\( +clone  -threshold -1 {$drawmask} \\) +matte -compose CopyOpacity -composite ";
883      $image->ops[] = $compose;
884      return TRUE;
885    }
886    
887  /**  /**
888   * Implementation of theme_hook() for imagecache_ui.module   * Implementation of theme_hook() for imagecache_ui.module
889   */   */

Legend:
Removed from v.1.1.4.18  
changed lines
  Added in v.1.1.4.19

  ViewVC Help
Powered by ViewVC 1.1.2