* version 1.01 * * constructor Watermark( $sWatermarkFile='') * public function applyWatermark ( $sImageFile, $sWatermarkedFile='') * public function setWatermark( $sWatermarkFile) * public function setWatermarkPosition ( $Hposition, $Vposition) * public function setJpgQuality() * public function getLastError() * public function cleanUp() */ // position constants define ('WATERMARK_VPOS_TOP', -1); define ('WATERMARK_VPOS_MIDDLE', 0); define ('WATERMARK_VPOS_BOTTOM', 1); define ('WATERMARK_HPOS_LEFT', -1); define ('WATERMARK_HPOS_CENTER', 0); define ('WATERMARK_HPOS_RIGHT', 1); // randomize level constants define ('WATERMARK_RANDOM_PIXEL_LIGHT_LEVEL', 7); define ('WATERMARK_RANDOM_PIXEL_POSITION_LEVEL', 2); class Watermark{ var $rWatermark = false; var $rImage = false; var $iMarkWidth; var $iMarkHeight; var $iMarkPosX = WATERMARK_HPOS_RIGHT; var $iMarkPosY = WATERMARK_VPOS_BOTTOM; var $bPNG = false; var $bJPG = false; var $iJpgQuality = 75; var $errorMsg = ''; /*============= CONSTRUCTOR =============*/ /** * Constructor * * @param string $sWatermarkFile filename of stamp image * @return boolean * @access public * @uses setWatermark() */ function Watermark($sWatermarkFile='', $iHposition=null, $iVposition=null) { if(!function_exists('gd_info')){ return false; }else{ $aGdInfo = gd_info(); $this->bPNG = isset($aGdInfo['PNG Support']) ? $aGdInfo['PNG Support'] : false; $this->bJPG = isset($aGdInfo['JPG Support']) ? $aGdInfo['JPG Support'] : false; } return( $this->setWatermark($sWatermarkFile, $iHposition, $iVposition) ); } // end constructor /*================ PUBLIC METHODS ================*/ /** * retrieve last error message * * @return string * @access public * @uses errorMsg */ function getLastError() { return($this->errorMsg); } // end function getLastError /** * destroy watermark image * * @return void * @access public * @uses rWatermark */ function cleanUp() { if($this->rWatermark){ imagedestroy($this->rWatermark); } } // end function cleanUp /** * mark an image file and display/save it * * @param int $sImageFile image file (JPEG or PNG format) * @param int $sResultImageFile new image file (same format) * @return boolean * @access public * @uses _readImage() * @uses _markImage() * @uses _writeImage() * @uses _displayImage() * @uses rWatermark * @uses rImage * @uses errorMsg */ function applyWatermark ( $sImageFile, $sWatermarkedFile='') { $result = false; if (!$this->rWatermark) { $this->errorMsg = 'Watermark image is not set.'; }else{ if( ($aImageInfo = @getimagesize($sImageFile)) === false){ $this->errorMsg = "Error examining '$sImageFile'. Image must be a valid PNG or JPEG file."; }else{ $iType = $aImageInfo[2]; if( ($this->rImage = $this->_readImage($sImageFile, $iType)) === false ){ $this->errorMsg .= "Error on loading '$sImageFile'. Image must be a valid PNG or JPEG file."; }else{ $this->_markImage(); ($sWatermarkedFile == '') ? $this->_displayImage($iType) : $this->_writeImage($sWatermarkedFile, $iType); imagedestroy($this->rImage); $result = true; } } } return $result; } // end function applyWatermark /** * set stamp image for watermak * * @param string $sWatermarkFile image file (JPEG or PNG) * @return boolean * @access public * @uses setWatermarkPosition() * @uses _readImage() * @uses rWatermark * @uses iMarkWidth * @uses iMarkHeight * @uses errorMsg */ function setWatermark($sWatermarkFile, $iHposition=null, $iVposition=null) { $result = false; if( ($aImageInfo = @getimagesize($sWatermarkFile)) === false){ $this->errorMsg = "Error on examining '$sWatermarkFile'. Watermark image must be a valid PNG or JPEG file."; }else{ $iWidth = $aImageInfo[0]; $iHeight = $aImageInfo[1]; $iType = $aImageInfo[2]; if($this->rWatermark){ imagedestroy($this->rWatermark); } $this->rWatermark = $this->_readImage($sWatermarkFile, $iType); if(!$this->rWatermark) { $this->errorMsg .= "Error on loading '$sWatermarkFile'. Watermark image must be a valid PNG or JPEG file."; }else{ $this->iMarkWidth = $iWidth; $this->iMarkHeight = $iHeight; if($iHposition || $iVposition){ $this->setWatermarkPosition($iHposition, $iVposition); } $result = true; } } return($result); } // end function setWatermark /** * set watermark position on image * * @param int $iHposition horizontal x position * @param int $iVposition vertical y position * @return void * @access public * @uses iMarkPosX * @uses iMarkPosY * @uses errorMsg */ function setWatermarkPosition ( $iHposition=WATERMARK_HPOS_RIGHT, $iVposition=WATERMARK_VPOS_BOTTOM) { // set horizontal (x) position switch ($iHposition) { case WATERMARK_HPOS_LEFT: case WATERMARK_HPOS_CENTER: case WATERMARK_HPOS_RIGHT: $this->iMarkPosX = $iHposition; break; default: /* do nothing */; } // set vertical (y) position switch ($iVposition) { case WATERMARK_VPOS_TOP: case WATERMARK_VPOS_MIDDLE: case WATERMARK_VPOS_BOTTOM: $this->iMarkPosY = $iVposition; break; default: /* do nothing */; } } // end function setWatermarkPosition /** * set quality of output watermaked JPG image * * @param int $iQuality * @return void * @access public * @uses iJpgQuality */ function setJpgQuality ( $iQuality ) { if(is_int($iQuality) && $iQuality > 0 && $iQuality <= 100){ $this->iJpgQuality = $iQuality; } } // end function setJpgQuality /*================= PRIVATE METHODS =================*/ /** * mark an image * * @return boolean * @access private * @uses rImage * @uses iMarkWidth * @uses iMarkHeight * @uses rWatermark * @uses iMarkPosX * @uses iMarkPosY */ function _markImage() { if(!$this->rWatermark){ $this->errorMsg = 'Watermark image is not set.'; return(false); } $iImageWidth = imagesx($this->rImage); $iImageHeight = imagesy($this->rImage); //set position of watermark switch($this->iMarkPosX){ case WATERMARK_HPOS_LEFT: $iOffsetLeft = 0; break; case WATERMARK_HPOS_CENTER: $iOffsetLeft = ($iImageWidth - $this->iMarkWidth) / 2; break; case WATERMARK_HPOS_RIGHT: $iOffsetLeft = $iImageWidth - $this->iMarkWidth; break; default: $iOffsetLeft = 0; } switch($this->iMarkPosY){ case WATERMARK_VPOS_TOP: $iOffsetTop = 0; break; case WATERMARK_VPOS_MIDDLE: $iOffsetTop = ($iImageHeight - $this->iMarkHeight) / 2; break; case WATERMARK_VPOS_BOTTOM: $iOffsetTop = $iImageHeight - $this->iMarkHeight; break; default: $iOffsetTop = 0; } // randomize position $iOffsetLeft += rand(-WATERMARK_RANDOM_PIXEL_POSITION_LEVEL, +WATERMARK_RANDOM_PIXEL_POSITION_LEVEL); $iOffsetTop += rand(-WATERMARK_RANDOM_PIXEL_POSITION_LEVEL, +WATERMARK_RANDOM_PIXEL_POSITION_LEVEL); // for each pixel of watermark for($x=0; $x<$this->iMarkWidth; $x++){ if( ($x + $iOffsetLeft) < 0 || ($x + $iOffsetLeft) >= $iImageWidth ){ continue; } for($y=0; $y<$this->iMarkHeight; $y++){ if( ($y + $iOffsetTop) < 0 || ($y + $iOffsetTop) >= $iImageHeight ){ continue; } // search RGB values of watermark image pixel $iMarkIndex = imagecolorat($this->rWatermark, $x, $y); $iMarkRGB = imagecolorsforindex($this->rWatermark, $iMarkIndex); // search RGB values of image pixel $iImageIndex = imagecolorat($this->rImage, $x + $iOffsetLeft, $y + $iOffsetTop); $iImageRGB = imagecolorsforindex($this->rImage, $iImageIndex); // randomize light shift $iMarkAverage = ($iMarkRGB['red'] + $iMarkRGB['green'] + $iMarkRGB['blue']) / 3; if($iMarkAverage > 10){ $iRandomizer = rand(-WATERMARK_RANDOM_PIXEL_LIGHT_LEVEL, +WATERMARK_RANDOM_PIXEL_LIGHT_LEVEL); } else{ $iRandomizer = 0; } // compute new values of colors pixel $r = max( min($iImageRGB['red'] + $iMarkRGB['red'] + $iRandomizer - 0x80, 0xFF), 0x00); $g = max( min($iImageRGB['green'] + $iMarkRGB['green'] + $iRandomizer - 0x80, 0xFF), 0x00); $b = max( min($iImageRGB['blue'] + $iMarkRGB['blue'] + $iRandomizer - 0x80, 0xFF), 0x00); // change image pixel imagesetpixel ( $this->rImage, $x + $iOffsetLeft, $y + $iOffsetTop, ($r<<16) + ($g<<8) + $b); } } } // end function markImage /** * read image from file * * @param string $file image file (JPEG or PNG) * @param int $type file type (2:JPEG or 3:PNG) * @return resource * @access protected * @uses errorMsg */ function _readImage($sFile, $iType) { switch ($iType) { case 2: //JPEG if($this->bJPG){ return(imagecreatefromjpeg($sFile)); } else{ $this->errorMsg = 'No JPG Support. '; return false; } break; case 3: //PNG if($this->bPNG){ return(imagecreatefrompng($sFile)); } else{ $this->errorMsg = 'No PNG Support. '; return false; } break; default: $this->errorMsg = 'File format not supported.'; return false; } } // end function _readImage /** * write image to file * * @param string $sWatermarkedFile image file (JPEG or PNG) * @param int $iType file type (2:JPEG or 3:PNG) * @return void * @access protected * @uses rImage * @uses errorMsg */ function _writeImage($sWatermarkedFile, $iType) { switch($iType){ case 2: //JPEG imagejpeg($this->rImage, $sWatermarkedFile, $this->iJpgQuality); break; case 3: //PNG imagepng($rthis->Image, $sWatermarkedFile); break; default: $this->errorMsg = 'File format not supported.'; } } // end function _writeImage /** * send image to stdout * * @param int $iType image type (2:JPEG or 3:PNG) * @return void * @access protected * @uses rImage * @uses errorMsg */ function _displayImage( $iType) { switch($iType) { case 2: //JPEG ob_start(); imagejpeg($this->rImage, '', $this->iJpgQuality); $iImageSize = ob_get_length(); $sImage = ob_get_clean(); header('Content-Type: image/jpeg'); header('Content-Length: '.$iImageSize); header('Content-Disposition: attachment; filename=image'.time().'.jpg'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 13 Mar 2006 01:00:00 GMT'); echo $sImage; unset($sImage, $iImageSize); break; case 3: //PNG ob_start(); imagepng($this->rImage); $iImageSize = ob_get_length(); $sImage = ob_get_clean(); header('Content-Type: image/png'); header('Content-Length: '.$iImageSize); header('Content-Disposition: attachment; filename=image'.time().'.png'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 13 Mar 2006 01:00:00 GMT'); echo $sImage; unset($sImage, $iImageSize); break; default: $this->errorMsg = 'File format not supported.'; } } // end function _displayImage } // end class WaterMark ?> Leuke Klompen. Eindelijk een site met écht leuke klompen
Skip Navigation
Uw winkelmandje is leeg
« homepage