type = "jpg"; $this->srcImage = @imagecreatefromjpeg($srcImage); break; case "gif": $this->type = "gif"; $this->srcImage = @imagecreatefromgif($srcImage); break; case "png": $this->type = "png"; $this->srcImage = @imagecreatefrompng($srcImage); break; } } $this->setWidth($this->srcImage); $this->setHeight($this->srcImage); } public function getWidth() { return $this->intWidth; } public function getHeight() { return $this->intHeight; } public function setWidth($srcImage) { $newWidth = imagesx($srcImage); $this->intWidth = $newWidth; } public function setHeight($srcImage) { $newHeight = imagesy($srcImage); $this->intHeight = $newHeight; } public function setType($type) { $this->type = $type; } public function outputImage() { switch ($this->type) { case "jpeg": case "jpg": header("Content-type: image/jpeg"); imagejpeg($this->img); break; case "png": header("Content-type: image/png"); imagepng($this->img); break; case "gif": header("Content-type: image/png"); imagegif($this->img); break; } } public function outputToFile($filename, $quality = 75) { switch ($this->type) { case "jpeg": case "jpg": imagejpeg($this->srcImage, $filename, $quality); break; case "png": imagepng($this->srcImage, $filename, $quality); break; case "gif": imagegif($this->srcImage, $filename, $quality); break; } } public function getSupportedImageTypes() { $arrTypes = array(); $arrTypeBits = array( IMG_GIF => "GIF", IMG_JPG => "JPG", IMG_PNG => "PNG", IMG_WBMP => "WBMP" ); foreach ( $arrTypeBits as $intTypeBits => $strType ) { if ( imageTypes() & $intTypeBits ) { $arrTypes[] = $strType; } } return $arrTypes; } public function gdVersion() { if ( ! extension_loaded( "gd" ) ) { return false; } ob_start(); phpinfo( 8 ); $info = ob_get_contents(); ob_end_clean(); $info = stristr( $info, "gd version" ); $gd = array(); preg_match( "/\d/", $info, $gd ); return $gd[ 0 ]; } public function hsv2rgb( $h, $s, $v ) { if ( $s == 0 ) { $r = $g = $b = $v; return array( $r * 255, $g * 255, $b * 255 ); } else { $h %= 360; $h /= 60; $i = floor( $h ); $f = $h - $i; $p = $v * ( 1 - $s ); $q = $v * ( 1 - $s * $f ); $t = $v * ( 1 - $s * ( 1 - $f ) ); switch( $i ) { case 0: $r = $v; $g = $t; $b = $p; break; case 1: $r = $q; $g = $v; $b = $p; break; case 2: $r = $p; $g = $v; $b = $t; break; case 3: $r = $p; $g = $q; $b = $v; break; case 4: $r = $t; $g = $p; $b = $v; break; default: $r = $v; $g = $p; $b = $q; break; } return array( $r * 255, $g * 255, $b * 255 ); } } public function rgb2hsv( $red, $green, $blue ) { $r = $red / 255; $g = $green / 255; $b = $blue / 255; $min = min( $r, $g, $b ); $max = max( $r, $g, $b ); $v = $max; $delta = $max - $min; if ( $max != 0 ) { $s = $delta / $max; } else { $s = 0; $h = -1; return array( $h, $s, $v ); } if ( $delta ) { if ( $r == $max ) { $h = ( $g - $b ) / $delta; } else if ( $g == $max ) { $h = 2 + ( $b - $r ) / $delta; } else { $h = 4 + ( $r - $g ) / $delta; } } else { $h = 0; } $h *= 60; if ( $h < 0 ) { $h += 360; } return array( $h, $s, $v ); } protected function imageCalcSpot( $intSize, $fltFreq, $fltAngle ) { $fltAng = tan( deg2rad( $fltAngle ) ); $fltRad = 2.0 * PI(); $fltFactor = $intSize / $fltFreq; $arrSpot = array(); for ( $y = 0; $y < $intSize; $y++ ) { for ( $x = 0; $x < $intSize; $x++ ) { $value = cos( $fltRad * ( $x + $fltAng * $y ) / $fltFactor ) + cos( $fltRad * ( $y - $fltAng * $x ) / $fltFactor ); $arrSpot[ $y ][ $x ] = 128 + 63 * $value; } } return $arrSpot; } protected function imageCalcGammaLUT( $arrVal = array( "black" => 0, "gamma" => 1.0, "white" => 255 ) ) { $arrLUT = array(); $intMin = $arrVal[ "black" ]; $fltLUT = 255.0 * ( 255.0 / ( $arrVal[ "white" ] - $intMin ) ); for ( $i = 0; $i < $intMin; $i++ ) { $arrLUT[ $i ] = 0; } for ( $i = 0; $i < 256; $i++ ) { $intVal = round( $fltLUT * pow( $i / 255.0, 1.0 / $arrVal[ "gamma" ] ) ); if ( $intVal < 0 ) { $arrLUT[ $i + $intMin ] = 0; } else if ( $intVal > 255 ) { $arrLUT[ $i + $intMin ] = 255; } else { $arrLUT[ $i + $intMin ] = $intVal; } } return $arrLUT; } public function imageToolResize($width, $height) { $tmpimage = imagecreatetruecolor($width, $height); imagecopyresampled($tmpimage, $this->srcImage, 0, 0, 0, 0, $width, $height, $this->intWidth, $this->intHeight); imagedestroy($this->srcImage); $this->srcImage = $tmpimage; $this->intWidth = $width; $this->intHeight = $height; } public function imageToolResizePercent($percent = 100) { $width = ($this->intWidth/100)*$percent; $height = ($this->intHeight/100)*$percent; $tmpimage = imagecreatetruecolor($width, $height); imagecopyresampled($tmpimage, $this->srcImage, 0, 0, 0, 0, $width, $height, $this->intWidth, $this->intHeight); imagedestroy($this->srcImage); $this->srcImage = $tmpimage; $this->intWidth = $width; $this->intHeight = $height; } public function imageToolCrop($intX, $intY, $width, $height) { $tmpimage = imagecreatetruecolor($width, $height); imagecopyresampled($tmpimage, $this->srcImage, 0, 0, $intX, $intY, $width, $height, $width, $height); imagedestroy($this->srcImage); $this->srcImage = $tmpimage; $this->intWidth = $width; $this->intHeight = $height; } public function imageFilterGrayscale($strFilter = "normal" ) { $arrFilterSet = array( "normal" => array( 0.299, 0.587, 0.114 ), "ultrared" => array( 0.9, 0.07, 0.03 ), "infrared" => array( 0.8, 0.14, 0.06 ), "red" => array( 0.7, 0.2, 0.1 ), "orange" => array( 0.5, 0.3, 0.2 ) ); $arrFilter = $arrFilterSet[ $strFilter ]; for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = $arrFilter[0] * $red + $arrFilter[1] * $green + $arrFilter[2] * $blue; imageSetPixel( $this->srcImage, $x, $y, ( $gray << 16 ) | ( $gray << 8 ) | $gray ); } } } public function imageFilterEnhance() { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $lumi = max( $red, $green, $blue ); $gray = ( ( 255 - $lumi ) * $red + $lumi * $green ) >> 8; imageSetPixel( $this->srcImage, $x, $y, ( $gray << 16 ) | ( $gray << 8 ) | $gray ); } } } public function imageFilterFalseColor( $fltFactor = 320.0, $fltOffset = - 64.0 ) { $intLevel = 127.5; $fltHlf = 0.5 * PI(); $fltRad = 2.0 * PI(); for ( $i = 0; $i < 256; $i++ ) { $arrLUT_R[ $i ] = round( $intLevel * ( 1.0 + sin( - $fltHlf + $fltOffset + $fltRad * $i / $fltFactor ) ) ); $arrLUT_G[ $i ] = round( $intLevel * ( 1.0 + sin( $fltOffset + $fltRad * $i / $fltFactor ) ) ); $arrLUT_B[ $i ] = round( $intLevel * ( 1.0 + sin( $fltHlf + $fltOffset + $fltRad * $i / $fltFactor ) ) ); } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = ( 77 * $red + 150 * $green + 29 * $blue ) >> 8; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_R[ $gray ] << 16 ) | ( $arrLUT_G[ $gray ] << 8 ) | $arrLUT_B[ $gray ] ); } } } public function imageFilterBlackWhite( $intLevel = 128, $rgbBlack = array( "R" => 0x00, "G" => 0x00, "B" => 0x00 ), $rgbWhite = array( "R" => 0xFF, "G" => 0xFF, "B" => 0xFF ) ) { $colBlack = imageColorExactAlpha( $this->srcImage, $rgbBlack[ "R" ], $rgbBlack[ "G" ], $rgbBlack[ "B" ], 0 ); $colWhite = imageColorExactAlpha( $this->srcImage, $rgbWhite[ "R" ], $rgbWhite[ "G" ], $rgbWhite[ "B" ], 0 ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = ( 77 * $red + 150 * $green + 29 * $blue ) >> 8; if ( $gray > $intLevel ) { imageSetPixel( $this->srcImage, $x, $y, $colWhite ); } else { imageSetPixel( $this->srcImage, $x, $y, $colBlack ); } } } } public function imageFilterNoise( $intGray = 16, $intColor = 16 ) { for ( $i = 0; $i < 768; $i++ ) { if ( $i < 255 ) { $arrLUT[ $i ] = 0; } else if ( $i < 512 ) { $arrLUT[ $i ] = $i - 256; } else { $arrLUT[ $i ] = 255; } } $intGray2 = $intGray / 2; $intColor2 = $intColor / 2; for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $rndGray = rand( 0, $intGray ) - $intGray2; $red += 255 + $rndGray + mt_rand( 0, $intColor ) - $intColor2; $green += 255 + $rndGray + mt_rand( 0, $intColor ) - $intColor2; $blue += 255 + $rndGray + mt_rand( 0, $intColor ) - $intColor2; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT[ $red ] << 16 ) | ( $arrLUT[ $green ] << 8 ) | $arrLUT[ $blue ] ); } } } public function imageFilterInvert( ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = 255 - ( $rgbCol >> 16 ) & 0xFF; $green = 255 - ( $rgbCol >> 8 ) & 0xFF; $blue = 255 - $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageFilterMultiply( ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $red = ( $red * $red ) >> 8; $green = ( $green * $green ) >> 8; $blue = ( $blue * $blue ) >> 8; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageFilterTint( $rgbColor = array( "R" => 0xFF, "G" => 0x88, "B" => 0x00 ) ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = ( 77 * $red + 150 * $green + 29 * $blue ) >> 8; $red = ( $gray * $rgbColor[ "R" ] ) >> 8; $green = ( $gray * $rgbColor[ "G" ] ) >> 8; $blue = ( $gray * $rgbColor[ "B" ] ) >> 8; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageFilterColorize( $rgbColor = array( "R" => 0xFF, "G" => 0x88, "B" => 0x00 ) ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = ( 77 * $red + 150 * $green + 29 * $blue ) >> 8; $red = ( ( 255 - $gray ) * $rgbColor[ "R" ] + $gray * 255 ) >> 8; $green = ( ( 255 - $gray ) * $rgbColor[ "G" ] + $gray * 255 ) >> 8; $blue = ( ( 255 - $gray ) * $rgbColor[ "B" ] + $gray * 255 ) >> 8; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageFilterContrast( $fltContrastR = 0.0, $fltContrastG = 0.0, $fltContrastB = 0.0 ) { $arrLUT_R = array(); $arrLUT_G = array(); $arrLUT_B = array(); $fltRadian = 2.0 * PI(); for ( $i = 0; $i < 256; $i++ ) { $arrLUT_R[ $i ] = $i - round( 10.0 * $fltContrastR * sin( $fltRadian * $i / 255.0 ) ); $arrLUT_G[ $i ] = $i - round( 10.0 * $fltContrastG * sin( $fltRadian * $i / 255.0 ) ); $arrLUT_B[ $i ] = $i - round( 10.0 * $fltContrastB * sin( $fltRadian * $i / 255.0 ) ); } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_R[ $red ] << 16 ) | ( $arrLUT_G[ $green ] << 8 ) | $arrLUT_B[ $blue ] ); } } } public function imageFilterOptimize( ) { // get black and white point... $minR = $minG = $minB = 127; $gamR = $gamG = $gamB = 1.0; $maxR = $maxG = $maxB = 129; for ( $y = 0; $y < $this->intHeight - 2; $y += 2 ) { for ( $x = 0; $x < $this->intWidth - 2; $x += 2 ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; if ( $red < $minR ) $minR = $red; if ( $red > $maxR ) $maxR = $red; $green = ( $rgbCol >> 8 ) & 0xFF; if ( $green < $minG ) $minG = $green; if ( $green > $maxG ) $maxG = $green; $blue = $rgbCol & 0xFF; if ( $blue < $minB ) $minB = $blue; if ( $blue > $maxB ) $maxB = $blue; } } // calc correction LUT... $arrLUT_R = $this->imageCalcGammaLUT( array( "black" => $minR, "gamma" => $gamR, "white" => $maxR ) ); $arrLUT_G = $this->imageCalcGammaLUT( array( "black" => $minG, "gamma" => $gamG, "white" => $maxG ) ); $arrLUT_B = $this->imageCalcGammaLUT( array( "black" => $minB, "gamma" => $gamB, "white" => $maxB ) ); // optimize image... for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_R[ $red ] << 16 ) | ( $arrLUT_G[ $green ] << 8 ) | $arrLUT_B[ $blue ] ); } } } public function imageFilterGamma( $arrR = array( "black" => 0, "gamma" => 1.0, "white" => 255 ), $arrG = array( "black" => 0, "gamma" => 1.0, "white" => 255 ), $arrB = array( "black" => 0, "gamma" => 1.0, "white" => 255 ) ) { $arrLUT_R = $this->imageCalcGammaLUT( $arrR ); $arrLUT_G = $this->imageCalcGammaLUT( $arrG ); $arrLUT_B = $this->imageCalcGammaLUT( $arrB ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_R[ $red ] << 16 ) | ( $arrLUT_G[ $green ] << 8 ) | $arrLUT_B[ $blue ] ); } } } public function imageFilterGammaFast( $fltGamma = 1.0 ) { imageGammaCorrect( $this->srcImage, 1.0, $fltGamma ); } public function imageFilterSteps( $intStepR = 8, $intStepG = 8, $intStepB = 8 ) { $intStepR = 255 / $intStepR; $intStepG = 255 / $intStepG; $intStepB = 255 / $intStepB; $arrLUT_R = array(); $arrLUT_G = array(); $arrLUT_B = array(); for ( $i = 0; $i < 256; $i++ ) { $arrLUT_R[ $i ] = round( $intStepR * round( $i / $intStepR ) ); $arrLUT_G[ $i ] = round( $intStepG * round( $i / $intStepG ) ); $arrLUT_B[ $i ] = round( $intStepB * round( $i / $intStepB ) ); } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_R[ $red ] << 16 ) | ( $arrLUT_G[ $green ] << 8 ) | $arrLUT_B[ $blue ] ); } } } public function imageFilterFlipH( ) { $intWidth2 = $this->intWidth / 2; for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $intWidth2; $x++ ) { $rgbCol1 = imageColorAt( $this->srcImage, $x, $y ); $rgbCol2 = imageColorAt( $this->srcImage, $this->intWidth - $x - 1, $y ); imageSetPixel( $this->srcImage, $x, $y, $rgbCol2 ); imageSetPixel( $this->srcImage, $this->intWidth - $x - 1, $y, $rgbCol1 ); } } } public function imageFilterFlipV( ) { $intHeight2 = $this->intHeight / 2; for ( $x = 0; $x < $this->intWidth; $x++ ) { for ( $y = 0; $y < $intHeight2; $y++ ) { $rgbCol1 = imageColorAt( $this->srcImage, $x, $y ); $rgbCol2 = imageColorAt( $this->srcImage, $x, $this->intHeight - $y - 1 ); imageSetPixel( $this->srcImage, $x, $y, $rgbCol2 ); imageSetPixel( $this->srcImage, $x, $this->intHeight - $y - 1, $rgbCol1 ); } } } public function imageFilterFlipHV( ) { $intWidth2 = $this->intWidth / 2; $intHeight2 = $this->intHeight / 2; for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $intWidth2; $x++ ) { $rgbCol1 = imageColorAt( $this->srcImage, $x, $y ); $rgbCol2 = imageColorAt( $this->srcImage, $this->intWidth - $x - 1, $y ); imageSetPixel( $this->srcImage, $x, $y, $rgbCol2 ); imageSetPixel( $this->srcImage, $this->intWidth - $x - 1, $y, $rgbCol1 ); } } for ( $x = 0; $x < $this->intWidth; $x++ ) { for ( $y = 0; $y < $intHeight2; $y++ ) { $rgbCol1 = imageColorAt( $this->srcImage, $x, $y ); $rgbCol2 = imageColorAt( $this->srcImage, $x, $this->intHeight - $y - 1 ); imageSetPixel( $this->srcImage, $x, $y, $rgbCol2 ); imageSetPixel( $this->srcImage, $x, $this->intHeight - $y - 1, $rgbCol1 ); } } } public function imageFilterTechnicolor( ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $orange = ( $red + $red + $green ) / 3; $cyan = ( $green + $blue + $blue ) / 3; $red = $orange; $green = ( $orange + $cyan ) / 2; $blue = $cyan; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageFilterGetHue( ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $color = $this->rgb2hsv( $red, $green, $blue ); $gray = $color[ 0 ] >> 1; imageSetPixel( $this->srcImage, $x, $y, ( $gray << 16 ) | ( $gray << 8 ) | $gray ); } } } public function imageFilterScanline( $intSize = 1, $arrVal1 = array( "black" => 0, "gamma" => 1.4, "white" => 255 ), $arrVal2 = array( "black" => 25, "gamma" => 0.6, "white" => 230 ) ) { $arrLUT_1 = $this->imageCalcGammaLUT( $arrVal1 ); $arrLUT_2 = $this->imageCalcGammaLUT( $arrVal2 ); $intStep = $intSize * 2; for ( $y = 0; $y < $this->intHeight; $y++ ) { if ( ( $y % $intStep ) < $intSize ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_1[ $red ] << 16 ) | ( $arrLUT_1[ $green ] << 8 ) | $arrLUT_1[ $blue ] ); } } else { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT_2[ $red ] << 16 ) | ( $arrLUT_2[ $green ] << 8 ) | $arrLUT_2[ $blue ] ); } } } } public function imageFilterMonitor( $intXSize = 2, $intYSize = 5, $fltGammaA = 3.0, $fltGammaB = 0.5, $fltGammaC = 0.3 ) { $arrLUT_A = $this->imageCalcGammaLUT( array( "black" => 0, "gamma" => $fltGammaA, "white" => 255 ) ); $arrLUT_B = $this->imageCalcGammaLUT( array( "black" => 0, "gamma" => $fltGammaB, "white" => 270 ) ); $arrLUT_C = $this->imageCalcGammaLUT( array( "black" => 0, "gamma" => $fltGammaC, "white" => 285 ) ); $intCell2 = 2 * $intXSize; $intCell3 = 3 * $intXSize; $intCell4 = $intCell3 + 1; $intLine2 = 2 * $intYSize; $intLine3 = $intYSize - 1; for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; if ( ( $y % $intYSize ) == $intLine3 ) { $intVal = $intCell4; } else if ( ( $y % $intLine2 ) < $intYSize ) { $intVal = $x % $intCell4; } else { $intVal = ( $x + $intCell2 ) % $intCell4; } if ( $intVal < $intXSize ) { $red = $arrLUT_A[ $red ]; $green = $arrLUT_B[ $green ]; $blue = $arrLUT_B[ $blue ]; } else if ( $intVal < $intCell2 ) { $red = $arrLUT_B[ $red ]; $green = $arrLUT_A[ $green ]; $blue = $arrLUT_B[ $blue ]; } else if ( $intVal < $intCell3 ) { $red = $arrLUT_B[ $red ]; $green = $arrLUT_B[ $green ]; $blue = $arrLUT_A[ $blue ]; } else { $red = $arrLUT_C[ $red ]; $green = $arrLUT_C[ $green ]; $blue = $arrLUT_C[ $blue ]; } imageSetPixel( $this->srcImage, $x, $y, $red << 16 | $green << 8 | $blue ); } } } //BUG //times out.. public function imageFilterPixelate( $intXSize = 8, $intYSize = 8 ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $color = imageColorAt( $this->srcImage, $x * $intXSize, $y * $intYSize ); imageFilledRectangle( $this->srcImage, $x * $intXSize, $y * $intYSize, $x * $intXSize + $intXSize - 1, $y * $intYSize + $intYSize - 1, $color ); } } } public function imageFilterHistogram( $intAlpha = 32, $booGrid = true, $booR = true, $booG = true, $booB = true, $booL = false ) { $arrHist_R = array(); $arrHist_G = array(); $arrHist_B = array(); $arrHist_L = array(); for ( $i = 0; $i < 256; $i++ ) { $arrHist_R[ $i ] = 0; $arrHist_G[ $i ] = 0; $arrHist_B[ $i ] = 0; $arrHist_L[ $i ] = 0; } // collect values... for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $red = ( $rgbCol >> 16 ) & 0xFF; $green = ( $rgbCol >> 8 ) & 0xFF; $blue = $rgbCol & 0xFF; $gray = ( 77 * $red + 150 * $green + 29 * $blue ) >> 8; $arrHist_R[ $red ] += 1; $arrHist_G[ $green ] += 1; $arrHist_B[ $blue ] += 1; $arrHist_L[ $gray ] += 1; } } // find maximum... // define colors... $colBlack = imageColorExactAlpha( $this->srcImage, 0, 0, 0, $intAlpha ); $colGrid = imageColorExactAlpha( $this->srcImage, 96, 96, 96, $intAlpha ); $colRed = imageColorExactAlpha( $this->srcImage, 255, 96, 96, $intAlpha ); $colGreen = imageColorExactAlpha( $this->srcImage, 96, 255, 96, $intAlpha ); $colBlue = imageColorExactAlpha( $this->srcImage, 96, 96, 255, $intAlpha ); $colWhite = imageColorExactAlpha( $this->srcImage, 255, 255, 255, $intAlpha ); imageFilledRectangle( $this->srcImage, 0, 0, 255, 200, $colBlack ); // imageAntialias( $srcImage, true ); // render grid... if ( $booGrid ) { for ( $x = 0; $x < 52; $x++ ) { for ( $y = 0; $y < 41; $y++ ) { imageSetPixel( $this->srcImage, $x * 5, $y * 5, $colGrid ); } } } // render histogram... $intMaxAll = ( $this->intWidth * $this->intHeight ) / 32; for ( $x = 0; $x < 256; $x++ ) { if ( $x < 255 ) { if ( $booR and $arrHist_R[ $x ] ) imageLine( $this->srcImage, $x, 200 - 200 * $arrHist_R[ $x ] / $intMaxAll, $x + 1, 200 - 200 * $arrHist_R[ $x + 1 ] / $intMaxAll, $colRed ); if ( $booG and $arrHist_G[ $x ] ) imageLine( $this->srcImage, $x, 200 - 200 * $arrHist_G[ $x ] / $intMaxAll, $x + 1, 200 - 200 * $arrHist_G[ $x + 1 ] / $intMaxAll, $colGreen ); if ( $booB and $arrHist_B[ $x ] ) imageLine( $this->srcImage, $x, 200 - 200 * $arrHist_B[ $x ] / $intMaxAll, $x + 1, 200 - 200 * $arrHist_B[ $x + 1 ] / $intMaxAll, $colBlue ); if ( $booL and $arrHist_L[ $x ] ) imageLine( $this->srcImage, $x, 200 - 200 * $arrHist_L[ $x ] / $intMaxAll, $x + 1, 200 - 200 * $arrHist_L[ $x + 1 ] / $intMaxAll, $colWhite ); } } } public function imageFilterBlur( $intSize = 2 ) { $tmpImage = imageCreateTruecolor( $this->intWidth, $this->intHeight ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $blurP = imageColorAt( $this->srcImage, $x, $y ); $blurR = ( $blurP >> 16 ) & 0xFF; $blurG = ( $blurP >> 8 ) & 0xFF; $blurB = $blurP & 0xFF; $blurF = 1.0; for ( $f = 1; $f < $intSize; $f++ ) { if ( ( $x + $f ) < $this->intWidth ) { $blurP = imageColorAt( $this->srcImage, $x + $f, $y ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } if ( ( $x - $f ) >= 0 ) { $blurP = imageColorAt( $this->srcImage, $x - $f, $y ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } } $red = $blurR / $blurF; $green = $blurG / $blurF; $blue = $blurB / $blurF; imageSetPixel( $tmpImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $blurP = imageColorAt( $tmpImage, $x, $y ); $blurR = ( $blurP >> 16 ) & 0xFF; $blurG = ( $blurP >> 8 ) & 0xFF; $blurB = $blurP & 0xFF; $blurF = 1.0; for ( $f = 1; $f < $intSize; $f++ ) { if ( ( $y + $f ) < $this->intHeight ) { $blurP = imageColorAt( $tmpImage, $x, $y + $f ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } if ( ( $y - $f ) >= 0 ) { $blurP = imageColorAt( $tmpImage, $x, $y - $f ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } } $red = $blurR / $blurF; $green = $blurG / $blurF; $blue = $blurB / $blurF; imageSetPixel( $this->srcImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } imageDestroy( $tmpImage ); } public function imageFilterSharpen( $intSize = 2 ) { for ( $i = 0; $i < 768; $i++ ) { if ( $i < 255 ) { $arrLUT[ $i ] = 0; } else if ( $i < 512 ) { $arrLUT[ $i ] = $i - 256; } else { $arrLUT[ $i ] = 255; } } $tmpImage = imageCreateTruecolor( $this->intWidth, $this->intHeight ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $blurP = imageColorAt( $this->srcImage, $x, $y ); $blurR = ( $blurP >> 16 ) & 0xFF; $blurG = ( $blurP >> 8 ) & 0xFF; $blurB = $blurP & 0xFF; $blurF = 1.0; for ( $f = 1; $f < $intSize; $f++ ) { if ( ( $x + $f ) < $this->intWidth ) { $blurP = imageColorAt( $this->srcImage, $x + $f, $y ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } if ( ( $x - $f ) >= 0 ) { $blurP = imageColorAt( $this->srcImage, $x - $f, $y ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } } $red = $blurR / $blurF; $green = $blurG / $blurF; $blue = $blurB / $blurF; imageSetPixel( $tmpImage, $x, $y, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $blurP = imageColorAt( $tmpImage, $x, $y ); $blurR = ( $blurP >> 16 ) & 0xFF; $blurG = ( $blurP >> 8 ) & 0xFF; $blurB = $blurP & 0xFF; $blurF = 1.0; for ( $f = 1; $f < $intSize; $f++ ) { if ( ( $y + $f ) < $this->intHeight ) { $blurP = imageColorAt( $tmpImage, $x, $y + $f ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } if ( ( $y - $f ) >= 0 ) { $blurP = imageColorAt( $tmpImage, $x, $y - $f ); $blurR += ( ( $blurP >> 16 ) & 0xFF ); $blurG += ( ( $blurP >> 8 ) & 0xFF ); $blurB += ( $blurP & 0xFF ); $blurF += 1.0; } } $origP = imageColorAt( $this->srcImage, $x, $y ); $origR = ( $origP >> 16 ) & 0xFF; $origG = ( $origP >> 8 ) & 0xFF; $origB = $origP & 0xFF; $red = $origR + ( $origR - $blurR / $blurF ); $green = $origG + ( $origG - $blurG / $blurF ); $blue = $origB + ( $origB - $blurB / $blurF ); imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT[ 256 + $red ] << 16 ) | ( $arrLUT[ 256 + $green ] << 8 ) | $arrLUT[ 256 + $blue ] ); } } imageDestroy( $tmpImage ); } public function imageDrawBox( $intX, $intY, $intWidth = 8, $intHeight = 8, $rgbFill = array( "R" => 0x00, "G" => 0x00, "B" => 0x00, "A" => 0x00 ) ) { $colFill = imageColorExactAlpha( $this->srcImage, $rgbFill[ "R" ], $rgbFill[ "G" ], $rgbFill[ "B" ], $rgbFill[ "A" ] ); imageFilledRectangle( $this->srcImage, $intX, $intY, $intX + $intWidth - 1, $intY + $intHeight - 1, $colFill ); } public function imageDrawMoonPhase( $intX, $intY, $intSize = 16, $rgbBlack = array( "R" => 0x00, "G" => 0x00, "B" => 0x00, "A" => 0x00 ), $rgbWhite = array( "R" => 0xFF, "G" => 0xFF, "B" => 0xFF, "A" => 0x00 ) ) { $colBlack = imageColorExactAlpha( $this->srcImage, $rgbBlack[ "R" ], $rgbBlack[ "G" ], $rgbBlack[ "B" ], $rgbBlack[ "A" ] ); $colWhite = imageColorExactAlpha( $this->srcImage, $rgbWhite[ "R" ], $rgbWhite[ "G" ], $rgbWhite[ "B" ], $rgbWhite[ "A" ] ); $dateToday = mktime(); $dateFullMoon = mktime( 2, 22, 0, 8, 30, 2004 ); $intMoonPeriod = 29 * 86400 + 12 * 3600 + 44.0496 * 60; $intMoonPhase = ( $dateToday - $dateFullMoon ) % $intMoonPeriod; $fltPercent = $intMoonPhase / $intMoonPeriod; $intPercent = round( 200 * $fltPercent ); $colLeft = ( $fltPercent >= 0.5 ) ? $colBlack : $colWhite; $colRight = ( $fltPercent >= 0.5 ) ? $colWhite : $colBlack; if ( $intPercent > 100 ) { $intPercent = $intPercent - 100; } $intSize += 2; $fltHalf = 0.5 * $intSize - 0.5; for ( $i = 1; $i < $intSize - 1; $i++ ) { $intYPos = $intY + $i - 1; $intWidth = 2.0 * sqrt( $fltHalf * $fltHalf - ( $i - $fltHalf ) * ( $i - $fltHalf ) ); $intStart = $intX + $fltHalf - 0.5 * $intWidth; if ( $intPercent < 100 ) { $intLeft = $intWidth * ( ( 100 - $intPercent ) / 100 ); imageLine( $this->srcImage, $intStart, $intYPos, $intStart + $intLeft - 1, $intYPos, $colLeft ); } else { $intLeft = 0; } if ( $intPercent > 0 ) { $intRight = $intWidth * ( $intPercent / 100 ); imageLine( $this->srcImage, $intStart + $intLeft, $intYPos, $intStart + $intLeft + $intRight - 1, $intYPos, $colRight ); } } } public function imageDrawBorder( $intXBorder = 8, $intYBorder = 8, $rgbBorder = array( "R" => 0x00, "G" => 0x00, "B" => 0x00 ), $intXOffset = 0, $intYOffset = 0 ) { $intWidth = $this->intWidth - 1; $intHeight = $this->intHeight - 1; $colDraw = imageColorExactAlpha( $this->srcImage, $rgbBorder[ "R" ], $rgbBorder[ "G" ], $rgbBorder[ "B" ], $rgbBorder[ "A" ] ); if ( $intXBorder > 0 ) { imageFilledRectangle( $this->srcImage, 0, $intYBorder + $intYOffset, $intXBorder + $intXOffset - 1, $intHeight + $intYOffset - $intYBorder, $colDraw ); imageFilledRectangle( $this->srcImage, $intWidth - $intXBorder + $intXOffset + 1, $intYBorder + $intYOffset, $intWidth, $intHeight + $intYOffset - $intYBorder, $colDraw ); } if ( $intYBorder > 0 ) { imageFilledRectangle( $this->srcImage, 0, 0, $intWidth, $intYBorder + $intYOffset - 1, $colDraw ); imageFilledRectangle( $this->srcImage, 0, $intHeight + $intYOffset - $intYBorder + 1, $intWidth, $intHeight, $colDraw ); } } public function imageDrawShadow( $intBorder, $rgbShadow = array( "R" => 0x00, "G" => 0x00, "B" => 0x00 ), $rgbBack = array( "R" => 0xFF, "G" => 0xFF, "B" => 0xFF ) ) { $intWidth = $this->intWidth - 1; $intHeight = $this->intHeight - 1; $arrGamma = array(); for ( $i = 0; $i < $intBorder; $i++ ) { $arrGamma[ $i ] = 127.9 * pow( ( $i + 1 ) / $intBorder, 0.7 ); } $colDraw = imageColorExactAlpha( $this->srcImage, $rgbBack[ "R" ], $rgbBack[ "G" ], $rgbBack[ "B" ], 0 ); imageFilledRectangle( $this->srcImage, $intWidth - $intBorder, 0, $intWidth, $intHeight, $colDraw ); imageFilledRectangle( $this->srcImage, 0, $intHeight - $intBorder, $intWidth, $intHeight, $colDraw ); for ( $i = 0; $i < $intBorder; $i++ ) { $colDraw = imageColorExactAlpha( $this->srcImage, $rgbShadow[ "R" ], $rgbShadow[ "G" ], $rgbShadow[ "B" ], $arrGamma[ $i ] ); imageLine( $this->srcImage, $intWidth - $intBorder + $i, $i + 1, $intWidth - $intBorder + $i, $intHeight - $intBorder + $i, $colDraw ); imageLine( $this->srcImage, $i + 1, $intHeight - $intBorder + $i, $intWidth - $intBorder + $i - 1, $intHeight - $intBorder + $i, $colDraw ); } } public function imageDrawPointGrid( $intXSize = 8, $intYSize = 8, $rgbPoint = array( "R" => 0x00, "G" => 0x00, "B" => 0x00, "A" => 0xD0 ), $intXOffset = 0, $intYOffset = 0 ) { $intWidth = $this->intWidth / $intXSize; $intHeight = $this->intHeight / $intYSize; $colDraw = imageColorExactAlpha( $this->srcImage, $rgbPoint[ "R" ], $rgbPoint[ "G" ], $rgbPoint[ "B" ], $rgbPoint[ "A" ] ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { imageSetPixel( $this->srcImage, $x * $intXSize + $intXOffset, $y * $intYSize + $intYOffset, $colDraw ); } } } public function imageDrawInvPointGrid( $intXSize = 8, $intYSize = 8, $intXOffset = 0, $intYOffset = 0 ) { for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x * $intXSize + $intXOffset, $y * $intYSize + $intYOffset ); $red = 255 - ( $rgbCol >> 16 ) & 0xFF; $green = 255 - ( $rgbCol >> 8 ) & 0xFF; $blue = 255 - $rgbCol & 0xFF; imageSetPixel( $this->srcImage, $x * $intXSize + $intXOffset, $y * $intYSize + $intYOffset, ( $red << 16 ) | ( $green << 8 ) | $blue ); } } } public function imageDrawLineGrid( $intXSize = 8, $intYSize = 8, $rgbLine = array( "R" => 0x00, "G" => 0x00, "B" => 0x00, "A" => 0xD0 ), $intXOffset = 0, $intYOffset = 0 ) { $intWidth = $this->intWidth / $intXSize; $intHeight = $this->intHeight / $intYSize; $colDraw = imageColorExactAlpha( $this->srcImage, $rgbLine[ "R" ], $rgbLine[ "G" ], $rgbLine[ "B" ], $rgbLine[ "A" ] ); for ( $x = 0; $x < $intWidth; $x++ ) { imageLine( $this->srcImage, $x * $intXSize + $intXOffset, 0, $x * $intXSize + $intXOffset, $intHeight * $intYSize, $colDraw ); } for ( $y = 0; $y < $intHeight; $y++ ) { imageLine( $this->srcImage, 0, $y * $intYSize + $intYOffset, $intWidth * $intXSize, $y * $intYSize + $intYOffset, $colDraw ); } } public function imageDrawInnerShadow( $intBorder = 8, $rgbShadow = array( "R" => 0x00, "G" => 0x00, "B" => 0x00 ) ) { $arrGamma = array(); for ( $i = 0; $i < $intBorder; $i++ ) { $arrGamma[ $i ] = 127.0 * pow( ( $i + 1 ) / $intBorder, 0.7 ); } for ( $i = 0; $i < $intBorder; $i++ ) { $colDraw = imageColorExactAlpha( $this->srcImage, $rgbShadow[ "R" ], $rgbShadow[ "G" ], $rgbShadow[ "B" ], $arrGamma[ $i ] ); imageLine( $this->srcImage, $i, $i, $this->intWidth - 1, $i, $colDraw ); imageLine( $this->srcImage, $i, $i + 1, $i, $this->intHeight - 1, $colDraw ); } } public function imageDrawInnerGlow( $intBorder = 8, $rgbGlow = array( "R" => 0xFF, "G" => 0xFF, "B" => 0xFF ) ) { $arrGamma = array(); for ( $i = 0; $i < $intBorder; $i++ ) { $arrGamma[ $i ] = 127.0 * pow( ( $i + 1 ) / $intBorder, 0.7 ); } for ( $i = 0; $i < $intBorder; $i++ ) { $colDraw = imageColorExactAlpha( $this->srcImage, $rgbGlow[ "R" ], $rgbGlow[ "G" ], $rgbGlow[ "B" ], $arrGamma[ $i ] ); imageLine( $this->srcImage, $i, $i, $this->intWidth - $i - 1, $i, $colDraw ); imageLine( $this->srcImage, $i, $i + 1, $i, $this->intHeight - $i - 1, $colDraw ); imageLine( $this->srcImage, $this->intWidth - $i - 1, $i + 1, $this->intWidth - $i - 1, $this->intHeight - $i - 1, $colDraw ); imageLine( $this->srcImage, $i + 1, $this->intHeight - $i - 1, $this->intWidth - $i - 2, $this->intHeight - $i - 1, $colDraw ); } } public function imageDrawText( $intFontSize, $intAngle, $intXPos, $intYPos, $rgbText = array( "R" => 0xFF, "G" => 0xFF, "B" => 0xFF, "A" => 0x00 ), $strFontName, $strText ) { $strPath = "_fonts/"; $colDraw = imageColorExactAlpha( $this->srcImage, $rgbText[ "R" ], $rgbText[ "G" ], $rgbText[ "B" ], $rgbText[ "A" ] ); imageTTFText( $this->srcImage, $intFontSize, $intAngle, $intXPos, $intYPos, $colDraw, $strPath.$strFontName, $strText ); } /* * * IMAGEEDIT:DITHER * */ public function imageDitherPrintBlack( $intSize = 8, $intBlur = 6 ) { $intSize *= 15; $arrDither = $this->imageCalcSpot( $intSize, 15, 15.0 ); // 15 degrees $intOffset = 255 - ( 255 / $intBlur ) / 2; for ( $i = 0; $i < 512; $i++ ) { $intVal = round( $intBlur * ( $i - $intOffset ) ); if ( $intVal < 0 ) { $arrLUT[ $i ] = 0; } else if ( $intVal > 255 ) { $arrLUT[ $i ] = 255; } else { $arrLUT[ $i ] = $intVal; } } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $intR = ( $rgbCol >> 16 ) & 0xFF; $intG = ( $rgbCol >> 8 ) & 0xFF; $intB = $rgbCol & 0xFF; $gray = ( 77 * $intR + 150 * $intG + 29 * $intB ) >> 8; $value = $arrLUT[ 255 + $gray - $arrDither[ $x % $intSize ][ $y % $intSize ] ]; imageSetPixel( $this->srcImage, $x, $y, ( $value << 16 ) | ( $value << 8 ) | $value ); } } } public function imageDitherPrintColor( $intSize = 8, $intBlur = 6 ) { $intSize *= 15; $arrDitherR = $this->imageCalcSpot( $intSize, 15.0, - 15.0 ); // cyan: 75 degrees $arrDitherG = $this->imageCalcSpot( $intSize, 15.0, 15.0 ); // magenta: 15 degrees // optimized spot size for yellow... if ( $intSize == 90 ) { $arrDitherB = $this->imageCalcSpot( $intSize, 9.0, 45.0 ); // yellow: 45 degrees } else if ( $intSize == 120 ) { $arrDitherB = $this->imageCalcSpot( $intSize, 12.0, 45.0 ); // yellow: 45 degrees } else if ( $intSize == 180 ) { $arrDitherB = $this->imageCalcSpot( $intSize, 12.0, 45.0 ); // yellow: 45 degrees } else { $arrDitherB = $this->imageCalcSpot( $intSize, 15.0, 45.0 ); // yellow: 45 degrees } $intOffset = 255 - ( 255 / $intBlur ) / 2; for ( $i = 0; $i < 512; $i++ ) { $intVal = round( $intBlur * ( $i - $intOffset ) ); if ( $intVal < 0 ) { $arrLUT[ $i ] = 0; } else if ( $intVal > 255 ) { $arrLUT[ $i ] = 255; } else { $arrLUT[ $i ] = $intVal; } } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $intR = ( $rgbCol >> 16 ) & 0xFF; $intG = ( $rgbCol >> 8 ) & 0xFF; $intB = $rgbCol & 0xFF; $intR = 255 + $intR - $arrDitherR[ $x % $intSize ][ $y % $intSize ]; $intG = 255 + $intG - $arrDitherG[ $x % $intSize ][ $y % $intSize ]; $intB = 255 + $intB - $arrDitherB[ $x % $intSize ][ $y % $intSize ]; imageSetPixel( $this->srcImage, $x, $y, ( $arrLUT[ $intR ] << 16 ) | ( $arrLUT[ $intG ] << 8 ) | $arrLUT[ $intB ] ); } } } public function imageDitherSimple( ) { $arrDither[ 0 ] = array( 0, 8, 2, 10 ); $arrDither[ 1 ] = array( 12, 4, 14, 6 ); $arrDither[ 2 ] = array( 3, 11, 1, 9 ); $arrDither[ 3 ] = array( 15, 7, 13, 5 ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; $newG = ( $rgbG > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; $newB = ( $rgbB > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; imageSetPixel( $this->srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } public function imageDitherPrint( ) { $arrDither[ 0 ] = array( 7, 111, 183, 239, 231, 191, 71, 15 ); $arrDither[ 1 ] = array( 87, 47, 135, 175, 199, 159, 55, 127 ); $arrDither[ 2 ] = array( 215, 143, 39, 79, 119, 63, 151, 207 ); $arrDither[ 3 ] = array( 247, 223, 103, 31, 23, 95, 167, 255 ); $arrDither[ 4 ] = array( 231, 191, 71, 15, 7, 111, 183, 239 ); $arrDither[ 5 ] = array( 199, 159, 55, 127, 87, 47, 135, 175 ); $arrDither[ 6 ] = array( 119, 63, 151, 207, 215, 143, 39, 79 ); $arrDither[ 7 ] = array( 23, 95, 167, 255, 247, 223, 103, 31 ); for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newG = ( $rgbG > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newB = ( $rgbB > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; imageSetPixel( $this->srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } public function imageDitherSketch($intCell = 8 ) { $kc = 256 / $intCell; $jc = $kc / $intCell; for ( $i = 0; $i < $intCell; $i++ ) { for ( $j = 0; $j < $intCell; $j++ ) { $k1 = abs( ( $j + $i ) % $intCell ); $k2 = abs( ( $j - $i ) % $intCell ); $arrDitherR[ $i ][ $j ] = round( abs( 2 * $k1 * $kc + $j * $jc - 254 ) ); $arrDitherG[ $i ][ $j ] = round( abs( 2 * $k2 * $kc + $j * $jc - 254 ) ); $arrDitherB[ $i ][ $j ] = round( abs( 2 * $i * $kc + $j * $jc - 254 ) ); } } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDitherR[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; $newG = ( $rgbG > $arrDitherG[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; $newB = ( $rgbB > $arrDitherB[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; imageSetPixel( $this->srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } public function imageDitherSierra($intSteps = 4 ) { $fltLevel = 255 / $intSteps; for ( $i = 0; $i < 256; $i++ ) { $arrSteps[ $i ] = round( round( $i / $fltLevel ) * $fltLevel ); } for ( $i = 0; $i < 512; $i++ ) { $arrLUT[ $i ] = ( $i < 255 ) ? $i : 255; } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = $arrSteps[ $rgbR ]; $newG = $arrSteps[ $rgbG ]; $newB = $arrSteps[ $rgbB ]; imageSetPixel( $this->srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); $deltaR = $rgbR - $newR; $deltaG = $rgbG - $newG; $deltaB = $rgbB - $newB; // $rgbCol = imageColorAt( $this->srcImage, $x + 1, $y ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 1 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 1 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 1 ); imageSetPixel( $this->srcImage, $x + 1, $y, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $this->srcImage, $x - 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 2 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 2 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 2 ); imageSetPixel( $this->srcImage, $x - 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $this->srcImage, $x, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 2 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 2 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 2 ); imageSetPixel( $this->srcImage, $x, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); } } } public function imageDitherFloyd( $intSteps = 4 ) { $fltLevel = 255 / $intSteps; for ( $i = 0; $i < 256; $i++ ) { $arrSteps[ $i ] = round( round( $i / $fltLevel ) * $fltLevel ); } for ( $i = 0; $i < 512; $i++ ) { $arrLUT[ $i ] = ( $i < 255 ) ? $i : 255; } for ( $y = 0; $y < $this->intHeight; $y++ ) { for ( $x = 0; $x < $this->intWidth; $x++ ) { $rgbCol = imageColorAt( $this->srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = $arrSteps[ $rgbR ]; $newG = $arrSteps[ $rgbG ]; $newB = $arrSteps[ $rgbB ]; imageSetPixel( $this->srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); $deltaR = $rgbR - $newR; $deltaG = $rgbG - $newG; $deltaB = $rgbB - $newB; // $rgbCol = imageColorAt( $this->srcImage, $x + 1, $y ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 7 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 7 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 7 * $deltaB ) >> 4 ); imageSetPixel( $this->srcImage, $x + 1, $y, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $this->srcImage, $x - 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 3 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 3 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 3 * $deltaB ) >> 4 ); imageSetPixel( $this->srcImage, $x - 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $this->srcImage, $x, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 5 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 5 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 5 * $deltaB ) >> 4 ); imageSetPixel( $this->srcImage, $x, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $this->srcImage, $x + 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 1 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 1 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 1 * $deltaB ) >> 4 ); imageSetPixel( $this->srcImage, $x + 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); } } } }