Файл: adultscript-2.0.3-pro/files/libraries/framework/image/gd.php
Строк: 234
<?php
defined('_VALID') or die('Restricted Access!');
class VImage_Driver_gd extends VImage
{
public $src = array();
private $dst = array();
private $wrk = array();
private $canvas = array();
private $options = array(
'jpeg_quality' => 75,
'preserve_alpha' => TRUE,
'alpha_mask_color' => array (255, 255, 255),
'preserve_transparency' => TRUE,
'transparency_mask_color' => array(0, 0, 0),
'watermark' => FALSE,
'watermark_img_path' => NULL,
);
private $error = false;
private $errors = array(
'image.src_not_found' => 'Image source file not found!',
'image.src_not_readable' => 'Image source file is not readable!',
'image.dst_dir_create_failed' => 'Failed to create image destination directory!',
'image.dst_dir_not_found' => 'Destination directory not found!',
'image.dst_dir_not_writable' => 'Image destination directory is not writable!',
'image.src_invalid' => 'Invalid image source file!',
'image.src_not_supported' => 'Source image file type is not supported!',
'image.src_identifier_failed' => 'Failed to create a image identifier from the source image!',
'image.dst_failed' => 'Failed to create processed image!',
'image.calculate_failed' => 'Invalid resize method specified!',
'image.dst_file_not_writable' => 'Destination file not writable!'
);
public function __construct()
{
parent::__construct();
$this->check();
}
public function __destruct()
{
if (isset($this->src['image']) && is_resource($this->src['image'])) {
imagedestroy($this->src['image']);
}
if (isset($this->wrk['image']) && is_resource($this->wrk['image'])) {
imagedestroy($this->wrk['image']);
}
}
public function load($src, $options=array())
{
if (!file_exists($src) OR !is_file($src)) {
$this->error = 'image.src_not_found';
}
if (!is_readable($src)) {
$this->error = 'image.src_not_readable';
}
if (!$this->error) {
$size = getimagesize($src);
if ($size) {
$this->src['path'] = $src;
$this->src['width'] = $size['0'];
$this->src['height'] = $size['1'];
$this->src['format'] = $size['2'];
switch ($this->src['format']) {
case 1:
$this->src['image'] = imagecreatefromgif($src);
$this->src['ext'] = 'gif';
break;
case 2:
$this->src['image'] = imagecreatefromjpeg($src);
$this->src['ext'] = 'jpg';
break;
case 3:
$this->src['image'] = imagecreatefrompng($src);
$this->src['ext'] = 'png';
break;
default:
$this->error['image.src_format_invalid'];
}
if (!$this->error) {
if (!isset($this->src['image']) OR !$this->src['image']) {
$this->error = 'image.create_failed';
}
}
} else {
$this->error = 'image.src_invalid';
}
}
return ($this->error !== FALSE) ? FALSE : TRUE;
}
public function resize($width, $height, $type='EXACT', $dst_path=FALSE, $clean=true)
{
if ($clean === true) {
unset($this->wrk['image']);
}
if ($this->calculate_image_size($width, $height, $type)) {
$pos_x = 0;
$pos_y = 0;
if (isset($this->wrk['image']) && $this->wrk['image']) {
if (isset($this->canvas['width']) && $this->canvas['width'] >= $this->wrk['width']) {
$pos_x = (int) (($this->canvas['width'] - $this->wrk['width']) / 2);
}
if (isset($this->canvas['height']) && $this->canvas['height'] >= $this->wrk['height']) {
$pos_y = (int) (($this->canvas['height'] - $this->wrk['height']) / 2);
}
}
if (!isset($this->wrk['image']) OR !$this->wrk['image']) {
$this->wrk['image'] = imagecreatetruecolor($this->wrk['width'], $this->wrk['height']);
$this->preserve();
}
imagecopyresampled($this->wrk['image'], $this->src['image'], $pos_x, $pos_y, 0, 0,
$this->wrk['width'], $this->wrk['height'], $this->src['width'], $this->src['height']);
if ($dst_path !== FALSE) {
return $this->save($dst_path);
}
} else {
$this->error = 'image.calculate_failed';
}
return ($this->error) ? FALSE : TRUE;
}
public function canvas($width, $height, $color, $type=NULL, $dst_path=FALSE)
{
$this->canvas['width'] = (int) $width;
$this->canvas['height'] = (int) $height;
$this->wrk['image'] = imagecreatetruecolor($this->canvas['width'], $this->canvas['height']);
$rgb = $this->hex2rgb($color);
$color = imagecolorallocate($this->wrk['image'], $rgb['0'], $rgb['1'], $rgb['2']);
imagefill($this->wrk['image'], 0, 0, $color);
if (!is_null($type) && $dst_path !== FALSE) {
return $this->resize($width, $height, $type, $dst_path);
}
return TRUE;
}
public function crop($crop_x, $crop_y, $crop_width, $crop_height, $dst_path=FALSE)
{
if (!isset($this->wrk['image'])) {
$this->wrk['image'] = imagecreatetruecolor($crop_width, $crop_height);
$this->preserve();
}
imagecopyresampled($this->wrk['image'], $this->src['image'], 0, 0,
$crop_x, $crop_y, $crop_width, $crop_height, $crop_width, $crop_height);
if ($dst_path !== FALSE) {
return $this->save($dst_path);
}
return true;
}
public function crop_from_center($crop_width, $crop_height=FALSE, $dst_path=FALSE)
{
$crop_width = (int) $crop_width;
if ($crop_height === FALSE) {
$crop_height = $crop_width;
}
$crop_width = ($this->src['width'] < $crop_width) ? $this->src['width'] : $crop_width;
$crop_height = ($this->src['height'] < $crop_height) ? $this->src['height'] : $crop_height;
$crop_x = (int) (($this->src['width'] - $crop_width) / 2);
$crop_y = (int) (($this->src['height'] - $crop_height) / 2);
return $this->crop($crop_x, $crop_y, $crop_width, $crop_height, $dst_path);
}
public function rotate($direction='CW')
{
if ($direction == 'CW') {
$this->rotate_degrees(90);
} else {
$this->rotate_degrees(-90);
}
}
public function rotate_degrees($degrees)
{
if (isset($this->wrk['image'])) {
throw new Exception('Application Error! Image rotate must be done before other operations!');
}
$image = imagerotate($this->src['image'], $degrees, 0);
$this->src['image'] = $image;
$this->src['width'] = $this->src['height'];
$this->src['height'] = $this->src['width'];
}
public function zoom($scale)
{
$width = $this->src['width']*$scale;
$height = $this->src['height']*$scale;
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $this->src['image'], 0, 0, 0, 0, $width, $height, $this->src['width'], $this->src['height']);
$this->src['image'] = $image;
$this->src['width'] = $width;
$this->src['height'] = $height;
return true;
}
public function watermark($dst_path=FALSE)
{
if ($this->options['watermark'] === TRUE) {
}
}
public function save($dst_path)
{
$dir = dirname($dst_path);
if (!file_exists($dir) OR !is_dir($dir)) {
if (!VFolder::create($dir, 0777)) {
$this->error = 'image.dst_dir_create_failed';
}
if (!file_exists($dir) OR !is_dir($dir)) {
$this->error = 'image.dst_dir_not_found';
}
}
if (!$this->error) {
if (!is_writable($dir)) {
$this->error = 'image.dst_dir_not_writable';
}
}
if (file_exists($dst_path) && is_file($dst_path) && !is_writable($dst_path)) {
$this->error = 'image.dst_file_not_writable';
}
if ($this->error) {
return FALSE;
}
$ext = VFile::ext($dst_path);
switch ($ext) {
case 'jpg':
case 'jpeg':
if (!imagejpeg($this->wrk['image'], $dst_path, $this->options['jpeg_quality'])) {
$this->error = 'image.save_failed';
}
break;
case 'png':
if (!imagepng($this->wrk['image'], $dst_path)) {
$this->error = 'image.save_failed';
}
break;
case 'gif':
if (!imagegif($this->wrk['image'], $dst_path)) {
$this->error = 'image.save_failed';
}
break;
default:
$this->error = 'image.dst_format_invalid';
}
return ($this->error) ? FALSE : TRUE;
}
/*
/ $this->set_image_size($percent, 0, 'PERCENT');
*/
private function calculate_image_size($width, $height, $type)
{
$this->dst['width'] = (int) $width;
$this->dst['height'] = (int) $height;
$this->dst['percent'] = $this->dst['width'];
switch (strtoupper($type)) {
case 'MAX_WIDTH':
$this->wrk['width'] = ($this->src['width'] > $this->dst['width']) ? $this->dst['width'] : $this->src['width'];
$this->wrk['height'] = (int) ($this->wrk['width'] * $this->src['height']) / $this->src['width'];
break;
case 'MAX_HEIGHT':
$this->wrk['height'] = ($this->src['height'] > $this->dst['height']) ? $this->dst['height'] : $this->src['height'];
$this->wrk['width'] = (int) ($this->wrk['height'] * $this->src['width']) / $this->src['height'];
break;
case 'ASPECT_RATIO':
if (($this->src['height'] / $this->dst['height']) >= ($this->src['width'] / $this->dst['width'])) {
$this->calculate_image_size($width, $height, 'MAX_HEIGHT');
} else {
$this->calculate_image_size($width, $height, 'MAX_WIDTH');
}
break;
case 'PERCENT':
$this->wrk['width'] = ceil(($this->src['width'] * $this->dst['percent']) / 100);
$this->wrk['height'] = ceil(($this->src['height'] * $this->dst['percent']) / 100);
break;
case 'TO_WIDTH':
$this->wrk['width'] = $this->dst['width'];
$this->wrk['height'] = (int) ($this->dst['width'] * $this->src['height']) / $this->src['width'];
break;
case 'TO_HEIGHT':
$this->wrk['height'] = $this->dst['height'];
$this->wrk['width'] = (int) (int) ($this->dst['height'] * $this->src['width']) / $this->src['height'];
break;
case 'TO_ASPECT_RATIO':
if (($this->src['height'] / $this->dst['height']) >= ($this->src['width'] / $this->dst['width'])) {
$this->calculate_image_size($width, $height, 'TO_HEIGHT');
} else {
$this->calculate_image_size($width, $height, 'TO_WIDTH');
}
break;
case 'EXACT':
$this->wrk['width'] = $this->dst['width'];
$this->wrk['height'] = $this->dst['height'];
break;
case 'SAME':
$this->wrk['width'] = $this->src['width'];
$this->wrk['height'] = $this->src['height'];
break;
default:
return FALSE;
}
return TRUE;
}
private function preserve()
{
if (!isset($this->wrk['image']) OR !$this->wrk['image']) {
throw new Exception('Application Error. Trying to preserve image properties without a identifier!');
}
if ((isset($this->src['format']) && $this->src['format'] == 'png') &&
$this->options['preserve_alpha'] === TRUE) {
imagealphablending($this->wrk['image'], false);
$color_transparent = imagecolorallocatealpha(
$this->wrk['image'],
$this->options['alpha_mask_color']['0'],
$this->options['alpha_mask_color']['1'],
$this->options['alpha_mask_color']['2'],
0
);
imagefill($this->wrk['image'], 0, 0, $color_transparent);
imagesavealpha($this->wrk['image'], true);
}
if ((isset($this->src['format']) && $this->src['format'] == 'gif') &&
$this->options['preserve_transparency'] === TRUE) {
$color_transparent = imagecolorallocate(
$this->wrk['image'],
$this->options['transparency_mask_color']['0'],
$this->options['transparency_mask_color']['1'],
$this->options['transparency_mask_color']['2']
);
imagecolortransparent($this->wrk['image'], $color_transparent);
imagetruecolortopalette($this->wrk['image'], true, 255);
}
}
private function hex2rgb($hex_code)
{
return array(
base_convert(substr($hex_code, 0, 2), 16, 10),
base_convert(substr($hex_code, 2, 2), 16, 10),
base_convert(substr($hex_code, 4, 2), 16, 10)
);
}
public function clear()
{
$this->__destruct();
$this->src = array();
$this->dst = array();
$this->wrk = array();
$this->canvas = array();
$this->error = false;
}
public function reset()
{
$this->options = array(
'jpeg_quality' => 75,
'preserve_alpha' => TRUE,
'alpha_mask_color' => array (255, 255, 255),
'preserve_transparency' => TRUE,
'transparency_mask_color' => array(0, 0, 0),
'watermark' => FALSE,
'watermark_img_path' => NULL,
);
}
public function get_error($translate=FALSE)
{
if ($this->error !== FALSE) {
return $this->errors[$this->error];
}
}
public function set_option($option, $value)
{
$this->options[$option] = $value;
}
private function check()
{
if (!function_exists('gd_info')) {
throw new Exception('Application Error. Support for GD is not available!');
}
$gd_info = gd_info();
if (!isset($gd_info['JPG Support']) OR !$gd_info['JPG Support']) {
// required to PHP >= 5.3
if (!isset($gd_info['JPEG Support']) OR !$gd_info['JPEG Support']) {
throw new Exception('Application Error. Support for JPG is not available in GD!');
}
}
if (!$gd_info['PNG Support']) {
throw new Exception('Application Error. Support for PNG is not available in GD!');
}
if (!$gd_info['GIF Read Support'] || !$gd_info['GIF Create Support']) {
throw new Exception('Application Error. Support for GIF is not available in GD!');
}
}
}
?>