Source for file Mask.php
Documentation is available at Mask.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
* This is a driver file contains the Image_Tools_Mask class.
* Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the BSD License.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version CVS: $Id: Mask.php,v 1.2 2006/11/23 22:17:31 firman Exp $
* Load Image_Tools as the base class.
require_once 'Image/Tools.php';
* Require for color mixing. Notes: conflict when integrated with
* Image_Color v1.0.0 (http://pear.php.net/package/Image_Color).
require_once 'Image/Color.php';
// {{{ Class: Image_Tools_Mask
* This class provide masking tool for manipulating an image.
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version Release: 0.4.1
* image mixed Destination image, a filename or an image string
* data or a GD image resource.
* mask mixed Mask image, a filename or an image string
* data or a GD image resource.
* sample mixed Sample image, a filename or an image string
* data or a GD image resource.
* mask_color mixed Mask color, use string for hexa color format or
* array contains 3 indexes 0 for RGB format
* unmask_color mixed Mask color, use string for hexa color format or
* array contains 3 indexes 0 for RGB format
* antialias bool Flag whether attempt to draw antialias mask
* antialias_factor int Antialias factor, this setting for antialias
'image' => null , // Destination image.
'mask' => null , // Mask image.
'sample' => null , // Sample image.
'mask_color' => '000000', // Mask color.
'unmask_color' => 'ffffff', // Unmask color.
'antialias' => true , // Antialias flag.
'antialias_factor' => 16 // Antialias factor.
* Available options for Image_Tools_Mask.
'unmask_color' => 'mixed',
'antialias_factor' => 'int'
* There is no public methods in Image_Tool_Mask.
* Image_Tools_Mask API version.
* GD image resource for mask image.
* GD image resource for sample image.
* Initialize some internal variables.
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
if (PEAR ::isError ($res)) {
$this->_maskImage = $res;
if (PEAR ::isError ($res)) {
$this->_sampleImage = $res;
if (PEAR ::isError ($res)) {
* This function scan for mask color and closes colors position, grab color
* at found the position on sample image, then set the pixel color at the same
* position on destination image.
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
* @see Image_Tools_Mask::_getNearestColors()
if (PEAR ::isError ($res)) {
return PEAR ::raiseError ('Invalid image resource Image_Tools_Mask::$_maskImage');
return PEAR ::raiseError ('Invalid image resource Image_Tools_Mask::$_sampleImage');
return PEAR ::raiseError ('Invalid image resource Image_Tools_Mask::$_resultImage');
$maskWidth = imagesx($this->_maskImage);
$maskHeight = imagesy($this->_maskImage);
$sampleWidth = imagesx($this->_sampleImage);
$sampleHeight = imagesy($this->_sampleImage);
$closesColors = $this->_getNearestColors ();
$closesColors = array ($this->options['maskColor']);
// scan for mask color or closes colors position
for ($x = 0; $x < $maskWidth; $x++ ) {
for ($y = 0; $y < $maskHeight; $y++ ) {
if ($x >= $sampleWidth || $y >= $sampleHeight) {
// grab color at x, y and convert to hex color format
// check color in closes colors collection
if (in_array($maskColor, $closesColors)) {
// grab color at x, y from sample image
// allocate color on destination image
// set a pixel color at destination image
* Get nearest colors between mask color and unmask color using
* @return array Colors range.
function _getNearestColors ()
$objColor = new Image_Color;
$objColor->setColors ($this->options['mask_color'], $this->options['unmask_color']);
return $objColor->getRange ($this->options['antialias_factor']);
* c-hanging-comment-ender-p: nil
Documentation generated on Thu, 23 Nov 2006 18:00:07 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|