【PHP】負片效果


【目的】

  1. 呈現出類似底片的效果。

【程式】(negative.php)

  1. 法一
    <?php
    $img = imagecreatefrompng('lena_std.png');
    imageTrueColorToPalette($img,true,256);
    $numColors = imageColorsTotal($img);
    for ($x = 0; $x < $numColors; $x++)
      {
        list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));
        $r = 255 - $r;
        $g = 255 - $g;
        $b = 255 - $b;
        imageColorSet($img,$x,$r,$g,$b);
      }
    imagepng($img,'lena_negative.png');
    imagedestroy($img);
    ?>
  2. 法二(還需驗證)
    <?php
    $im = imagecreatefrompng('lena_std.png');
    if($im && imagefilter($im, IMG_FILTER_NEGATE))
    {
      echo 'Reverses all colors of the imagee.';
      imagepng($im, 'lena_negative.png');
    }
     else
    {
      echo 'Reverses all colors failed.';
    }
    imagedestroy($im);
    ?>

【結果】(lena_negative.png)

image
 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com