我稍微修改了 switch251 的代码,这里我们生成了复古效果
<?php
$originalFileName = $filename;
$destinationFileName = "2".$filename;
$fullPath = explode(".",$originalFileName);
$lastIndex = sizeof($fullPath) - 1;
$extension = $fullPath[$lastIndex];
if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension))
{
$sourceImage = imagecreatefromjpeg($originalFileName);
}
$img_width = imageSX($sourceImage);
$img_height = imageSY($sourceImage);
for ($y = 0; $y <$img_height; $y++)
{
for ($x = 0; $x <$img_width; $x++)
{
$rgb = imagecolorat($sourceImage, $x, $y);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
$red2 = min($red*.393 + $green*.769 + $blue*.189,255);
$green2 = min($red*.349 + $green*.686 + $blue*.168,255);
$blue2 = min($red*.272 + $green*.534 + $blue*.131,255);
$grayR = $red2 << 16; $grayG = $green2 << 8 ; $grayB = $blue2; $grayColor = $grayR | $grayG | $grayB;
imagesetpixel ($sourceImage, $x, $y, $grayColor);
imagecolorallocate ($sourceImage, $gray, $gray, $gray);
}
}
$destinationImage = ImageCreateTrueColor($img_width, $img_height);
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);
imagejpeg($destinationImage, $destinationFileName);
imagedestroy($destinationImage);
imagedestroy($sourceImage);
?>