Newer
Older
Import / web / www.xiaofrog.com / gallery / modules / gd / classes / GdFunctionality.class
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2007 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * All image functions from the GD library are implemented here.
 *
 * Every call to these functions should go through this class. Errors are handled the Gallery-way
 * (GalleryStatus) and it provides an easy way to test the Toolkit (by using an implementation with
 * pseudo-functionality instead of this class).
 *
 * @package Gd
 * @subpackage Classes
 * @author Ernesto Baschny <ernst@baschny.de>
 * @version $Revision: 15513 $
 */
class GdFunctionality {
    /**
     * @return array object GalleryStatus
     *	       string phpinfo output
     */
    function phpinfo($section=null) {
	ob_start();
	phpinfo($section);
	$phpinfo = ob_get_contents();
	ob_end_clean();
	return array(null, $phpinfo);
    }

    /**
     * The tempnam method from the GalleryPlatform we will need to overwrite in our testcases
     * @return string
     */
    function tempnam($tmpDir, $prefix) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->tempnam($tmpDir, $prefix);
    }

    /**
     * The copy method from GalleryPlatform, which we need to be able to overwrite in tests
     * @return string
     */
    function copy($src, $dest) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->copy($src, $dest);
    }

    /**
     * The unlink method from GalleryPlatform, which we need to be able to overwrite in tests
     * @return string
     */
    function unlink($file) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->unlink($file);
    }

    /**
     * The filesize method from GalleryPlatform, which we need to be able to overwrite in tests
     * @return int
     */
    function filesize($file) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->filesize($file);
    }

    /**
     * Set file permissions according to preferences
     * @param string $file file path
     * @return boolean true on success
     */
    function chmod($file) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->chmod($file);
    }

    /**
     * Just check if a function exists or not.
     * This is here so we can substitute the function-checker routine in our phpunit testcases.
     *
     * @return boolean true if exists
     */
    function functionExists($fct) {
	return function_exists($fct);
    }

    /**
     * Return an integer representing the known imageTypes
     * @return int
     */
    function imageTypes() {
	return imageTypes();
    }

    /**
     * Return an array of information about the currently installed GD version
     *
     * Requires:
     * - PHP 4 >= 4.3.0, PHP 5
     *
     * @return array object GalleryStatus
     *	       array the output of gd_info
     */
    function gd_info() {
	if (!function_exists('gd_info')) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
	}
	$info = @gd_info();
	return array(null, $info);
    }

    /**
     * Return the size (width, height) of an image file.
     * @return array (x, y)
     */
    function getImageSize($filename) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	return $platform->getimagesize($filename);
    }

    /**
     * Create a new palette based image with specified width and height
     *
     * Requires:
     * - any PHP
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreate($width, $height) {
	$res = @imageCreate($width, $height);
	if (!$res) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION),
			 null);
	}
	return array(null, $res);
    }

    /**
     * Create a new true color image with specified width and height
     *
     * Requires:
     * - PHP 4 >= 4.0.6 or PHP 5
     * - GD >= 2.0.1
     *
     * Danger: The imageCreateTruecolor() function is defined even if the
     * GD library does not support it! If called in this situation, the call
     * will generate a "Fatal error", which cannot be catched. So we have to
     * make sure to call this method ONLY if we are sure to have GD >= 2.0.1
     * (which at the current stage of this module will always be the case,
     * since this is the minimal required version).
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateTruecolor($width, $height) {
	if (!function_exists('imageCreateTruecolor')) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION),
			 null);
	}
	$res = @imageCreateTruecolor($width, $height);
	if (!$res) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
	}
	return array(null, $res);
    }

    /**
     * Helper functionality to see if a filename is readable
     * @return boolean
     */
    function isValidFile($filename) {
	global $gallery;
	$platform =& $gallery->getPlatform();
	if (!$platform->file_exists($filename)
	      || !$platform->is_file($filename)
	      || !$platform->is_readable($filename)) {
	    return false;
	}
	return true;
    }

    /**
     * Check if a resource that is based on $filename is valid
     *
     * @return array object GalleryStatus
     *	       resource GD resource
     */
    function checkResource($res, $filename) {
	if (!$res) {
	    if (!$this->isValidFile($filename)) {
		return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_PLATFORM_FAILURE),
			     null);
	    }
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_FILE_TYPE),
			 null);
	}
	return array(null, $res);
    }

    /**
     * Create a new image from GIF file or URL.
     *
     * Requires
     * - any PHP version
     * - GD with GIF support
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromGif($filename) {
	if (!function_exists('imageCreateFromGif')) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION),
			 null);
	}
	$res = @imageCreateFromGif($filename);
	list ($ret, $resource) = $this->checkResource($res, $filename);
	if ($ret) {
	    return array($ret, null);
	}

	return array(null, $resource);
    }

    /**
     * Create a new image from JPEG file or URL.
     *
     * Requires:
     * - any PHP >= 3.0.16
     * - GD >= 1.8 (for JPEG support)
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromJpeg($filename) {
	$res = @imageCreateFromJpeg($filename);
	return $this->checkResource($res, $filename);
    }

    /**
     * Create a new image from PNG file or URL.
     *
     * Requires:
     * - any PHP >= 3.0.13
     * - GD (with PNG support)
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromPng($filename) {
	$res = @imageCreateFromPng($filename);
	return $this->checkResource($res, $filename);
    }

    /**
     * Create a new image from WBMP (wap-bmp) file or URL.
     *
     * Requires:
     * - PHP 4 >= 4.0.1 or PHP 5
     * - GD >= 1.8 (for WBMP support)
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromWbmp($filename) {
	$res = @imageCreateFromWbmp($filename);
	return $this->checkResource($res, $filename);
    }

    /**
     * Create a new image from XBM file or URL.
     *
     * Requires:
     * - PHP 4 >= 4.0.1 or PHP 5
     * - GD
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromXbm($filename) {
	$res = @imageCreateFromXbm($filename);
	return $this->checkResource($res, $filename);
    }

    /**
     * Create a new image from XPM file or URL.
     *
     * Requires:
     * - PHP 4 >= 4.0.1 or PHP 5
     * - GD
     *
     * @return array object GalleryStatus
     *	       res GD-resource
     */
    function imageCreateFromXpm($filename) {
	$res = @imageCreateFromXpm($filename);
	return $this->checkResource($res, $filename);
    }

    /**
     * Output GIF image to file.
     *
     * Requires:
     * - any PHP
     * - GD with GIF support
     *
     * @return object GalleryStatus
     */
    function imageGif($res, $filename) {
	if (!function_exists('imageGif')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imageGif($res, $filename);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Output JPEG image to file.
     *
     * Requires:
     * - any PHP >= 3.0.16
     * - GD >= 1.8
     *
     * @return object GalleryStatus
     */
    function imageJpeg($res, $filename, $quality) {
	if (!function_exists('imageJpeg')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imageJpeg($res, $filename, $quality);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Output PNG image to file.
     *
     * Requires:
     * - any PHP 3>= 3.0.13
     * - GD
     *
     * @return object GalleryStatus
     */
    function imagePng($res, $filename) {
	if (!function_exists('imagePng')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imagePng($res, $filename);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Output WBMP image to file.
     *
     * Requires:
     * - PHP 3>= 3.0.15 or PHP 4 >= 4.0.1 or PHP 5
     * - GD >= 1.8 (for WBMP support)
     *
     * @return object GalleryStatus
     */
    function imageWbmp($res, $filename) {
	if (!function_exists('imageWbmp')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imageWbmp($res, $filename);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Output XBM image to file.
     *
     * Requires:
     * - PHP 5
     *
     * @return object GalleryStatus
     */
    function imageXbm($res, $filename) {
	if (!function_exists('imageXbm')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imageXbm($res, $filename);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Copy and resize part of an image with resampling
     *
     * Requires:
     * - PHP 4 >= 4.0.6 or PHP 5
     * - GD >= 2.0.1
     *
     * @return object GalleryStatus
     */
    function imageCopyResampled(
	    $dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) {
	if (!function_exists('imageCopyResampled')) {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION);
	}
	$result = @imageCopyResampled($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY,
				      $dstW, $dstH, $srcW, $srcH);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Copy and resize part of an image
     *
     * Requires:
     * - any PHP
     *
     * @return object GalleryStatus
     */
    function imageCopyResized($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY,
			      $dstW, $dstH, $srcW, $srcH) {
	$result = @imageCopyResized($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY,
				    $dstW, $dstH, $srcW, $srcH);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Copy and merge part of an image
     *
     * Requires:
     * - PHP 4 >= 4.0.1 or PHP 5
     *
     * @return object GalleryStatus
     */
    function imageCopyMerge($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH, $pct) {
	$result = @imageCopyMerge($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH, $pct);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Copy part of an image
     *
     * Requires:
     * - any PHP >= 3.0.6
     *
     * @return object GalleryStatus
     */
    function imageCopy($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH) {
	$result = @imageCopy($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH);
	if ($result) {
	    return null;
	} else {
	    return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
	}
    }

    /**
     * Get image width
     *
     * @return int the width of the image
     */
    function imageSx($res) {
	return array(null, @imageSx($res));
    }

    /**
     * Get image height
     *
     * @return int the height of the image
     */
    function imageSy($res) {
	return array(null, @imageSy($res));
    }

    /**
     * Copy and merge part of an image
     *
     * Requires:
     * - PHP 4 >= 4.3.0 or PHP 5
     *
     * @return array object GalleryStatus
     *	       resource new gd resource
     */
    function imageRotate($srcRes, $angle, $bgdColor) {
	if (!function_exists('imageRotate')) {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE | ERROR_UNSUPPORTED_OPERATION),
			 null);
	}
	$res = @imageRotate($srcRes, $angle, $bgdColor);
	if ($res) {
	    return array(null, $res);
	} else {
	    return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
	}
    }

    /**
     * Destroy an image
     * @param object $res image resource
     */
    function imagedestroy($res) {
	@imagedestroy($res);
    }
}
?>