Skip to content

Commit

Permalink
Allow Image_GD to be used with the non-bundled version of GD, throw e…
Browse files Browse the repository at this point in the history
…xceptions in specific methods that use bundled GD functions, fixes #2373
  • Loading branch information
Woody Gilk committed Nov 24, 2009
1 parent 97e8753 commit 37f0bf7
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions classes/kohana/image/gd.php
Expand Up @@ -9,6 +9,9 @@
*/
class Kohana_Image_GD extends Image {

// Is GD bundled or separate?
protected static $_bundled;

public static function check()
{
if ( ! function_exists('gd_info'))
Expand All @@ -19,20 +22,15 @@ public static function check()
if (defined('GD_BUNDLED'))
{
// Get the version via a constant, available in PHP 5.
$bundled = GD_BUNDLED;
Image_GD::$_bundled = GD_BUNDLED;
}
else
{
// Get the version information
$bundled = current(gd_info());
$info = gd_info();

// Extract the bundled status
$bundled = (bool) preg_match('/\bbundled\b/i', $bundled);
}

if ( ! $bundled)
{
throw new Kohana_Exception('Image_GD requires GD to be bundled with PHP');
Image_GD::$_bundled = (bool) preg_match('/\bbundled\b/i', $info['GD Version']);
}

if (defined('GD_VERSION'))
Expand All @@ -43,19 +41,19 @@ public static function check()
else
{
// Get the version information
$version = current(gd_info());
$info = gd_info();

// Extract the version number
preg_match('/\d+\.\d+(?:\.\d+)?/', $version, $matches);
preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches);

// Get the major version
$version = $matches[0];
}

if ( ! version_compare($version, '2.0', '>='))
if ( ! version_compare($version, '2.0.1', '>='))
{
throw new Kohana_Exception('Image_GD requires GD version 2.0 or greater, you have :version',
array(':version' => $version));
throw new Kohana_Exception('Image_GD requires GD version :required or greater, you have :version',
array('required' => '2.0.1', ':version' => $version));
}

return Image_GD::$_checked = TRUE;
Expand Down Expand Up @@ -177,6 +175,12 @@ protected function _do_crop($width, $height, $offset_x, $offset_y)

protected function _do_rotate($degrees)
{
if ( ! Image_GD::$_bundled)
{
throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
array(':function' => 'imagerotate'));
}

// Transparent black will be used as the background for the uncovered region
$transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);

Expand Down Expand Up @@ -235,6 +239,12 @@ protected function _do_flip($direction)

protected function _do_sharpen($amount)
{
if ( ! Image_GD::$_bundled)
{
throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
array(':function' => 'imageconvolution'));
}

// Amount should be in the range of 18-10
$amount = round(abs(-18 + ($amount * 0.08)), 2);

Expand All @@ -257,6 +267,12 @@ protected function _do_sharpen($amount)

protected function _do_reflection($height, $opacity, $fade_in)
{
if ( ! Image_GD::$_bundled)
{
throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
array(':function' => 'imagefilter'));
}

// Convert an opacity range of 0-100 to 127-0
$opacity = round(abs(($opacity * 127 / 100) - 127));

Expand Down Expand Up @@ -320,6 +336,12 @@ protected function _do_reflection($height, $opacity, $fade_in)

protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
{
if ( ! Image_GD::$_bundled)
{
throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
array(':function' => 'imagelayereffect'));
}

// Create the watermark image resource
$overlay = imagecreatefromstring($watermark->render());

Expand Down

0 comments on commit 37f0bf7

Please sign in to comment.