[ Index ]

PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09)

title

Body

[close]

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

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2009 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  // ========================================================================================================================
  25  
  26  // load external GD image handling class
  27  if(!IMAGICK_ON) {
  28      /*
  29      $cfg = array(    'JPEG_QUALITY'    => $phpwcms["jpg_quality"],
  30                      'USE_GD2'         => GD2_ON,
  31                      'USE_COLOR_NAMES' => false
  32                  );
  33      */
  34      include_once (PHPWCMS_ROOT."/include/inc_ext/ss_image/ss_image.class.php");
  35  }
  36  
  37  // ========================================================================================================================
  38  
  39  function imagick_converting ($imagick) {
  40  
  41      $default = array(    "max_width"        =>    150,
  42                          "max_height"    =>    150,
  43                          "error"            =>    0,
  44                          "image_name"    =>    '',
  45                          "thumb_name"    =>    '',
  46                          "target_ext"    =>    'jpg',
  47                          "image_dir"        =>    PHPWCMS_ROOT.PHPWCMS_FILES,
  48                          "thumb_dir"        =>    PHPWCMS_THUMB,
  49                          'jpg_quality'    =>    85,
  50                          'sharpen_level'    =>    0,
  51                          'density'        =>    72,
  52                          'add_command'    =>    '',
  53                          'crop_image'    =>    false
  54                      );
  55      
  56      $imagick = array_merge($default, $imagick);
  57      unset($default);
  58  
  59      if(!intval($imagick["max_width"])) {
  60          $imagick["max_width"]    = '';
  61          $imagick['crop_image']    = false;
  62      }
  63      if(!intval($imagick["max_height"])) {
  64          $imagick["max_height"]    = '';
  65          $imagick['crop_image']    = false;
  66      }
  67  
  68      //say that it should be converted
  69      $resize = true;
  70      
  71      // now it is good to check if image still available
  72      // otherwise use placeholder image "filestorage/image_placeholder.png"
  73      if(!is_file($imagick["image_dir"].$imagick["image_name"])) {
  74          $imagick["image_name"] = 'image_placeholder.png';
  75          $imagick["thumb_name"] = 'temp_'.$imagick["thumb_name"];
  76      }
  77      
  78      // check if it is useful only to copy the image
  79      $check_image = @getimagesize($imagick["image_dir"].$imagick["image_name"]);
  80  
  81      $channel_check = (isset($check_image['channels']) && $check_image['channels'] == 3) ? true : false;
  82  
  83      if($channel_check && $check_image && ($check_image[2] == 1 || $check_image[2] == 2 || $check_image[2] == 3)) {
  84  
  85          $copy_w = 0;
  86          $copy_h = 0;
  87          if($imagick["max_width"]    && $check_image[0]    <= $imagick["max_width"])    $copy_w = 1;
  88          if($imagick["max_height"]    && $check_image[1]    <= $imagick["max_height"])    $copy_h = 1;        
  89          if(!$imagick["max_width"]    && $check_image[0])                             $copy_w = 1;
  90          if(!$imagick["max_height"]    && $check_image[1])                                $copy_h = 1;
  91  
  92          if(!$imagick['crop_image'] && $copy_w && $copy_h) { // do not copy in case crop is enabled
  93  
  94              $imagick["thumb_name"] .= '.' . which_ext($imagick["image_name"]);
  95              if( @copy( $imagick["image_dir"].$imagick["image_name"], $imagick["thumb_dir"].$imagick["thumb_name"] ) ) {
  96                  $resize = false;
  97              }
  98          }
  99      }
 100      
 101      if($resize) {
 102      
 103          if(!ini_get('safe_mode') && function_exists('set_time_limit')) set_time_limit(90);
 104  
 105          if(!$imagick['jpg_quality']) {
 106              $imagick['jpg_quality'] = 80;
 107          } elseif ($imagick['jpg_quality'] < 25) {
 108              $imagick['jpg_quality'] = 25;
 109          } elseif ($imagick['jpg_quality'] > 100) {
 110              $imagick['jpg_quality'] = 100;
 111          }
 112          
 113          //Sharpen Level - only ImageMagick: 0, 1, 2, 3, 4, 5 -- 0 = no, 5 = extra sharp
 114          switch($imagick['sharpen_level']) {
 115              
 116              case 1:        $sharp4 = '-sharpen 10 ';
 117                          $sharp5 = '-sharpen 1x10 ';
 118                          break;
 119              case 2:        $sharp4 = '-sharpen 25 ';
 120                          $sharp5 = '-sharpen 3x10 ';
 121                          break;
 122              case 3:        $sharp4 = '-sharpen 50 ';
 123                          $sharp5 = '-sharpen 5x10 ';
 124                          break;
 125              case 4:        $sharp4 = '-sharpen 70 ';
 126                          $sharp5 = '-sharpen 7x10 ';
 127                          break;
 128              case 5:        $sharp4 = '-sharpen 90 ';
 129                          $sharp5 = '-sharpen 9x10 ';
 130                          break;
 131              default:    $sharp4 = '';
 132                          $sharp5 = '';
 133              
 134          }
 135          
 136          $sharpen = '';
 137  
 138          $imagick["thumb_name"] .= '.' . $imagick["target_ext"];
 139      
 140          if(IMAGICK_ON) {
 141              // If ImageMagick should be used
 142              
 143              $imagick["command"]  = IMAGICK_PATH."convert ";
 144              switch($imagick["target_ext"]) {
 145                  
 146                  case "jpg":    if(IMAGICK_ON == 1) {
 147                                  //ImageMagick >= 5
 148                                  $imagick["command"] .= "-colorspace RGB -type TrueColor ";
 149                                  
 150                                  $sharpen = $sharp5;
 151                                  
 152                              } else {
 153                                  //ImageMagick 4.2.9
 154                                  $imagick["command"] .= "-colorspace RGB -colors 16777216 ";
 155                                  
 156                                  $sharpen = $sharp4;
 157                                  
 158                              }
 159                              $imagick["source_image_name"] = $imagick["image_dir"].$imagick["image_name"].'[0]';
 160                              break;
 161                              
 162                  case "gif":    if(IMAGICK_ON == 1) {
 163                                  //ImageMagick >= 5
 164                                  $imagick["command"] .= "-colors 256 ";
 165                              } else {
 166                                  //ImageMagick 4.2.9
 167                                  $imagick["command"] .= "-colorspace Transparent -colors 128 ";
 168                              }
 169                              $imagick["source_image_name"] = $imagick["image_dir"].$imagick["image_name"];
 170                              break;
 171                              
 172                  case "png":    $imagick["command"] .= "-colorspace RGB ";
 173                              $imagick["source_image_name"] = $imagick["image_dir"].$imagick["image_name"];
 174                              break;
 175  
 176              }
 177              
 178              /**
 179               * to keep 4.2.9 compatibility there is more that has to be done
 180               * it might fail for PNG and for cropping
 181               */
 182              
 183              if($imagick['crop_image'] && $imagick["max_width"] && $imagick["max_height"]) {
 184  
 185                  if(IMAGICK_ON == 1) {
 186                      //ImageMagick >= 5
 187                      $resize_factor = 2 * ( $imagick["max_width"] > $imagick["max_height"] ? $imagick["max_width"] : $imagick["max_height"] );
 188                      
 189                      $imagick["command"] .= '-resize "x'.$resize_factor.'" -resize "'.$resize_factor.'x<" -resize 50% ';
 190                      $imagick["command"] .= '-gravity center -crop '.$imagick["max_width"].'x'.$imagick["max_height"].'+0+0 ';
 191                      $imagick["command"] .= '+repage ';
 192              
 193                  } else {
 194                      //ImageMagick 4.2.9
 195                      $imagick["command"] .= '-geometry "'.$imagick["max_width"].'x'.$imagick["max_height"].'>" ';
 196                      $imagick["command"] .= '-gravity center -crop '.$imagick["max_width"].'x'.$imagick["max_height"].'+0+0 ';
 197                      $imagick["command"] .= '+repage ';
 198                      
 199                  }
 200              
 201              } elseif( $imagick["max_width"] || $imagick["max_height"] ) {
 202  
 203                  // resize
 204                  if(IMAGICK_ON == 1) {
 205                      //ImageMagick >= 5
 206                      $imagick["command"] .= '-resize "'.$imagick["max_width"].'x'.$imagick["max_height"].'>" ';
 207                  
 208                  } else {
 209                      //ImageMagick 4.2.9
 210                      $imagick["command"] .= '-geometry "'.$imagick["max_width"].'x'.$imagick["max_height"].'>" ';
 211                      
 212                  }
 213              
 214              }
 215              
 216              // quality level
 217              $imagick["command"] .= "-quality ".$imagick['jpg_quality']." ";
 218              
 219              // density
 220              $imagick["command"] .= "-density ".$imagick['density']."x".$imagick['density']." ";
 221              
 222              // additional command
 223              if($imagick['add_command']) {
 224                  $imagick["command"] .= $imagick['add_command'] . " ";
 225              }
 226          
 227              $imagick["command"] .= '-antialias ';
 228              $imagick["command"] .= $sharpen;
 229              $imagick['command'] .= '"'.$imagick["source_image_name"].'" ';
 230              /*
 231              if(IMAGICK_ON == 2) {
 232                  $imagick["command"] .= '+profile "*" ';
 233              }
 234              */
 235              $imagick["command"] .= '"'.$imagick["thumb_dir"].$imagick["thumb_name"].'" ';
 236              
 237              // debug commands
 238              //write_textfile(PHPWCMS_TEMP.'imagemagick.log', date('Y-m-d H:i:s').' - '.$imagick["command"].LF, 'a');
 239              
 240              @exec($imagick["command"], $imagick_return);
 241              
 242              if (isset($imagick_return[0])) {
 243                  $imagick["error"] = $imagick_return[0];
 244              }
 245          
 246          } else {
 247              // use GD function
 248              if($check_image) {
 249              
 250                  // try to handle limited PHP memory
 251                  $php_memory = getBytes( @ini_get('memory_limit') );
 252                  $img_memory = getRealImageSize( $check_image );
 253                  
 254                  // do memory checks only when PHP's memory limit 
 255                  // and "real" image size is known
 256                  if(empty($GLOBALS['phpwcms']['gd_memcheck_off']) && $php_memory && $img_memory) {
 257                      
 258                      // in general we need around twice the memory for 
 259                      // successful GD resizing - so lets halve it
 260                      // and compare against the RAM the image will need
 261                      if($php_memory / 2 < $img_memory) {
 262                      
 263                          $imagick["image_name"] = 'image_memoryinfo.png';
 264                          $imagick["thumb_name"] = 'mem_'.$imagick["thumb_name"];
 265                      
 266                      }
 267                  
 268                  }
 269  
 270                  $image = new ss_image($imagick["image_dir"].$imagick["image_name"], 'f'); //given original image
 271                  $image->set_parameter($imagick["jpg_quality"], GD2_ON, false);
 272          
 273                  if($imagick['crop_image']) {
 274                  
 275                      $image->set_size($imagick["max_width"], $imagick["max_height"], '+');
 276                      $image->commit();
 277                      
 278                      $crop_x = abs( $image->get_w() - $imagick["max_width"] );
 279                      $crop_x = ceil( $crop_x / 2 );
 280                      $crop_y = abs( $image->get_h() - $imagick["max_height"] );
 281                      $crop_y = ceil( $crop_y / 2 );
 282                      
 283                      $image->crop($crop_x, $crop_y, $imagick["max_width"], $imagick["max_height"]);                    
 284                      
 285                  } else {
 286  
 287                      if($imagick["max_width"] && $check_image[0] < $imagick["max_width"]) $imagick["max_width"] = $check_image[0];
 288                      if($imagick["max_height"] && $check_image[1] < $imagick["max_height"]) $imagick["max_height"] = $check_image[1];        
 289                      if(!$imagick["max_width"]) $imagick["max_width"] = "*";
 290                      if(!$imagick["max_height"]) $imagick["max_height"] = "*";
 291                      
 292                      $image->set_size($imagick["max_width"], $imagick["max_height"], '--');
 293  
 294                  }
 295                      
 296                  /*
 297                  if($imagick['sharpen_level']) {
 298                      $image->commit();
 299                      $image->unsharp(80, 0.5, 3);
 300                  }
 301                  */
 302                  $image->output($imagick["thumb_dir"].$imagick["thumb_name"], 'c', strtoupper($imagick["target_ext"]));
 303                  if(!is_file($imagick["thumb_dir"].$imagick["thumb_name"])) {
 304                      $imagick["error"] = "GD image creation failed.";
 305                  }
 306              } else {
 307                   $imagick["error"] = "GD source image error. Maybe not exists.";
 308              }
 309          }
 310          
 311      }
 312      return $imagick;
 313  }
 314  
 315  // ========================================================================================================================
 316  
 317  
 318  // build thumbnail image name
 319  function get_cached_image($val, $db_track=true, $return_all_imageinfo=true) {
 320      
 321      // predefine values
 322      $default = array(
 323          "max_width"        =>    $GLOBALS['phpwcms']["img_list_width"],
 324          "max_height"    =>    $GLOBALS['phpwcms']["img_list_height"],
 325          "image_dir"        =>    PHPWCMS_ROOT . '/' . PHPWCMS_FILES,
 326          "thumb_dir"        =>    PHPWCMS_ROOT . '/' . PHPWCMS_IMAGES,
 327          'jpg_quality'    =>    $GLOBALS['phpwcms']['jpg_quality'],
 328          'sharpen_level'    =>    $GLOBALS['phpwcms']['sharpen_level'],
 329          'crop_image'    =>    false
 330          );
 331      
 332      $val = array_merge($default, $val);
 333      
 334      $imgCache = false; //do not insert file information in db image cache
 335      $thumb_image_info = array();
 336      $thumb_image_info[0] = false; // Thumb Image
 337      $image_hash = substr($val['image_name'], 0, (strlen($val['target_ext']) * -1) - 1);
 338  
 339      // now check if thumbnail was created - proof for GIF, PNG, JPG
 340      
 341      $thumb_check = $val['thumb_dir'] . $val['thumb_name'];
 342      
 343      if(is_file($thumb_check .'.jpg')) {
 344          
 345          $thumb_image_info[0] = $val['thumb_name'] .'.jpg';
 346          
 347      } elseif(is_file($thumb_check .'.png')) {
 348          
 349          $thumb_image_info[0] = $val['thumb_name'] .'.png';
 350      
 351      } elseif(is_file($thumb_check .'.gif')) {
 352          
 353          $thumb_image_info[0] = $val['thumb_name'] .'.gif';
 354      
 355      } else {
 356  
 357          // check if current file's extension is handable by ImageMagick or GD
 358          if( $val["target_ext"] = is_ext_true($val["target_ext"]) ) {    
 359  
 360              $create_preview    = imagick_converting( $val );
 361  
 362              if( is_file( $val['thumb_dir'] . $create_preview["thumb_name"] ) ) {
 363                  $thumb_image_info[0] = $create_preview["thumb_name"];
 364                  $imgCache = true; // insert/update information in db image cache
 365              };
 366  
 367          }
 368      }
 369      
 370      if($thumb_image_info[0] != false) {
 371      
 372          if($return_all_imageinfo === false) {
 373              return $thumb_image_info;
 374          }
 375          
 376          $thumb_info = @getimagesize($val['thumb_dir'].$thumb_image_info[0]);
 377          if(is_array($thumb_info)) {
 378                  
 379              $thumb_image_info[1] = $thumb_info[0]; // width
 380              $thumb_image_info[2] = $thumb_info[1]; // height
 381              $thumb_image_info[3] = $thumb_info[3]; // HTML width & height attribute
 382  
 383              // now update image caching information in db
 384              if($imgCache && $db_track) {
 385              
 386                  if(!function_exists('_dbQuery')) {
 387                      require_once (PHPWCMS_ROOT.'/include/inc_lib/dbcon.inc.php');
 388                  }
 389              
 390                  $sql  = "INSERT INTO ".DB_PREPEND."phpwcms_imgcache SET ";
 391                  $sql .= "imgcache_hash = '" .         aporeplace($image_hash)             . "', ";
 392                  $sql .= "imgcache_imgname = '" .     aporeplace($thumb_image_info[0])     . "', ";
 393                  $sql .= "imgcache_width = " .         intval($thumb_image_info[1])         . " , ";
 394                  $sql .= "imgcache_height = " .         intval($thumb_image_info[2])         . " , ";
 395                  $sql .= "imgcache_wh = '" .         aporeplace($thumb_image_info[3])     . "'";
 396                  @_dbQuery($sql, 'INSERT');
 397              }
 398          
 399          } else {
 400          
 401              // if wrong - no result, return false
 402              return false;
 403          
 404          }
 405          
 406      } else {
 407          // if wrong - no result, return false
 408          return false;
 409      }
 410  
 411      // Return cached thumbnail image info
 412      // $thumb_image_info[0] = Name, 
 413      // $thumb_image_info[1] = width, 
 414      // $thumb_image_info[2] = height, 
 415      // $thumb_image_info[3] = HTML width & height attribute
 416      return $thumb_image_info;
 417  }
 418  
 419  ?>


Generated: Wed Dec 30 05:55:15 2009 Cross-referenced by PHPXref 0.7