[ Index ]

PHP Cross Reference of phpwcms V1.5.0 _r431 (28.01.12)

title

Body

[close]

/include/inc_lib/ -> imagick.convert.inc.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2012 Oliver Georgi <oliver@phpwcms.de> // All rights reserved.
   6   
   7     This script is part of PHPWCMS. The PHPWCMS web content management system is
   8     free software; you can redistribute it and/or modify it under the terms of
   9     the GNU General Public License as published by the Free Software Foundation;
  10     either version 2 of the License, or (at your option) any later version.
  11    
  12     The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
  13     A copy is found in the textfile GPL.txt and important notices to the license 
  14     from the author is found in LICENSE.txt distributed with these scripts.
  15    
  16     This script is distributed in the hope that it will be useful, but WITHOUT ANY 
  17     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  18     PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  19   
  20     This copyright notice MUST APPEAR in all copies of the script!
  21  *************************************************************************************/
  22  
  23  
  24  // load external GD image handling class
  25  include_once (PHPWCMS_ROOT."/include/inc_lib/helper.image.php");
  26  
  27  
  28  // Deprecated function, use for 3rd party fallback usage
  29  function imagick_converting(array $config) {
  30      return image_manipulate($config);
  31  }
  32  
  33  // Resize, Crop and other image manipulation
  34  function image_manipulate(array $config) {
  35      
  36      global $phpwcms;
  37      
  38      // Merge config values with default
  39      $config = array_merge(array(
  40              "max_width"        => $phpwcms["img_list_width"],
  41              "max_height"    => $phpwcms["img_list_height"],
  42              "error"            => '',
  43              "image_name"    => '',
  44              "thumb_name"    => '',
  45              "target_ext"    => 'jpg',
  46              "image_dir"        => PHPWCMS_ROOT.'/'.PHPWCMS_FILES,
  47              "thumb_dir"        => PHPWCMS_THUMB,
  48              'jpg_quality'    => 85,
  49              'sharpen_level'    => 0,
  50              'density'        => 72,
  51              'add_command'    => '',
  52              'crop_image'    => false,
  53              'master_dim'    => 'auto'
  54          ), $config);
  55      
  56      // Test width and height and set correct dimensions
  57      if(!intval($config["max_width"]) && !intval($config["max_height"])) {
  58          // Should not happen, but better have a fallback
  59          $config["max_width"]    = $phpwcms["img_list_width"];
  60          $config["max_height"]    = $phpwcms["img_list_height"];
  61          $config['master_dim']    = 'auto';
  62      } elseif(!intval($config["max_width"])) {
  63          // No width given, recalculate final image size based on height
  64          $config["max_width"]    = $phpwcms["img_prev_width"];
  65          $config['crop_image']    = false;
  66          $config['master_dim']    = 'height';
  67      } elseif(!intval($config["max_height"])) {
  68          // No height given, recalculate final image size based on width
  69          $config["max_height"]    = $phpwcms["img_prev_height"];
  70          $config['crop_image']    = false;
  71          $config['master_dim']    = 'width';
  72      }
  73      
  74      // Check if source image is accessible
  75      // otherwise use placeholder image "filestorage/image_placeholder.png"
  76      if(!is_file($config["image_dir"].$config["image_name"])) {
  77          $config["image_name"]  = 'image_placeholder.png';
  78          $config["thumb_name"]  = 'temp_'.$config["thumb_name"];
  79      }
  80      
  81      // Doubled config setting but especially for Image manipulation class
  82      $image_config = array(
  83          
  84          'image_library'        => $phpwcms['image_library'],
  85          'library_path'        => $phpwcms['library_path'],
  86          'source_image'        => $config["image_dir"].$config["image_name"],
  87          'new_image'            => $config["thumb_dir"].$config["thumb_name"].'.'.$config["target_ext"],
  88          'maintain_ratio'    => true,
  89          'width'                => $config['max_width'],
  90          'height'            => $config['max_height'],
  91          'master_dim'        => $config['master_dim'],
  92          'sharpen'            => $config['sharpen_level'],
  93          'quality'            => $config['jpg_quality'],
  94          'create_thumb'        => false,
  95          'target_ext'        => $config["target_ext"]
  96  
  97      );
  98      
  99      $IMG = new Phpwcms_Image_lib($image_config);
 100      
 101      // try to handle limited PHP memory
 102      if(empty($GLOBALS['phpwcms']['gd_memcheck_off']) && ($phpwcms['image_library'] == 'gd2' || $phpwcms['image_library'] == 'gd')) {
 103  
 104          $php_memory = getBytes( @ini_get('memory_limit') );
 105          $img_memory = getRealImageSize( $IMG->image_current_vals );
 106          
 107          // do memory checks only when PHP's memory limit 
 108          // and "real" image size is known
 109          if($php_memory && $img_memory) {
 110              
 111              // test if we have enough PHP memory for this image and test to set it up
 112              if($php_memory / 3 < $img_memory) {
 113                  @ini_set('memory_limit', $img_memory * 3);
 114              }
 115              
 116              $php_memory = getBytes( @ini_get('memory_limit') );
 117              
 118              // still not enough, use fallback memory warning image
 119              if($php_memory / 3 < $img_memory) {
 120                  $config["image_name"]            = 'image_memoryinfo.png';
 121                  $config["thumb_name"]            = 'mem_'.$config["thumb_name"];
 122                  
 123                  $image_config['source_image']    = $config["image_dir"].$config["image_name"];
 124                  $image_config['new_image']        = $config["thumb_dir"].$config["thumb_name"].'.'.$config["target_ext"];
 125                  
 126                  $IMG->initialize($image_config);
 127              }
 128          
 129          }
 130      }
 131      
 132          
 133      if($phpwcms['image_library'] == 'imagemagick' && $config['crop_image']) {
 134          
 135          $IMG->crop_centered_resize();
 136      
 137      } elseif($config['crop_image']) {
 138          
 139          $image_config = set_cropped_imagesize($image_config, $IMG->orig_width, $IMG->orig_height);
 140          
 141          if( $image_config['do_cropping'] ) {
 142              
 143              // first resize width recalculated height/width
 144              $IMG->width        = $image_config['resize_width'];
 145              $IMG->height    = $image_config['resize_height'];
 146              $IMG->quality    = 100;
 147              $IMG->resize();
 148              
 149              $image_config['sharpen']        = 0;
 150              $image_config['maintain_ratio']    = FALSE;
 151              $image_config['create_thumb']    = FALSE;
 152              $image_config['source_image']    = $image_config['new_image'];
 153  
 154              $IMG->initialize( $image_config );
 155              $IMG->crop();
 156          
 157          } else {
 158              
 159              $IMG->resize();
 160              
 161          }
 162          
 163          
 164      } else {
 165      
 166          $IMG->resize();
 167      
 168      }
 169      
 170      $config["thumb_name"]    = $IMG->dest_image;
 171      $config['error']        = $IMG->display_errors('<li>', '</li>', '<ul class="error">', '</ul>');
 172  
 173      return $config;
 174  }
 175  
 176  // ========================================================================================================================
 177  
 178  
 179  // build thumbnail image name
 180  function get_cached_image(array $val, $db_track=true, $return_all_imageinfo=true) {
 181      
 182      $val = array_merge(array(
 183          "max_width"        =>    $GLOBALS['phpwcms']["img_list_width"],
 184          "max_height"    =>    $GLOBALS['phpwcms']["img_list_height"],
 185          "image_dir"        =>    PHPWCMS_ROOT . '/' . PHPWCMS_FILES,
 186          "thumb_dir"        =>    PHPWCMS_ROOT . '/' . PHPWCMS_IMAGES,
 187          'jpg_quality'    =>    $GLOBALS['phpwcms']['jpg_quality'],
 188          'sharpen_level'    =>    $GLOBALS['phpwcms']['sharpen_level'],
 189          'crop_image'    =>    false
 190      ), $val);
 191      
 192      $imgCache = false; //do not insert file information in db image cache
 193      $thumb_image_info = array();
 194      $thumb_image_info[0] = false; // Thumb Image
 195      $image_hash = substr($val['image_name'], 0, (strlen($val['target_ext']) * -1) - 1);
 196  
 197      // now check if thumbnail was created - proof for GIF, PNG, JPG
 198      
 199      $thumb_check = $val['thumb_dir'] . $val['thumb_name'];
 200      
 201      if(is_file($thumb_check .'.jpg')) {
 202          
 203          $thumb_image_info[0] = $val['thumb_name'] .'.jpg';
 204          
 205      } elseif(is_file($thumb_check .'.png')) {
 206          
 207          $thumb_image_info[0] = $val['thumb_name'] .'.png';
 208      
 209      } elseif(is_file($thumb_check .'.gif')) {
 210          
 211          $thumb_image_info[0] = $val['thumb_name'] .'.gif';
 212      
 213      } else {
 214  
 215          // check if current file's extension is handable by ImageMagick or GD
 216          if( $val["target_ext"] = is_ext_true($val["target_ext"]) ) {    
 217  
 218              $create_preview    = image_manipulate( $val );
 219              
 220              if( is_file( $val['thumb_dir'] . $create_preview["thumb_name"] ) ) {
 221                  $thumb_image_info[0] = $create_preview["thumb_name"];
 222                  $imgCache = true; // insert/update information in db image cache
 223              };
 224  
 225          }
 226      }
 227  
 228      
 229      if($thumb_image_info[0] != false) {
 230      
 231          if($return_all_imageinfo === false) {
 232              return $thumb_image_info;
 233          }
 234          
 235          $thumb_info = @getimagesize($val['thumb_dir'].$thumb_image_info[0]);
 236          if(is_array($thumb_info)) {
 237                  
 238              $thumb_image_info[1] = $thumb_info[0]; // width
 239              $thumb_image_info[2] = $thumb_info[1]; // height
 240              $thumb_image_info[3] = $thumb_info[3]; // HTML width & height attribute
 241  
 242              // now update image caching information in db
 243              if($imgCache && $db_track) {
 244              
 245                  if(!function_exists('_dbQuery')) {
 246                      require_once (PHPWCMS_ROOT.'/include/inc_lib/dbcon.inc.php');
 247                  }
 248              
 249                  $sql  = "INSERT INTO ".DB_PREPEND."phpwcms_imgcache SET ";
 250                  $sql .= "imgcache_hash = '" .         aporeplace($image_hash)             . "', ";
 251                  $sql .= "imgcache_imgname = '" .     aporeplace($thumb_image_info[0])     . "', ";
 252                  $sql .= "imgcache_width = " .         intval($thumb_image_info[1])         . " , ";
 253                  $sql .= "imgcache_height = " .         intval($thumb_image_info[2])         . " , ";
 254                  $sql .= "imgcache_wh = '" .         aporeplace($thumb_image_info[3])     . "'";
 255                  @_dbQuery($sql, 'INSERT');
 256                  
 257                  
 258              }
 259          
 260          } else {
 261          
 262              // if wrong - no result, return false
 263              return false;
 264          
 265          }
 266          
 267      } else {
 268          // if wrong - no result, return false
 269          return false;
 270      }
 271  
 272      // Return cached thumbnail image info
 273      // $thumb_image_info[0] = Name, 
 274      // $thumb_image_info[1] = width, 
 275      // $thumb_image_info[2] = height, 
 276      // $thumb_image_info[3] = HTML width & height attribute
 277      return $thumb_image_info;
 278  }
 279  
 280  function set_cropped_imagesize($config, $orig_width=0, $orig_height=0) {
 281      $config['resize_width']        = $config['width'];
 282      $config['resize_height']    = $config['height'];
 283      $config['x_axis']            = 0;
 284      $config['y_axis']            = 0;
 285      
 286      if($orig_width && $orig_height) {
 287          
 288          // compare original image sizes against cropped image size
 289          $ratio_width    = $orig_width / $config['width'];
 290          $ratio_height    = $orig_height / $config['height'];
 291          
 292          // check if cropping is necessary
 293          if( $ratio_width == $ratio_height ) {
 294  
 295              $config['do_cropping'] = FALSE;
 296  
 297          } else {
 298              
 299              $config['do_cropping'] = TRUE;
 300          
 301              // source image dimensions are both larger than target
 302              if( $ratio_width >= 1 && $ratio_height >= 1 ) {
 303                  
 304                  if( $ratio_width <= $ratio_height ) {
 305                      $config['resize_height']    = ceil( $orig_height / $ratio_width );
 306                      $config['y_axis']            = round( ( $config['resize_height'] - $config['height'] ) / 2 );
 307                  } else {
 308                      $config['resize_width']        = ceil( $orig_width / $ratio_height );
 309                      $config['x_axis']            = round( ( $config['resize_width'] - $config['width'] ) / 2 );
 310                  }
 311              
 312              // source image dimensions width and/or height is smaller than target
 313              } else {
 314                  
 315                  if( $ratio_width <= $ratio_height ) {
 316                      $config['resize_width']        = ceil( $orig_width + ( $orig_width * ( 1 - $ratio_height ) ) );
 317                      $config['x_axis']            = round( ( $config['resize_width'] - $config['width'] ) / 2 );
 318                  } else {
 319                      $config['resize_height']    = ceil( $orig_height + ( $orig_height * ( 1 - $ratio_width ) ) );
 320                      $config['y_axis']            = round( ( $config['resize_height'] - $config['height'] ) / 2 );
 321                  }
 322              }
 323          
 324          }
 325      }
 326      
 327      return $config;
 328  }
 329  
 330  
 331  ?>


Generated: Sun Jan 29 16:31:14 2012 Cross-referenced by PHPXref 0.7.1