[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/include/ -> functions_images_crop.inc.php (source)

   1  <?php
   2  /**************************************************

   3   * imgedit.inc.php

   4   * 2003-10-17

   5   * www.sonnd.com / www.supergarv.de

   6   *

   7   * COPYRIGHT (C) BY sonnd / Garvin Hicking

   8   * Published as LGPL. Copyright notice has to stay in effect.

   9   **************************************************/
  10  
  11  class imgedit {
  12      // Needed variables.

  13      var $orientation_available;
  14      var $real_img_name, $real_img_info, $real_img_width, $real_img_height, $http_img_name;
  15      var $img_width, $img_height, $img_name;
  16      var $zoombox_width;
  17      var $area_border, $area_width, $area_height, $inner_area_x, $inner_area_y;
  18      var $harea_width, $harea_height, $harea_img_name, $harea_visibility;
  19      var $varea_width, $varea_height, $varea_img_name, $varea_visibility;
  20      var $ratio;
  21      var $overlay_clip_top, $overlay_clip_bottom, $overlay_clip_left, $overlay_clip_right;
  22      var $slice_from_x, $slice_from_y, $slice_to_x, $slice_to_y;
  23      var $output_template = '';
  24      var $imgedit_smarty = array();
  25  
  26      // Helper function to cycle through an array. Strips of "_x" and "_y" coordinates if an 'input type="image"'

  27      // variable has been submitted.

  28      function makeImgComp(&$array) {
  29          foreach($array AS $key => $val) {
  30              if (is_array($val)) {
  31                  $this->makeImgComp($array[$key]);
  32              } elseif (preg_match('@^.*_(x|y)$@', $key)) {
  33                  $array[preg_replace('@^(.*)_(x|y)$@', '\1', $key)] = $val;
  34              }
  35          }
  36  
  37          return true;
  38      }
  39  
  40      // Constructor.

  41      function imgedit($img_name, $http_img_name) {
  42          global $serendipity;
  43          $this->makeImgComp($_REQUEST);
  44  
  45          // Store the given variables

  46          $this->real_img_name      = $this->img_name = $img_name;
  47          $this->real_img_info      = getimagesize($this->real_img_name);
  48          $this->real_img_width     = $this->real_img_info[0];
  49          $this->real_img_height    = $this->real_img_info[1];
  50  
  51          $this->http_img_name      = $http_img_name;
  52  
  53          // Set even more variables

  54          $this->setDefaults();
  55  
  56          return true;
  57      }
  58  
  59      // The main logic

  60      function main() {
  61  
  62          // 1. Parse the $_REQUEST vars.

  63          $this->parseRequest();
  64  
  65          // 2. Get the current size of the cropping area

  66          $this->getCurrentArea();
  67  
  68          // 3. Parse any actions

  69          $this->parseRequestActions();
  70  
  71          // 4. Valide the results of any actions

  72          $this->validateCoordinates();
  73  
  74          // 5. Populate the template

  75          $this->setVars();
  76  
  77          return true;
  78      }
  79  
  80      // Sets default vars.

  81      function setDefaults() {
  82          $this->orientation_available = true;
  83  
  84          $this->img_width             = $this->real_img_width;
  85          $this->img_height            = $this->real_img_height;
  86          $this->img_name              = $this->real_img_name;
  87  
  88          $this->zoombox_width         = 100;
  89          $this->area_border           = 6;
  90  
  91          // Set values if they haven't been submitted yet.

  92          if (!isset($_REQUEST['area_orientation']) || ($_REQUEST['area_orientation'] != 'h' && $_REQUEST['area_orientation'] != 'v')) {
  93              $_REQUEST['area_orientation'] = 'h';
  94          }
  95  
  96          if (!isset($_REQUEST['autoguess_clicked'])) {
  97              $_REQUEST['autoguess_clicked'] = 'false';
  98          }
  99  
 100          if (!isset($_REQUEST['zoombox_x'])) {
 101              $_REQUEST['zoombox_x'] = -$this->area_border;
 102          }
 103  
 104          if (!isset($_REQUEST['zoombox_y'])) {
 105              $_REQUEST['zoombox_y'] = -$this->area_border;
 106          }
 107  
 108          if (!isset($_REQUEST['move_increase'])) {
 109              $_REQUEST['move_increase'] = 8;
 110          } else {
 111              $_REQUEST['move_increase'] = intval($_REQUEST['move_increase']);
 112          }
 113  
 114          return true;
 115      }
 116  
 117      // Parse the $_REQUEST vars

 118      function parseRequest() {
 119          if (isset($_REQUEST['toggle_area_orientation'])) {
 120              if ($_REQUEST['area_orientation'] == 'h') {
 121                  $_REQUEST['area_orientation'] = 'v';
 122              } else {
 123                  $_REQUEST['area_orientation'] = 'h';
 124              }
 125          }
 126  
 127          if (isset($_REQUEST['zoombox_factor'])) {
 128              $_REQUEST['zoombox_factor'] = floatval($_REQUEST['zoombox_factor']);
 129          } else {
 130              $_REQUEST['zoombox_factor'] = 1;
 131          }
 132  
 133          if (isset($_REQUEST['action']['enlarge'])) {
 134              $_REQUEST['zoombox_factor'] += 0.1;
 135          } elseif (isset($_REQUEST['action']['reduce'])) {
 136              $_REQUEST['zoombox_factor'] -= 0.1;
 137          }
 138  
 139          if ($_REQUEST['zoombox_factor'] > 3) {
 140              $_REQUEST['zoombox_factor'] = 3;
 141          } elseif ($_REQUEST['zoombox_factor'] < 0.1) {
 142              $_REQUEST['zoombox_factor'] = 0.1;
 143          }
 144  
 145          if (isset($_REQUEST['action']['moveup'])) {
 146              $_REQUEST['zoombox_y'] -= $_REQUEST['move_increase'];
 147          } elseif (isset($_REQUEST['action']['movedown'])) {
 148              $_REQUEST['zoombox_y'] += $_REQUEST['move_increase'];
 149          } elseif (isset($_REQUEST['action']['moveleft'])) {
 150              $_REQUEST['zoombox_x'] -= $_REQUEST['move_increase'];
 151          } elseif (isset($_REQUEST['action']['moveright'])) {
 152              $_REQUEST['zoombox_x'] += $_REQUEST['move_increase'];
 153          }
 154  
 155          return true;
 156      }
 157  
 158      // Fit an image inside the cropping area.

 159      function imgFit($orientation) {
 160          if ($orientation == 'height') {
 161              $this->imgFitHeight();
 162          } else {
 163              $this->imgFitWidth();
 164          }
 165  
 166          $_REQUEST['zoombox_factor']    = $this->ratio;
 167          $_REQUEST['autoguess_clicked'] = 'true';
 168          $_REQUEST['zoombox_x']         = -$this->area_border;
 169          $_REQUEST['zoombox_y']         = -$this->area_border;
 170  
 171          return true;
 172      }
 173  
 174      // Fit an image, using height as fixed value

 175      function imgFitHeight() {
 176          $this->img_height = $this->inner_area_y - $this->area_border;
 177          $this->ratio      = round($this->img_height / $this->real_img_height, 3);
 178          $this->img_width  = intval(round($this->real_img_width * $this->ratio));
 179  
 180          return true;
 181      }
 182  
 183      // Fit an image, using width as fixed value

 184      function imgFitWidth() {
 185          $this->img_width  = $this->inner_area_x - $this->area_border;
 186          $this->ratio      = round($this->img_width / $this->real_img_width, 3);
 187          $this->img_height = intval(round($this->real_img_height * $this->ratio));
 188  
 189          return true;
 190      }
 191  
 192      // Parse actions that were submitted

 193      function parseRequestActions() {
 194          global $serendipity;
 195  
 196          if (isset($_REQUEST['autoscale'])) {
 197  
 198              if ($this->real_img_width > $this->real_img_height) {
 199                  // The image is a horizontal one. Resize height to fit.

 200                  $this->imgFit('height');
 201              } else {
 202                  // The image is a vertical one. Resize width to fit.

 203                  $this->imgFit('width');
 204              }
 205  
 206  
 207          } elseif (isset($_REQUEST['scale'])) {
 208  
 209  
 210              if ($this->real_img_width > $this->real_img_height) {
 211                  // The image is a horizontal one. Resize width to fit.

 212                  $this->imgFit('width');
 213              } else {
 214                  // The image is a vertical one. Resize height to fit.

 215                  $this->imgFit('height');
 216              }
 217  
 218          } else {
 219  
 220              $this->img_width  = intval(round($this->real_img_width  * $_REQUEST['zoombox_factor']));
 221              $this->img_height = intval(round($this->real_img_height * $_REQUEST['zoombox_factor']));
 222  
 223          }
 224  
 225  
 226          // Check which template to use

 227          if (isset($_REQUEST['crop'])) {
 228              $this->output_template = 'admin/media_imgedit_done.tpl';
 229          } else {
 230              $this->output_template = 'admin/media_imgedit.tpl';
 231              if ($this->orientation_available) {
 232                  $this->imgedit_smarty['orientation_available'] = true;
 233              }
 234          }
 235  
 236          // The final action. Cropping will take place.

 237          if (isset($_REQUEST['crop'])) {
 238              $new_img_name = $this->img_name . '.tmp';
 239  
 240              if ($this->img_width <= $this->area_width && $this->img_height <= $this->area_height) {
 241                  $this->imgedit_smarty['image_no_cut'] = true;
 242  
 243                  $new_img_width  = $this->img_width;
 244                  $new_img_height = $this->img_height;
 245              } else {
 246                  $this->imgedit_smarty['image_cut'] = true;
 247  
 248                  $new_img_width  = $this->area_width  - $this->area_border - $this->area_border;
 249                  $new_img_height = $this->area_height - $this->area_border - $this->area_border;
 250              }
 251  
 252              $this->slice_from_x = ($this->area_border + $_REQUEST['zoombox_x']) / $_REQUEST['zoombox_factor'];
 253              $this->slice_from_y = ($this->area_border + $_REQUEST['zoombox_y']) / $_REQUEST['zoombox_factor'];
 254  
 255              $slice_width  = $new_img_width  / $_REQUEST['zoombox_factor'];
 256              $slice_height = $new_img_height / $_REQUEST['zoombox_factor'];
 257  
 258              $this->slice_to_x = $this->slice_from_x + $this->slice_width;
 259              $this->slice_to_y = $this->slice_from_y + $this->slice_height;
 260  
 261              // TODO:

 262              // - Operate also on PNG, TIFF etc.

 263              // - Support image magick

 264              // - Save file as new image!

 265              // - Optionally ask whether to only make a thumbnail from the extracted portion

 266  
 267              // PHP: int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)

 268              // Open the user's image.

 269              $img_res      = imagecreatefromjpeg($this->img_name);
 270  
 271              // Init a blank picture to save the cropped image in.

 272              $new_img_res  = imagecreatetruecolor($new_img_width, $new_img_height);
 273  
 274              // Crop the image based on user input, save the resulting image. If either action fails, report an error.

 275              if (!imagecopyresampled($new_img_res, $img_res, 0, 0, $this->slice_from_x, $this->slice_from_y, $new_img_width, $new_img_height, $slice_width, $slice_height) ||
 276                  !imageJpeg($new_img_res, $new_img_name, 90)) {
 277  
 278                  $this->imgedit_smarty['image_error'] = true;
 279              }
 280  
 281              $backup = $this->increment($this->img_name);
 282              rename($this->img_name, $backup . '.backup');
 283              rename($new_img_name, $this->img_name);
 284              $http_new_file = preg_replace('@^' . preg_quote($serendipity['serendipityPath'] . $serendipity['uploadPath']) . '@', '', $this->img_name);
 285              serendipity_makeThumbnail(basename($http_new_file), dirname($http_new_file) . '/');
 286  
 287              $this->img_name   = $new_img_name;
 288              $this->img_width  = $new_img_width;
 289              $this->img_height = $new_img_height;
 290          }
 291  
 292          return true;
 293      }
 294  
 295      function increment($fullfile) {
 296          $d = dirname($fullfile) . '/';
 297          $f = basename($fullfile);
 298  
 299          $f = time() . '.' . $f;
 300  
 301          return $d . $f;
 302      }
 303  
 304      // Checks if any coordinates are out of allowed range

 305      function validateCoordinates() {
 306          if ($this->img_width > $this->area_width) {
 307              $c_img_width = $this->img_width;
 308              $ca_img_width = $this->area_width;
 309              $x_area_border = $this->area_border;
 310          } else {
 311              $c_img_width = $this->area_width;
 312              $ca_img_width = $this->img_width;
 313              $x_area_border = -$this->area_border;
 314          }
 315  
 316          if ($this->img_height > $this->area_height) {
 317              $c_img_height = $this->img_height;
 318              $ca_img_height = $this->area_height;
 319              $y_area_border = $this->area_border;
 320          } else {
 321              $c_img_height = $this->area_height;
 322              $ca_img_height = $this->img_height;
 323              $y_area_border = -$this->area_border;
 324          }
 325  
 326          if ($_REQUEST['zoombox_x'] < (0 - $this->area_border)) {
 327              $_REQUEST['zoombox_x'] = 0 - $this->area_border;
 328          } elseif ($_REQUEST['zoombox_x'] > ($this->img_width - $ca_img_width + $x_area_border)) {
 329              $_REQUEST['zoombox_x'] = $this->img_width - $ca_img_width + $x_area_border;
 330          }
 331  
 332          if ($_REQUEST['zoombox_y'] < (0 - $this->area_border)) {
 333              $_REQUEST['zoombox_y'] = 0 - $this->area_border;
 334          } elseif ($_REQUEST['zoombox_y'] > ($this->img_height - $ca_img_height + $y_area_border)) {
 335              $_REQUEST['zoombox_y'] = $this->img_height - $ca_img_height + $y_area_border;
 336          }
 337  
 338          $this->overlay_clip_top    += $_REQUEST['zoombox_y'];
 339          $this->overlay_clip_bottom += $_REQUEST['zoombox_y'];
 340          $this->overlay_clip_left   += $_REQUEST['zoombox_x'];
 341          $this->overlay_clip_right  += $_REQUEST['zoombox_x'];
 342  
 343          return true;
 344      }
 345  
 346      // Sets the sizes of the cropping area

 347      function setArea($img_name, $orientation = 'h') {
 348          global $serendipity;
 349  
 350          $fimg         = serendipity_getTemplateFile('admin/img/' . $img_name, 'serendipityPath');
 351          $hfimg        = serendipity_getTemplateFile('admin/img/' . $img_name, 'serendipityHTTPPath');
 352          $img_info     = getImageSize($fimg);
 353  
 354          if ($orientation == 'h') {
 355              $this->harea_width    = $img_info[0];
 356              $this->harea_height   = $img_info[1];
 357              $this->harea_img_name = $hfimg;
 358          } else {
 359              $this->varea_width    = $img_info[0];
 360              $this->varea_height   = $img_info[1];
 361              $this->varea_img_name = $hfimg;
 362          }
 363  
 364          return true;
 365      }
 366  
 367      // Get the size of the selected cropping area

 368      function getCurrentArea() {
 369          if ($_REQUEST['area_orientation'] == 'h') {
 370              $this->area_width       = $this->harea_width;
 371              $this->area_height      = $this->harea_height;
 372              $this->varea_visibility = 'hidden';
 373              $this->harea_visibility = 'visible';
 374          } else {
 375              $this->area_width       = $this->varea_width;
 376              $this->area_height      = $this->varea_height;
 377              $this->varea_visibility = 'visible';
 378              $this->harea_visibility = 'hidden';
 379          }
 380  
 381          $this->inner_area_x         = $this->area_width  - $this->area_border;
 382          $this->inner_area_y         = $this->area_height - $this->area_border;
 383  
 384          $this->overlay_clip_top     = $this->area_border;
 385          $this->overlay_clip_right   = $this->area_width  - $this->area_border;
 386          $this->overlay_clip_bottom  = $this->area_height - $this->area_border;
 387          $this->overlay_clip_left    = $this->area_border;
 388  
 389          return true;
 390      }
 391  
 392      // Set the template variables

 393      function setVars() {
 394  
 395          $this->imgedit_smarty['zoombox_width'] =       $this->zoombox_width;
 396          $this->imgedit_smarty['zoombox_padding'] =     $this->zoombox_width + 20;
 397          $this->imgedit_smarty['area_width'] =          $this->area_width;
 398          $this->imgedit_smarty['area_height'] =         $this->area_height;
 399          $this->imgedit_smarty['varea_width'] =         $this->varea_width;
 400          $this->imgedit_smarty['varea_height'] =        $this->varea_height;
 401          $this->imgedit_smarty['varea_img_name'] =      $this->varea_img_name;
 402          $this->imgedit_smarty['varea_visibility'] =    $this->varea_visibility;
 403          $this->imgedit_smarty['harea_width'] =         $this->harea_width;
 404          $this->imgedit_smarty['harea_height'] =        $this->harea_height;
 405          $this->imgedit_smarty['harea_img_name'] =      $this->harea_img_name;
 406          $this->imgedit_smarty['harea_visibility'] =    $this->harea_visibility;
 407          $this->imgedit_smarty['area_border'] =         $this->area_border;
 408          $this->imgedit_smarty['refresh_line'] =        time();
 409  
 410          $this->imgedit_smarty['real_img_width'] =      $this->real_img_width;
 411          $this->imgedit_smarty['real_img_height'] =     $this->real_img_height;
 412          $this->imgedit_smarty['real_img_name'] =       $this->real_img_name;
 413          $this->imgedit_smarty['http_img_name'] =       $this->http_img_name;
 414  
 415          $this->imgedit_smarty['img_width'] =           $this->img_width;
 416          $this->imgedit_smarty['img_height'] =          $this->img_height;
 417          $this->imgedit_smarty['zoom_img_width'] =      $this->img_width;
 418          $this->imgedit_smarty['zoom_img_height'] =     $this->img_height;
 419          $this->imgedit_smarty['img_name'] =            $this->img_name;
 420  
 421          $this->imgedit_smarty['overlay_clip_top'] =    $this->overlay_clip_top;
 422          $this->imgedit_smarty['overlay_clip_bottom'] = $this->overlay_clip_bottom;
 423          $this->imgedit_smarty['overlay_clip_left'] =   $this->overlay_clip_left;
 424          $this->imgedit_smarty['overlay_clip_right'] =  $this->overlay_clip_right;
 425  
 426          $this->imgedit_smarty['slice_from_x'] =        $this->slice_from_x;
 427          $this->imgedit_smarty['slice_from_y'] =        $this->slice_from_y;
 428          $this->imgedit_smarty['slice_to_x'] =          $this->slice_to_x;
 429          $this->imgedit_smarty['slice_to_y'] =          $this->slice_to_y;
 430  
 431          $this->imgedit_smarty['zoombox_x'] =           $_REQUEST['zoombox_x'];
 432          $this->imgedit_smarty['zoombox_y'] =           $_REQUEST['zoombox_y'];
 433          $this->imgedit_smarty['zoombox_factor'] =      $_REQUEST['zoombox_factor'];
 434  
 435          $this->imgedit_smarty['php_self'] =            $_SERVER['PHP_SELF'];
 436          $this->imgedit_smarty['area_orientation'] =    $_REQUEST['area_orientation'];
 437          $this->imgedit_smarty['move_increase'] =       $_REQUEST['move_increase'];
 438          $this->imgedit_smarty['autoguess_clicked'] =   $_REQUEST['autoguess_clicked'];
 439  
 440          $this->imgedit_smarty['my_url']            =   htmlspecialchars($_SERVER['REQUEST_URI']);
 441  
 442          if ($_REQUEST['autoguess_clicked'] == 'true') {
 443              $this->imgedit_smarty['scale_visibility'] = 'visible';
 444          } else {
 445              $this->imgedit_smarty['scale_visibility'] = 'hidden';
 446          }
 447  
 448          return true;
 449      }
 450  }


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics