Ignore:
Timestamp:
Jan 5, 2012, 10:35:25 PM (12 years ago)
Author:
rvelices
Message:

feature 2548 multisize - sharpen + watermarks (partially implemented / no test with imagick extension)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/image.class.php

    r12756 r12851  
    4242
    4343  function resize($width, $height);
     44 
     45  function sharpen($amount);
     46 
     47  function compose($overlay, $x, $y, $opacity);
    4448
    4549  function write($destination_filepath);
     
    257261
    258262    return $rotation;
     263  }
     264
     265  /** Returns a normalized convolution kernel for sharpening*/
     266  static function get_sharpen_matrix($amount)
     267  {
     268                // Amount should be in the range of 18-10
     269                $amount = round(abs(-18 + ($amount * 0.08)), 2);
     270
     271                $matrix = array
     272                (
     273                        array(-1,   -1,    -1),
     274                        array(-1, $amount, -1),
     275                        array(-1,   -1,    -1),
     276                );
     277   
     278    $norm = array_sum(array_map('array_sum', $matrix));
     279
     280    for ($i=0; $i<3; $i++)
     281    {
     282      $line = & $matrix[$i];
     283      for ($j=0; $j<3; $j++)
     284        $line[$j] /= $norm;
     285    }
     286
     287                return $matrix;
    259288  }
    260289
     
    398427  }
    399428
     429  function sharpen($amount)
     430  {
     431    $m = pwg_image::get_sharpen_matrix($amount);
     432                return  $this->image->convolveImage($m);
     433  }
     434 
     435  function compose($overlay, $x, $y, $opacity)
     436  {
     437    // todo
     438    return false;
     439  }
     440
    400441  function write($destination_filepath)
    401442  {
     
    416457  var $commands = array();
    417458
    418   function __construct($source_filepath, $imagickdir='')
    419   {
     459  function __construct($source_filepath)
     460  {
     461    global $conf;
    420462    $this->source_filepath = $source_filepath;
    421     $this->imagickdir = $imagickdir;
    422 
    423     $command = $imagickdir.'identify -format "%wx%h" "'.realpath($source_filepath).'"';
     463    $this->imagickdir = $conf['ext_imagick_dir'];
     464
     465    $command = $this->imagickdir.'identify -format "%wx%h" "'.realpath($source_filepath).'"';
    424466    @exec($command, $returnarray);
    425467    if(!is_array($returnarray) or empty($returnarray[0]) or !preg_match('/^(\d+)x(\d+)$/', $returnarray[0], $match))
    426468    {
    427       die("[External ImageMagick] Corrupt image");
     469      die("[External ImageMagick] Corrupt image\n" . var_export($returnarray, true));
    428470    }
    429471
     
    477519    $this->add_command('filter', 'Lanczos');
    478520    $this->add_command('resize', $width.'x'.$height.'!');
     521    return true;
     522  }
     523
     524  function sharpen($amount)
     525  {
     526    $m = pwg_image::get_sharpen_matrix($amount);
     527   
     528    $param ='convolve "'.count($m).':';
     529    foreach ($m as $line)
     530    {
     531      $param .= ' ';
     532      $param .= implode(',', $line);
     533    }
     534    $param .= '"';
     535    $this->add_command('morphology', $param);
     536    return true;
     537  }
     538 
     539  function compose($overlay, $x, $y, $opacity)
     540  {
     541    $param = 'compose dissolve -define compose:args='.$opacity;
     542    $param .= ' '.escapeshellarg(realpath($overlay->image->source_filepath));
     543    $param .= ' -gravity NorthWest -geometry +'.$x.'+'.$y;
     544    $param .= ' -composite';
     545    $this->add_command($param);
    479546    return true;
    480547  }
     
    497564    $exec .= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'"';
    498565    @exec($exec, $returnarray);
     566   
     567    //echo($exec);
    499568    return is_array($returnarray);
    500569  }
     
    612681  }
    613682
     683  function sharpen($amount)
     684  {
     685    $m = pwg_image::get_sharpen_matrix($amount);
     686                return imageconvolution($this->image, $m, 1, 0);
     687  }
     688 
     689  function compose($overlay, $x, $y, $opacity)
     690  {
     691    $ioverlay = $overlay->image->image;
     692    /* A replacement for php's imagecopymerge() function that supports the alpha channel
     693    See php bug #23815:  http://bugs.php.net/bug.php?id=23815 */
     694
     695    $ow = imagesx($ioverlay);
     696    $oh = imagesy($ioverlay);
     697     
     698                // Create a new blank image the site of our source image
     699                $cut = imagecreatetruecolor($ow, $oh);
     700
     701                // Copy the blank image into the destination image where the source goes
     702                imagecopy($cut, $this->image, 0, 0, $x, $y, $ow, $oh);
     703
     704                // Place the source image in the destination image
     705                imagecopy($cut, $ioverlay, 0, 0, 0, 0, $ow, $oh);
     706                imagecopymerge($this->image, $cut, $x, $y, 0, 0, $ow, $oh, $opacity);
     707    return true;
     708  }
     709
    614710  function write($destination_filepath)
    615711  {
Note: See TracChangeset for help on using the changeset viewer.