All pastes #635486 Raw Edit

Something

public text v1 · immutable
#635486 ·published 2007-07-27 03:14 UTC
rendered paste body
<?php

define('DS', DIRECTORY_SEPARATOR);
define('ROOT_DIR', dirname(__FILE__) . DS);
define('FILE_DIR', ROOT_DIR . 'files' . DS);
define('TMP_DIR', FILE_DIR . 'tmp' . DS);
define('IMAGE_DIR', FILE_DIR . 'images' . DS);

class File
{
	public $file;
	public $file_name;
	public $file_extension;
	
	protected $original_file;
	
	public function __construct($file)
	{
		if(empty($file)) {
			throw new Exception();
		}
		
		$file_name = basename($file);
		
		$original_file = $file;
		$temporary_file = TMP_DIR . $file_name;
		
		
		$this->copy($original_file, $temporary_file);
		
		$this->file = $temporary_file;
		$this->file_name = $file_name;
		$this->file_extension = pathinfo($file, PATHINFO_EXTENSION);
		
		$this->original_file = $original_file;
	}
	
	public function copy($from, $to)
	{
		return copy($from, $to);
	}
	
	public function delete($file)
	{
		if(is_dir($file)) {
			return rmdir($file);
		} else {
			return unlink($file);
		}
	}
	
	public function save()
	{
		return $this->copy($this->file, $this->original_file);
	}
	
	public function __toString()
	{
		return $this->file;
	}
	
	public function __destruct()
	{
		$this->delete($this->file);
	}
}

class Image extends File
{
	public function crop($width, $height, $x = 0, $y = 0)
	{
		$original_image = $this->createFromFile($this->file);
		$image = $this->create($width, $height);
		
		imageCopy($image, $original_image, 0, 0, $x, $y, $width, $height);
		
		$this->saveImage($image, $this->file);
		
		imageDestroy($image);
		imageDestroy($original_image);
	}
	
	public function resize($width, $height)
	{
		$original_image = $this->createFromFile($this->file);
				
		$image_size = getImageSize($original_image);
		$image_size = array(
			'width' => $size[0],
			'height' => $size[1]
		);
		$image_size_ratio = array(
			'width' => $width / $size[0],
			'height' => $height / $size[1]
		);
		
		if(($image_size['width'] <= $width) && ($image_size['height'] <= $height)) {
			$new_size = array(
				'width' => $width,
				'height' => $image_size['height']
			);
		} elseif (($image_size_ratio['width'] * $image_size['height']) < $height) {
            $new_size = array(
				'width' => $width,
				'height' => ceil($image_size_ratio['width'] * $image_size['height'])
			);
		} else {
			$new_size = array(
				'width' => ceil($image_size_ratio['height'] * $width),
				'height' => $height
			);
		}

		$image = $this->create($new_size['width'], $new_size['height']);
		
		imageCopyResized($image, $original_image, 0, 0, 0, 0, $new_size['width'], $new_size['height'], $width, $height);
		
		$this->saveImage($image, $this->file);
		imageDestroy($image);
		imageDestroy($original_image);
	}
	
	private function create($width, $height, $true_color = true)
	{
		return ($true_color) ? imagecreatetruecolor($width, $height) : imagecreate($width, $height);
	}
	
	private function createFromFile($file)
	{
		$function = 'imagecreatefrom' . $this->imageType();
		
		return $function($file);
	}
	
	private function saveImage($source, $destination, $quality = 100)
	{
		$type = $this->imageType();
		
		if($type == 'gif') {
			return imagegif($source, $destination);
		} else {
			$function = 'image' . $type;
			return $function($source, $destination, $quality);
		}
	}
	
	private function imageType()
	{
		switch($this->file_extension) {
			case 'jpeg':
			case 'jpg':
				return 'jpeg';
			break;
			case 'png':
				return 'png';
			break;
			case 'gif':
				return 'gif';
			break;
		}
	}
}

$image = new Image(IMAGE_DIR . 'drew.jpg');
$image->crop(221, 202, 223, 7);
$image->save();

?>