模块

Image_GD
继承自 Image

使用 GD 对图像操作提供支持。

package
BootPHP/图像
category
驱动
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 MODPATH/image/classes/image/gd.php 第 13 行声明。

常量

NONE

integer 1

WIDTH

integer 2

HEIGHT

integer 3

AUTO

integer 4

INVERSE

integer 5

HORIZONTAL

integer 17

VERTICAL

integer 18

属性

public static string $default_driver

默认驱动:GD、ImageMagick 等

string(2) "GD"

public string $file

图像文件路径

public integer $height

图像高度

public string $mime

图像的 MIME 类型

public integer $type

IMAGETYPE_* 常量之一

public integer $width

图像宽度

protected static $_bundled

NULL

protected static $_checked

bool false

protected $_create_function

protected $_image

方法

public __construct( ) (在 Image_GD 中定义)

Runs Image_GD::check and loads the image.

Tags

返回值

  • void

源代码

public function __construct($file)
{
	if (!Image_GD::$_checked)
	{
		// Run the install check
		Image_GD::check();
	}
	parent::__construct($file);
	// Set the image creation function name
	switch ($this->type)
	{
		case IMAGETYPE_JPEG:
			$create = 'imagecreatefromjpeg';
			break;
		case IMAGETYPE_GIF:
			$create = 'imagecreatefromgif';
			break;
		case IMAGETYPE_PNG:
			$create = 'imagecreatefrompng';
			break;
	}
	if (!isset($create) || !function_exists($create))
	{
		throw new BootPHP_Exception('Installed GD does not support :type images', array(':type' => image_type_to_extension($this->type, false)));
	}
	// Save function for future use
	$this->_create_function = $create;
	// Save filename for lazy loading
	$this->_image = $this->file;
}

public __destruct( ) (在 Image_GD 中定义)

Destroys the loaded image to free up resources.

返回值

  • void

源代码

public function __destruct()
{
	if (is_resource($this->_image))
	{
		// Free all resources
		imagedestroy($this->_image);
	}
}

public static check( ) (在 Image_GD 中定义)

Checks if GD is enabled and bundled. Bundled GD is required for some methods to work. Exceptions will be thrown from those methods when GD is not bundled.

返回值

  • boolean

源代码

public static function check()
{
	if (!function_exists('gd_info'))
	{
		throw new BootPHP_Exception('GD is either not installed or not enabled, check your configuration');
	}
	if (defined('GD_BUNDLED'))
	{
		// Get the version via a constant, available in PHP 5.
		Image_GD::$_bundled = GD_BUNDLED;
	}
	else
	{
		// Get the version information
		$info = gd_info();
		// Extract the bundled status
		Image_GD::$_bundled = (bool) preg_match('/\bbundled\b/i', $info['GD Version']);
	}
	if (defined('GD_VERSION'))
	{
		// Get the version via a constant, available in PHP 5.2.4+
		$version = GD_VERSION;
	}
	else
	{
		// Get the version information
		$info = gd_info();
		// Extract the version number
		preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches);
		// Get the major version
		$version = $matches[0];
	}
	if (!version_compare($version, '2.0.1', '>='))
	{
		throw new BootPHP_Exception('Image_GD requires GD version :required or greater, you have :version', array('required' => '2.0.1', ':version' => $version));
	}
	return Image_GD::$_checked = true;
}

public __toString( ) (在 Image 中定义)

渲染当前图像。 [!!] The output of this function is binary and must be rendered with the appropriate Content-Type header or it will not be displayed correctly!

返回值

  • string

源代码

public function __toString()
{
	try
	{
		// 渲染当前图像
		return $this->render();
	}
	catch (Exception $e)
	{
		if (is_object(BootPHP::$log))
		{
			// Get the text of the exception
			$error = BootPHP_Exception::text($e);
			// Add this exception to the log
			BootPHP::$log->add(Log::ERROR, $error);
		}
		// Showing any kind of error will be "inside" image data
		return '';
	}
}

public background( string $color [, integer $opacity = integer 100 ] ) (在 Image 中定义)

设置图像的背景色。这只适用于带 Alpha 透明度的图像。

// 使图像背景为黑色
$image->background('#000');

// 使图像背景为带有 50% 不透明度的黑色
$image->background('#000', 50);

参数

  • string $color required - 十六进制颜色值
  • integer $opacity = integer 100 - 背景不透明度:0-100

Tags

返回值

  • $this

源代码

public function background($color, $opacity = 100)
{
	if ($color[0] === '#')
	{
		$color = substr($color, 1);
	}
	if (strlen($color) === 3)
	{
		// 将十六进制的简写形式转换为普通写法
		$color = preg_replace('/./', '$0$0', $color);
	}
	// 将十六进制转换成 RGB 值
	list($r, $g, $b) = array_map('hexdec', str_split($color, 2));
	// 不透明度必须在 0 到 100 的范围内
	$opacity = min(max($opacity, 0), 100);
	$this->_do_background($r, $g, $b, $opacity);
	return $this;
}

public crop( integer $width , integer $height [, mixed $offset_x = NULL , mixed $offset_y = NULL ] ) (在 Image 中定义)

裁剪指定大小的图像。宽度或高度可以省略,这时使用当前宽度或高度。

如果没有指定偏移量,将使用轴的中心。 如果指定了偏移量为 true,将使用轴的底部。

// 从中间裁剪 200x200 像素的图像
$image->crop(200, 200);

参数

  • integer $width required - 新的宽度
  • integer $height required - 新的高度
  • mixed $offset_x = NULL - 从左偏移
  • mixed $offset_y = NULL - 从上偏移

Tags

返回值

  • $this

源代码

public function crop($width, $height, $offset_x = NULL, $offset_y = NULL)
{
	if ($width > $this->width)
	{
		// 使用当前宽度
		$width = $this->width;
	}
	if ($height > $this->height)
	{
		// 使用当前高度
		$height = $this->height;
	}
	if ($offset_x === NULL)
	{
		// 居中 X 偏移
		$offset_x = round(($this->width - $width) / 2);
	}
	elseif ($offset_x === true)
	{
		// 置底 X 偏移
		$offset_x = $this->width - $width;
	}
	elseif ($offset_x < 0)
	{
		// 从右侧设置 X 偏移
		$offset_x = $this->width - $width + $offset_x;
	}
	if ($offset_y === NULL)
	{
		// 居中 Y 偏移
		$offset_y = round(($this->height - $height) / 2);
	}
	elseif ($offset_y === true)
	{
		// 置底 Y 偏移
		$offset_y = $this->height - $height;
	}
	elseif ($offset_y < 0)
	{
		// 从底部设置 Y 偏移
		$offset_y = $this->height - $height + $offset_y;
	}
	// 判断最大可用宽度与高度
	$max_width = $this->width - $offset_x;
	$max_height = $this->height - $offset_y;
	if ($width > $max_width)
	{
		// 使用最大可用宽度
		$width = $max_width;
	}
	if ($height > $max_height)
	{
		// 使用最大可用高度
		$height = $max_height;
	}
	$this->_do_crop($width, $height, $offset_x, $offset_y);
	return $this;
}

public static factory( string $file [, string $driver = NULL ] ) (在 Image 中定义)

加载图像,并准备对其操作。

$image = Image::factory('upload/test.jpg');

参数

  • string $file required - 图像文件路径
  • string $driver = NULL - 驱动类型:GD、ImageMagick 等

Tags

返回值

  • Image

源代码

public static function factory($file, $driver = NULL)
{
	if ($driver === NULL)
	{
		// 使用默认驱动
		$driver = Image::$default_driver;
	}
	// 设置类名
	$class = 'Image_' . $driver;
	return new $class($file);
}

public flip( integer $direction ) (在 Image 中定义)

Flip the image along the horizontal or vertical axis.

// Flip the image from top to bottom
$image->flip(Image::HORIZONTAL);

// Flip the image from left to right
$image->flip(Image::VERTICAL);

参数

  • integer $direction required - 方向:Image::HORIZONTAL、Image::VERTICAL

Tags

返回值

  • $this

源代码

public function flip($direction)
{
	if ($direction !== Image::HORIZONTAL)
	{
		// 垂直翻转
		$direction = Image::VERTICAL;
	}
	$this->_do_flip($direction);
	return $this;
}

public reflection( [ integer $height = NULL , integer $opacity = integer 100 , boolean $fade_in = bool false ] ) (在 Image 中定义)

为图像添加倒影。倒影的最不透明的部分等于不透明度设置,然后淡出为完全透明。 Alpha 透明度将被保留。

// 创建一个 50 像素的倒影,不透明度从 0 到 100% 渐变
$image->reflection(50);

// 创建一个 50 像素的倒影,不透明度从 100% 到 0 渐变
$image->reflection(50, 100, true);

// 创建一个 50 像素的倒影,不透明度从 0 到 60% 渐变
$image->reflection(50, 60, true);

注意:默认情况下,倒影会从顶部透明到底部不透明。

参数

  • integer $height = NULL - 倒影高度
  • integer $opacity = integer 100 - 倒影不透明度:0-100
  • boolean $fade_in = bool false - True 为淡入,false 为淡出

Tags

返回值

  • $this

源代码

public function reflection($height = NULL, $opacity = 100, $fade_in = false)
{
	if ($height === NULL || $height > $this->height)
	{
		// Use the current height
		$height = $this->height;
	}
	// The opacity must be in the range of 0 to 100
	$opacity = min(max($opacity, 0), 100);
	$this->_do_reflection($height, $opacity, $fade_in);
	return $this;
}

public render( [ string $type = NULL , integer $quality = integer 100 ] ) (在 Image 中定义)

渲染图像并返回二进制数据。

// 以 50% 的质量渲染图像
$data = $image->render(NULL, 50);

// 渲染图像为 PNG
$data = $image->render('png');

参数

  • string $type = NULL - 返回的图像格式:png、jpg、gif 等
  • integer $quality = integer 100 - 图像质量:1-100

Tags

返回值

  • string

源代码

public function render($type = NULL, $quality = 100)
{
	if ($type === NULL)
	{
		// 使用当前图像类型
		$type = image_type_to_extension($this->type, false);
	}
	return $this->_do_render($type, $quality);
}

public resize( [ integer $width = NULL , integer $height = NULL , integer $master = NULL ] ) (在 Image 中定义)

调整图像尺寸到指定大小。按比例调整大小,宽度或高度可以省略。

// 将长边调整为 200 像素,保持宽高比(大图调小后较小)
$image->resize(200, 200);

// 将短边调整为 200 像素,保持宽高比(大图调小后较大)
$image->resize(200, 200, Image::INVERSE);

// 调整为 500 像素的宽度,保持宽高比
$image->resize(500, NULL);

// 调整为 500 像素的高度,保持宽高比
$image->resize(NULL, 500);

// 调整为 200x500 像素,忽略宽高比
$image->resize(200, 500, Image::NONE);

参数

  • integer $width = NULL - 新的宽度
  • integer $height = NULL - 新的高度
  • integer $master = NULL - 主尺寸

Tags

返回值

  • $this

源代码

public function resize($width = NULL, $height = NULL, $master = NULL)
{
	if ($master === NULL)
	{
		// 自动选择主尺寸
		$master = Image::AUTO;
	}
	// Image::WIDTH and Image::HEIGHT deprecated. You can use it in old projects,
	// but in new you must pass empty value for non-master dimension
	elseif ($master == Image::WIDTH && !empty($width))
	{
		$master = Image::AUTO;
		// Set empty height for backward compatibility
		$height = NULL;
	}
	elseif ($master == Image::HEIGHT && !empty($height))
	{
		$master = Image::AUTO;
		// Set empty width for backward compatibility
		$width = NULL;
	}
	if (empty($width))
	{
		if ($master === Image::NONE)
		{
			// Use the current width
			$width = $this->width;
		}
		else
		{
			// If width not set, master will be height
			$master = Image::HEIGHT;
		}
	}
	if (empty($height))
	{
		if ($master === Image::NONE)
		{
			// Use the current height
			$height = $this->height;
		}
		else
		{
			// If height not set, master will be width
			$master = Image::WIDTH;
		}
	}
	switch ($master)
	{
		case Image::AUTO:
			// 用最大缩小比率选择方向
			$master = ($this->width / $width) > ($this->height / $height) ? Image::WIDTH : Image::HEIGHT;
			break;
		case Image::INVERSE:
			// 用最小缩小比率选择方向
			$master = ($this->width / $width) > ($this->height / $height) ? Image::HEIGHT : Image::WIDTH;
			break;
	}
	switch ($master)
	{
		case Image::WIDTH:
			// Recalculate the height based on the width proportions
			$height = $this->height * $width / $this->width;
			break;
		case Image::HEIGHT:
			// Recalculate the width based on the height proportions
			$width = $this->width * $height / $this->height;
			break;
	}
	// Convert the width and height to integers, minimum value is 1px
	$width = max(round($width), 1);
	$height = max(round($height), 1);
	$this->_do_resize($width, $height);
	return $this;
}

public rotate( integer $degrees ) (在 Image 中定义)

Rotate the image by a given amount.

// Rotate 45 degrees clockwise
$image->rotate(45);

// Rotate 90% counter-clockwise
$image->rotate(-90);

参数

  • integer $degrees required - Degrees to rotate: -360-360

Tags

返回值

  • $this

源代码

public function rotate($degrees)
{
	// Make the degrees an integer
	$degrees = (int) $degrees;
	if ($degrees > 180)
	{
		do
		{
			// Keep subtracting full circles until the degrees have normalized
			$degrees -= 360;
		}
		while ($degrees > 180);
	}
	if ($degrees < -180)
	{
		do
		{
			// Keep adding full circles until the degrees have normalized
			$degrees += 360;
		}
		while ($degrees < -180);
	}
	$this->_do_rotate($degrees);
	return $this;
}

public save( [ string $file = NULL , integer $quality = integer 100 ] ) (在 Image 中定义)

保存图像。如果省略文件名,则原始图像将被覆盖。

// 保存图像为 PNG
$image->save('uploads/fox.png');

// 覆盖原始图像
$image->save();

注意:如果文件存在,但是不可写,将抛出异常。 注意:如果文件不存在,目录不可写,将派出异常。

参数

  • string $file = NULL - New image path
  • integer $quality = integer 100 - Quality of image: 1-100

Tags

返回值

  • boolean

源代码

public function save($file = NULL, $quality = 100)
{
	if ($file === NULL)
	{
		// 覆盖文件
		$file = $this->file;
	}
	if (is_file($file))
	{
		if (!is_writable($file))
		{
			throw new BootPHP_Exception('File must be writable: :file', array(':file' => Debug::path($file)));
		}
	}
	else
	{
		// 获得文件目录
		$directory = realpath(pathinfo($file, PATHINFO_DIRNAME));
		if (!is_dir($directory) || !is_writable($directory))
		{
			throw new BootPHP_Exception('Directory must be writable: :directory', array(':directory' => Debug::path($directory)));
		}
	}
	// 质量必须在 1 到 100 的范围内
	$quality = min(max($quality, 1), 100);
	return $this->_do_save($file, $quality);
}

public sharpen( integer $amount ) (在 Image 中定义)

用指定的数量锐化图像。

// 锐化图像 20%
$image->sharpen(20);

参数

  • integer $amount required - 锐化数量:1-100

Tags

返回值

  • $this

源代码

public function sharpen($amount)
{
	// The amount must be in the range of 1 to 100
	$amount = min(max($amount, 1), 100);
	$this->_do_sharpen($amount);
	return $this;
}

public watermark( object $watermark [, integer $offset_x = NULL , integer $offset_y = NULL , integer $opacity = integer 100 ] ) (在 Image 中定义)

为图像添加指定不透明度的水印。Alpha 透明度将被保留。

如果没有指定偏移量,则使用轴的中心。 如果指定偏移量为 true,则使用轴的底部。

// 将水印添加到图像的右下角
$mark = Image::factory('upload/watermark.png');
$image->watermark($mark, true, true);

参数

  • object $watermark required - 水印图像实例
  • integer $offset_x = NULL - 左偏移量
  • integer $offset_y = NULL - 上偏移量
  • integer $opacity = integer 100 - 水印的不透明度:1-100

Tags

返回值

  • $this

源代码

public function watermark(Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity = 100)
{
	if ($offset_x === NULL)
	{
		// 居中 X 偏移
		$offset_x = round(($this->width - $watermark->width) / 2);
	}
	elseif ($offset_x === true)
	{
		// 置底 X 偏移
		$offset_x = $this->width - $watermark->width;
	}
	elseif ($offset_x < 0)
	{
		// 从右侧设置 X 偏移
		$offset_x = $this->width - $watermark->width + $offset_x;
	}
	if ($offset_y === NULL)
	{
		// 居中 Y 偏移
		$offset_y = round(($this->height - $watermark->height) / 2);
	}
	elseif ($offset_y === true)
	{
		// 置底 Y 偏移
		$offset_y = $this->height - $watermark->height;
	}
	elseif ($offset_y < 0)
	{
		// 从底部设置 Y 偏移
		$offset_y = $this->height - $watermark->height + $offset_y;
	}
	// 不透明度必须在 1 到 100 的范围内
	$opacity = min(max($opacity, 1), 100);
	$this->_do_watermark($watermark, $offset_x, $offset_y, $opacity);
	return $this;
}

protected _create( integer $width , integer $height ) (在 Image_GD 中定义)

Create an empty image with the given width and height.

参数

  • integer $width required - Image width
  • integer $height required - Image height

返回值

  • resource

源代码

protected function _create($width, $height)
{
	// Create an empty image
	$image = imagecreatetruecolor($width, $height);
	// Do not apply alpha blending
	imagealphablending($image, false);
	// Save alpha levels
	imagesavealpha($image, true);
	return $image;
}

protected _do_background( integer $r , integer $g , integer $b , integer $opacity ) (在 Image_GD 中定义)

执行背景色。

参数

  • integer $r required - 红
  • integer $g required - 绿
  • integer $b required - 蓝
  • integer $opacity required - 不透明度

返回值

  • void

源代码

protected function _do_background($r, $g, $b, $opacity)
{
	// Loads image if not yet loaded
	$this->_load_image();
	// Convert an opacity range of 0-100 to 127-0
	$opacity = round(abs(($opacity * 127 / 100) - 127));
	// Create a new background
	$background = $this->_create($this->width, $this->height);
	// Allocate the color
	$color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
	// Fill the image with white
	imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
	// Alpha blending must be enabled on the background!
	imagealphablending($background, true);
	// Copy the image onto a white background to remove all transparency
	if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
	{
		// Swap the new image for the old one
		imagedestroy($this->_image);
		$this->_image = $background;
	}
}

protected _do_crop( integer $width , integer $height , integer $offset_x , integer $offset_y ) (在 Image_GD 中定义)

执行剪切。

参数

  • integer $width required - 新宽度
  • integer $height required - 新高度
  • integer $offset_x required - 左偏移量
  • integer $offset_y required - 上偏移量

返回值

  • void

源代码

protected function _do_crop($width, $height, $offset_x, $offset_y)
{
	// Create the temporary image to copy to
	$image = $this->_create($width, $height);
	// Loads image if not yet loaded
	$this->_load_image();
	// Execute the crop
	if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
	{
		// Swap the new image for the old one
		imagedestroy($this->_image);
		$this->_image = $image;
		// Reset the width and height
		$this->width = imagesx($image);
		$this->height = imagesy($image);
	}
}

protected _do_flip( integer $direction ) (在 Image_GD 中定义)

执行翻转。

参数

  • integer $direction required - 翻转方向

返回值

  • void

源代码

protected function _do_flip($direction)
{
	// Create the flipped image
	$flipped = $this->_create($this->width, $this->height);
	// Loads image if not yet loaded
	$this->_load_image();
	if ($direction === Image::HORIZONTAL)
	{
		for ($x = 0; $x < $this->width; $x++)
		{
			// Flip each row from top to bottom
			imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
		}
	}
	else
	{
		for ($y = 0; $y < $this->height; $y++)
		{
			// Flip each column from left to right
			imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
		}
	}
	// Swap the new image for the old one
	imagedestroy($this->_image);
	$this->_image = $flipped;
	// Reset the width and height
	$this->width = imagesx($flipped);
	$this->height = imagesy($flipped);
}

protected _do_reflection( integer $height , integer $opacity , boolean $fade_in ) (在 Image_GD 中定义)

执行倒影。

参数

  • integer $height required - 倒影高度
  • integer $opacity required - 倒影不透明度
  • boolean $fade_in required - True 为淡出,false 淡入

返回值

  • void

源代码

protected function _do_reflection($height, $opacity, $fade_in)
{
	if (!Image_GD::$_bundled)
	{
		throw new BootPHP_Exception('This method requires :function, which is only available in the bundled version of GD', array(':function' => 'imagefilter'));
	}
	// Loads image if not yet loaded
	$this->_load_image();
	// Convert an opacity range of 0-100 to 127-0
	$opacity = round(abs(($opacity * 127 / 100) - 127));
	if ($opacity < 127)
	{
		// Calculate the opacity stepping
		$stepping = (127 - $opacity) / $height;
	}
	else
	{
		// Avoid a "divide by zero" error
		$stepping = 127 / $height;
	}
	// Create the reflection image
	$reflection = $this->_create($this->width, $this->height + $height);
	// Copy the image to the reflection
	imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height);
	for ($offset = 0; $height >= $offset; $offset++)
	{
		// Read the next line down
		$src_y = $this->height - $offset - 1;
		// Place the line at the bottom of the reflection
		$dst_y = $this->height + $offset;
		if ($fade_in === true)
		{
			// Start with the most transparent line first
			$dst_opacity = round($opacity + ($stepping * ($height - $offset)));
		}
		else
		{
			// Start with the most opaque line first
			$dst_opacity = round($opacity + ($stepping * $offset));
		}
		// Create a single line of the image
		$line = $this->_create($this->width, 1);
		// Copy a single line from the current image into the line
		imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1);
		// Colorize the line to add the correct alpha level
		imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity);
		// Copy a the line into the reflection
		imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1);
	}
	// Swap the new image for the old one
	imagedestroy($this->_image);
	$this->_image = $reflection;
	// Reset the width and height
	$this->width = imagesx($reflection);
	$this->height = imagesy($reflection);
}

protected _do_render( string $type , integer $quality ) (在 Image_GD 中定义)

执行渲染。

参数

  • string $type required - 图像格式:png、jpg、gif 等
  • integer $quality required - 质量

返回值

  • string

源代码

protected function _do_render($type, $quality)
{
	// Loads image if not yet loaded
	$this->_load_image();
	// Get the save function and IMAGETYPE
	list($save, $type) = $this->_save_function($type, $quality);
	// Capture the output
	ob_start();
	// Render the image
	$status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
	if ($status === true && $type !== $this->type)
	{
		// Reset the image type and mime type
		$this->type = $type;
		$this->mime = image_type_to_mime_type($type);
	}
	return ob_get_clean();
}

protected _do_resize( integer $width , integer $height ) (在 Image_GD 中定义)

执行调整大小。

参数

  • integer $width required - 新宽度
  • integer $height required - 新高度

返回值

  • void

源代码

protected function _do_resize($width, $height)
{
	// Presize width and height
	$pre_width = $this->width;
	$pre_height = $this->height;
	// Loads image if not yet loaded
	$this->_load_image();
	// Test if we can do a resize without resampling to speed up the final resize
	if ($width > ($this->width / 2) && $height > ($this->height / 2))
	{
		// The maximum reduction is 10% greater than the final size
		$reduction_width = round($width * 1.1);
		$reduction_height = round($height * 1.1);
		while ($pre_width / 2 > $reduction_width && $pre_height / 2 > $reduction_height)
		{
			// Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
			$pre_width /= 2;
			$pre_height /= 2;
		}
		// Create the temporary image to copy to
		$image = $this->_create($pre_width, $pre_height);
		if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height))
		{
			// Swap the new image for the old one
			imagedestroy($this->_image);
			$this->_image = $image;
		}
	}
	// Create the temporary image to copy to
	$image = $this->_create($width, $height);
	// Execute the resize
	if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
	{
		// Swap the new image for the old one
		imagedestroy($this->_image);
		$this->_image = $image;
		// Reset the width and height
		$this->width = imagesx($image);
		$this->height = imagesy($image);
	}
}

protected _do_rotate( integer $degrees ) (在 Image_GD 中定义)

执行旋转。

参数

  • integer $degrees required - 旋转角度

返回值

  • void

源代码

protected function _do_rotate($degrees)
{
	if (!Image_GD::$_bundled)
	{
		throw new BootPHP_Exception('This method requires :function, which is only available in the bundled version of GD', array(':function' => 'imagerotate'));
	}
	// Loads image if not yet loaded
	$this->_load_image();
	// Transparent black will be used as the background for the uncovered region
	$transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);
	// Rotate, setting the transparent color
	$image = imagerotate($this->_image, 360 - $degrees, $transparent, 1);
	// Save the alpha of the rotated image
	imagesavealpha($image, true);
	// Get the width and height of the rotated image
	$width = imagesx($image);
	$height = imagesy($image);
	if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100))
	{
		// Swap the new image for the old one
		imagedestroy($this->_image);
		$this->_image = $image;
		// Reset the width and height
		$this->width = $width;
		$this->height = $height;
	}
}

protected _do_save( string $file , integer $quality ) (在 Image_GD 中定义)

执行保存。

参数

  • string $file required - 新图像文件名
  • integer $quality required - 质量

返回值

  • boolean

源代码

protected function _do_save($file, $quality)
{
	// Loads image if not yet loaded
	$this->_load_image();
	// Get the extension of the file
	$extension = pathinfo($file, PATHINFO_EXTENSION);
	// Get the save function and IMAGETYPE
	list($save, $type) = $this->_save_function($extension, $quality);
	// Save the image to a file
	$status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
	if ($status === true && $type !== $this->type)
	{
		// Reset the image type and mime type
		$this->type = $type;
		$this->mime = image_type_to_mime_type($type);
	}
	return true;
}

protected _do_sharpen( integer $amount ) (在 Image_GD 中定义)

执行锐化。

参数

  • integer $amount required - 锐化数量

返回值

  • void

源代码

protected function _do_sharpen($amount)
{
	if (!Image_GD::$_bundled)
	{
		throw new BootPHP_Exception('This method requires :function, which is only available in the bundled version of GD', array(':function' => 'imageconvolution'));
	}
	// Loads image if not yet loaded
	$this->_load_image();
	// Amount should be in the range of 18-10
	$amount = round(abs(-18 + ($amount * 0.08)), 2);
	// Gaussian blur matrix
	$matrix = array
		(
		array(-1, -1, -1),
		array(-1, $amount, -1),
		array(-1, -1, -1),
	);
	// Perform the sharpen
	if (imageconvolution($this->_image, $matrix, $amount - 8, 0))
	{
		// Reset the width and height
		$this->width = imagesx($this->_image);
		$this->height = imagesy($this->_image);
	}
}

protected _do_watermark( object $watermark , integer $offset_x , integer $offset_y , integer $opacity ) (在 Image_GD 中定义)

执行水印。

参数

  • object $watermark required - 要水印的图像
  • integer $offset_x required - 左偏移量
  • integer $offset_y required - 上偏移量
  • integer $opacity required - 水印的不透明度

返回值

  • void

源代码

protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
{
	if (!Image_GD::$_bundled)
	{
		throw new BootPHP_Exception('This method requires :function, which is only available in the bundled version of GD', array(':function' => 'imagelayereffect'));
	}
	// Loads image if not yet loaded
	$this->_load_image();
	// Create the watermark image resource
	$overlay = imagecreatefromstring($watermark->render());
	imagesavealpha($overlay, true);
	// Get the width and height of the watermark
	$width = imagesx($overlay);
	$height = imagesy($overlay);
	if ($opacity < 100)
	{
		// Convert an opacity range of 0-100 to 127-0
		$opacity = round(abs(($opacity * 127 / 100) - 127));
		// Allocate transparent gray
		$color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity);
		// The transparent image will overlay the watermark
		imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
		// Fill the background with the transparent color
		imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
	}
	// Alpha blending must be enabled on the background!
	imagealphablending($this->_image, true);
	if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
	{
		// Destroy the overlay image
		imagedestroy($overlay);
	}
}

protected _load_image( ) (在 Image_GD 中定义)

Loads an image into GD.

返回值

  • void

源代码

protected function _load_image()
{
	if (!is_resource($this->_image))
	{
		// Gets create function
		$create = $this->_create_function;
		// Open the temporary image
		$this->_image = $create($this->file);
		// Preserve transparency when saving
		imagesavealpha($this->_image, true);
	}
}

protected _save_function( string $extension , integer & $quality ) (在 Image_GD 中定义)

Get the GD saving function and image type for this extension. Also normalizes the quality setting

参数

  • string $extension required - Image type: png, jpg, etc
  • byref integer $quality required - Image quality

Tags

返回值

  • array - Save function, IMAGETYPE_* constant

源代码

protected function _save_function($extension, & $quality)
{
	if (!$extension)
	{
		// Use the current image type
		$extension = image_type_to_extension($this->type, false);
	}
	switch (strtolower($extension))
	{
		case 'jpg':
		case 'jpeg':
			// Save a JPG file
			$save = 'imagejpeg';
			$type = IMAGETYPE_JPEG;
			break;
		case 'gif':
			// Save a GIF file
			$save = 'imagegif';
			$type = IMAGETYPE_GIF;
			// GIFs do not a quality setting
			$quality = NULL;
			break;
		case 'png':
			// Save a PNG file
			$save = 'imagepng';
			$type = IMAGETYPE_PNG;
			// Use a compression level of 9 (does not affect quality!)
			$quality = 9;
			break;
		default:
			throw new BootPHP_Exception('Installed GD does not support :type images', array(':type' => $extension));
			break;
	}
	return array($save, $type);
}