📄 phpthumb.filters.php
字号:
}
}
}
ImageAlphaBlending($gdimg_dropshadow_temp, true);
for ($x = 0; $x < ImageSX($gdimg); $x++) {
for ($y = 0; $y < ImageSY($gdimg); $y++) {
if ($PixelMap[$x][$y]['alpha'] < 127) {
$thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
}
}
}
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg, true);
}
ImageAlphaBlending($gdimg, false);
//$this->is_alpha = true;
$transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
ImageFilledRectangle($gdimg, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $transparent2);
ImageCopyResampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg_dropshadow_temp), ImageSY($gdimg_dropshadow_temp));
ImageDestroy($gdimg_dropshadow_temp);
}
return true;
}
function EdgeDetect(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
function Elipse($gdimg) {
if (phpthumb_functions::gd_version() < 2) {
return false;
}
// generate mask at twice desired resolution and downsample afterwards for easy antialiasing
if ($gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg) * 2, ImageSY($gdimg) * 2)) {
if ($gdimg_elipsemask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
$color_transparent = ImageColorAllocate($gdimg_elipsemask_double, 255, 255, 255);
ImageFilledEllipse($gdimg_elipsemask_double, ImageSX($gdimg), ImageSY($gdimg), (ImageSX($gdimg) - 1) * 2, (ImageSY($gdimg) - 1) * 2, $color_transparent);
ImageCopyResampled($gdimg_elipsemask, $gdimg_elipsemask_double, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg) * 2, ImageSY($gdimg) * 2);
phpthumb_filters::ApplyMask($gdimg_elipsemask, $gdimg);
ImageDestroy($gdimg_elipsemask);
return true;
} else {
//$this->DebugMessage('$gdimg_elipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
}
ImageDestroy($gdimg_elipsemask_double);
} else {
//$this->DebugMessage('$gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
}
return false;
}
function Emboss(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_EMBOSS)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
function Flip(&$gdimg, $x=false, $y=false) {
if (!$x && !$y) {
return false;
}
if ($tempImage = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
if ($x) {
ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
for ($x = 0; $x < ImageSX($gdimg); $x++) {
ImageCopy($gdimg, $tempImage, ImageSX($gdimg) - 1 - $x, 0, $x, 0, 1, ImageSY($gdimg));
}
}
if ($y) {
ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
for ($y = 0; $y < ImageSY($gdimg); $y++) {
ImageCopy($gdimg, $tempImage, 0, ImageSY($gdimg) - 1 - $y, 0, $y, ImageSX($gdimg), 1);
}
}
ImageDestroy($tempImage);
}
return true;
}
function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
$frame_width = ($frame_width ? $frame_width : 5);
$edge_width = ($edge_width ? $edge_width : 1);
$hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
$hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
$hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
$color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
$color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
$color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
for ($i = 0; $i < $edge_width; $i++) {
// outer bevel
ImageLine($gdimg, $i, $i, $i, ImageSY($gdimg) - $i, $color1); // left
ImageLine($gdimg, $i, $i, ImageSX($gdimg) - $i, $i, $color1); // top
ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i, $i, $color2); // right
ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, $i, ImageSY($gdimg) - $i, $color2); // bottom
}
for ($i = 0; $i < $frame_width; $i++) {
// actual frame
ImageRectangle($gdimg, $edge_width + $i, $edge_width + $i, ImageSX($gdimg) - $edge_width - $i, ImageSY($gdimg) - $edge_width - $i, $color_frame);
}
for ($i = 0; $i < $edge_width; $i++) {
// inner bevel
ImageLine($gdimg, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, $color2); // left
ImageLine($gdimg, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, ImageSX($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, $color2); // top
ImageLine($gdimg, ImageSX($gdimg) - $frame_width - $edge_width - $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, ImageSX($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, $color1); // right
ImageLine($gdimg, ImageSX($gdimg) - $frame_width - $edge_width - $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, $color1); // bottom
}
return true;
}
function Gamma(&$gdimg, $amount) {
if (number_format($amount, 4) == '1.0000') {
return true;
}
return ImageGammaCorrect($gdimg, 1.0, $amount);
}
function Grayscale(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)) {
return true;
}
$this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
return phpthumb_filters::Colorize($gdimg, 100, 'gray');
}
function HistogramAnalysis(&$gdimg, $calculateGray=false) {
$ImageSX = ImageSX($gdimg);
$ImageSY = ImageSY($gdimg);
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
@$Analysis['red'][$OriginalPixel['red']]++;
@$Analysis['green'][$OriginalPixel['green']]++;
@$Analysis['blue'][$OriginalPixel['blue']]++;
@$Analysis['alpha'][$OriginalPixel['alpha']]++;
if ($calculateGray) {
$GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
@$Analysis['gray'][$GrayPixel['red']]++;
}
}
}
$keys = array('red', 'green', 'blue', 'alpha');
if ($calculateGray) {
$keys[] = 'gray';
}
foreach ($keys as $key) {
ksort($Analysis[$key]);
}
return $Analysis;
}
function HistogramStretch(&$gdimg, $band='*', $min=-1, $max=-1) {
// equivalent of "Auto Contrast" in Adobe Photoshop
$Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
$keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
if (!isset($keys[$band])) {
return false;
}
$key = $keys[$band];
// If the absolute brightest and darkest pixels are used then one random
// pixel in the image could throw off the whole system. Instead, count up/down
// from the limit and allow 0.1% of brightest/darkest pixels to be clipped to min/max
$clip_threshold = ImageSX($gdimg) * ImageSX($gdimg) * 0.001;
if ($min >= 0) {
$range_min = min($min, 255);
} else {
$countsum = 0;
for ($i = 0; $i <= 255; $i++) {
$countsum += @$Analysis[$key][$i];
if ($countsum >= $clip_threshold) {
$range_min = $i - 1;
break;
}
}
$range_min = max($range_min, 0);
}
if ($max >= 0) {
$range_max = max($max, 255);
} else {
$countsum = 0;
$threshold = ImageSX($gdimg) * ImageSX($gdimg) * 0.001; // 0.1% of brightest and darkest pixels can be clipped
for ($i = 255; $i >= 0; $i--) {
$countsum += @$Analysis[$key][$i];
if ($countsum >= $clip_threshold) {
$range_max = $i + 1;
break;
}
}
$range_max = min($range_max, 255);
}
$range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
if (($range_min == 0) && ($range_max == 255)) {
// no adjustment neccesary - don't waste CPU time!
return true;
}
$ImageSX = ImageSX($gdimg);
$ImageSY = ImageSY($gdimg);
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
if ($band == '*') {
$new['red'] = min(255, max(0, ($OriginalPixel['red'] - $range_min) * $range_scale));
$new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
$new['blue'] = min(255, max(0, ($OriginalPixel['blue'] - $range_min) * $range_scale));
$new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
} else {
$new = $OriginalPixel;
$new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
}
$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
ImageSetPixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin=5) {
$Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
$histW = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
$histH = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
if ($gdHist = ImageCreateTrueColor($histW, $histH)) {
$color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
ImageFilledRectangle($gdHist, 0, 0, $histW, $histH, $color_back);
ImageAlphaBlending($gdHist, false);
ImageSaveAlpha($gdHist, true);
if ($gdHistTemp = ImageCreateTrueColor(256, 100)) {
$color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
ImageAlphaBlending($gdHistTemp, false);
ImageSaveAlpha($gdHistTemp, true);
ImageFilledRectangle($gdHistTemp, 0, 0, ImageSX($gdHistTemp), ImageSY($gdHistTemp), $color_back_temp);
$DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
$Colors = explode(';', $colors);
$BandsToGraph = array_unique(preg_split('//', $bands));
$keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
foreach ($BandsToGraph as $key => $band) {
if (!isset($keys[$band])) {
continue;
}
$PeakValue = max($Analysis[$keys[$band]]);
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
$tempHeight = ImageSY($gdHistTemp);
for ($x = 0; $x <= 255; $x++) {
ImageLine($gdHistTemp, $x, $tempHeight - 1, $x, $tempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $tempHeight), $thisColor);
}
ImageLine($gdHistTemp, 0, $tempHeight - 1, 255, $tempHeight - 1, $thisColor);
ImageLine($gdHistTemp, 0, $tempHeight - 2, 255, $tempHeight - 2, $thisColor);
}
ImageCopyResampled($gdHist, $gdHistTemp, 0, 0, 0, 0, ImageSX($gdHist), ImageSY($gdHist), ImageSX($gdHistTemp), ImageSY($gdHistTemp));
ImageDestroy($gdHistTemp);
} else {
return false;
}
phpthumb_filters::WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin);
ImageDestroy($gdHist);
return true;
}
return false;
}
function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -