5.13.2009

在 php 裡用 imagick 處理老舊文字

我依照通緝令教學,先用 gimp 把 background 弄出來,接著使用 php-imagick 去處裡老舊文字的特效,trample_text_layer() 大致上就是按照 tutorial 轉換成程式進行。效果還不錯。

我也試過 php-gd 來轉,但是我讀入透明的 background 後,輸出就變成不透明,設了一些參數,也是一樣,索性就試試看用 imagemagick 處理。

function trample_text_layer($draw, $text_width, $text_height)
{
  $im = new Imagick();
  $im->newImage($text_width, $text_height, 'none', 'png');
  $im->drawImage($draw);
 
  $noise_layer = $im->clone();
  $noise_layer->addNoiseImage(imagick::NOISE_POISSON);
  $im->compositeImage($noise_layer, imagick::COMPOSITE_DSTIN, 0, 0);
 
  $im->waveImage(0.3, 30);
  $im->chopImage(0, 2, 0, 0);
 
  $width = $im->getImageWidth();
  $height = $im->getImageHeight();
 
  for ($begin = 0; $begin < $width; $begin += $width / 3)
  {
    $begin = floor($begin);
    $portion = $im->getImageRegion(floor($width / 3) - 1, $height, $begin, 0);
    $portion->swirlImage(15);
    $im->compositeImage($portion, imagick::COMPOSITE_REPLACE, $begin, 0);
  }
 
  $im->blurImage(0.5, 0.1);
 
  return $im;
}
簡單三步驟是這樣:
  1. 加一層 noise layer。
  2. waveImage(波動)
  3. swirlImage(中心變形)
後兩步是為了要出現類似在 photoshop 中 liquify 的效果。

成果如下:


整個產生器展示在這裡。(如果他還活著。)
有興趣的可以下載原始碼