📄 phpthumb.filters.php
字号:
<?php
class phpthumb_filters {
function ApplyMask(&$gdimg_mask, &$gdimg_image) {
if (phpthumb_functions::gd_version() < 2) {
//$this->DebugMessage('Skipping ApplyMask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
return false;
}
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
//$this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__);
if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
ImageCopyResampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, ImageSX($gdimg_image), ImageSY($gdimg_image), ImageSX($gdimg_mask), ImageSY($gdimg_mask));
if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
$color_background = ImageColorAllocate($gdimg_mask_blendtemp, 0, 0, 0);
ImageFilledRectangle($gdimg_mask_blendtemp, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp), $color_background);
ImageAlphaBlending($gdimg_mask_blendtemp, false);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg_mask_blendtemp, true);
}
for ($x = 0; $x < ImageSX($gdimg_image); $x++) {
for ($y = 0; $y < ImageSY($gdimg_image); $y++) {
//$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y);
$RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y);
$MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y));
$MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127)));
$newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha);
ImageSetPixel($gdimg_mask_blendtemp, $x, $y, $newcolor);
}
}
ImageAlphaBlending($gdimg_image, false);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg_image, true);
}
ImageCopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp));
ImageDestroy($gdimg_mask_blendtemp);
} else {
//$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
}
ImageDestroy($gdimg_mask_resized);
} else {
//$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
}
} else {
// alpha merging requires PHP v4.3.2+
//$this->DebugMessage('Skipping ApplyMask() technique because PHP is v"'.phpversion().'"', __FILE__, __LINE__);
}
return true;
}
function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) {
$width = ($width ? $width : 5);
$hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
$hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
ImageAlphaBlending($gdimg, true);
for ($i = 0; $i < $width; $i++) {
$alpha = round(($i / $width) * 127);
$color1[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha);
$color2[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha);
ImageLine($gdimg, $i, $i, $i, ImageSY($gdimg) - $i, $color1[$i]); // left
ImageLine($gdimg, $i, $i, ImageSX($gdimg) - $i, $i, $color1[$i]); // top
ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i, $i, $color2[$i]); // right
ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, $i, ImageSY($gdimg) - $i, $color2[$i]); // bottom
}
return true;
}
function Blur(&$gdimg, $radius) {
// Taken from Torstein H鴑si's phrevosharpMask (see phpthumb.unsharp.php)
$radius = round(max(0, min($radius, 50)) * 2);
if (!$radius) {
return false;
}
$w = ImageSX($gdimg);
$h = ImageSY($gdimg);
if ($imgBlur = ImageCreateTrueColor($w, $h)) {
// Gaussian blur matrix:
// 1 2 1
// 2 4 2
// 1 2 1
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $radius; $i++) {
ImageCopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center
ImageCopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
}
return false;
}
function BlurGaussian(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
function BlurSelective(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
function Brightness(&$gdimg, $amount=0) {
if ($amount == 0) {
return true;
}
$amount = max(-255, min(255, $amount));
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, '.$amount.')', __FILE__, __LINE__);
// fall through and try it the hard way
}
$scaling = (255 - abs($amount)) / 255;
$baseamount = (($amount > 0) ? $amount : 0);
for ($x = 0; $x < ImageSX($gdimg); $x++) {
for ($y = 0; $y < ImageSY($gdimg); $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
foreach ($OriginalPixel as $key => $value) {
$NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling));
}
$newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
ImageSetPixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
function Contrast(&$gdimg, $amount=0) {
if ($amount == 0) {
return true;
}
$amount = max(-255, min(255, $amount));
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_CONTRAST, $amount)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_CONTRAST, '.$amount.')', __FILE__, __LINE__);
// fall through and try it the hard way
}
if ($amount > 0) {
$scaling = 1 + ($amount / 255);
} else {
$scaling = (255 - abs($amount)) / 255;
}
for ($x = 0; $x < ImageSX($gdimg); $x++) {
for ($y = 0; $y < ImageSY($gdimg); $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
foreach ($OriginalPixel as $key => $value) {
$NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling)));
}
$newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
ImageSetPixel($gdimg, $x, $y, $newColor);
}
}
}
function Colorize(&$gdimg, $amount, $targetColor) {
$amount = (is_numeric($amount) ? $amount : 25);
$targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
if ($amount == 0) {
return true;
}
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if ($targetColor == 'gray') {
$targetColor = '808080';
}
$r = substr($targetColor, 0, 2);
$g = substr($targetColor, 2, 2);
$b = substr($targetColor, 4, 2);
if (ImageFilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// overridden below for grayscale
if ($targetColor != 'gray') {
$TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
$TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
$TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
}
for ($x = 0; $x < ImageSX($gdimg); $x++) {
for ($y = 0; $y < ImageSY($gdimg); $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
if ($targetColor == 'gray') {
$TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
}
foreach ($TargetPixel as $key => $value) {
$NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * ($amount / 100)))));
}
//$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
$newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
ImageSetPixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
function Desaturate(&$gdimg, $amount, $color='') {
if ($amount == 0) {
return true;
}
return phpthumb_filters::Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray'));
}
function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $fade) {
if (phpthumb_functions::gd_version() < 2) {
return false;
}
$distance = ($distance ? $distance : 10);
$width = ($width ? $width : 10);
$hexcolor = ($hexcolor ? $hexcolor : '000000');
$angle = ($angle ? $angle : 225);
$fade = ($fade ? $fade : 1);
$width_shadow = cos(deg2rad($angle)) * ($distance + $width);
$height_shadow = sin(deg2rad($angle)) * ($distance + $width);
$scaling = min(ImageSX($gdimg) / (ImageSX($gdimg) + abs($width_shadow)), ImageSY($gdimg) / (ImageSY($gdimg) + abs($height_shadow)));
for ($i = 0; $i < $width; $i++) {
$WidthAlpha[$i] = (abs(($width / 2) - $i) / $width) * $fade;
$Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
$Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
}
$tempImageWidth = ImageSX($gdimg) + abs($Offset['x']);
$tempImageHeight = ImageSY($gdimg) + abs($Offset['y']);
if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
ImageAlphaBlending($gdimg_dropshadow_temp, false);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg_dropshadow_temp, true);
}
$transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
ImageFill($gdimg_dropshadow_temp, 0, 0, $transparent1);
for ($x = 0; $x < ImageSX($gdimg); $x++) {
for ($y = 0; $y < ImageSY($gdimg); $y++) {
$PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
}
}
for ($x = 0; $x < $tempImageWidth; $x++) {
for ($y = 0; $y < $tempImageHeight; $y++) {
//for ($i = 0; $i < $width; $i++) {
for ($i = 0; $i < 1; $i++) {
if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -