<?php
class ImageResize {
	/**
	 * This function return imageRource if file is png,gif or jpeg
	 * @param $image_type $image_size_info['mime'];
	 * @param $image_temp $_FILES['image_file']['tmp_name']
	 * @return imageResource
	 */
	public static function createImageResource($image_type, $image_temp) {
		switch($image_type){
			case 'image/png':
				$image_res =  imagecreatefrompng($image_temp); break;
			case 'image/gif':
				$image_res =  imagecreatefromgif($image_temp); break;
			case 'image/jpeg': case 'image/pjpeg':
				$image_res = imagecreatefromjpeg($image_temp); break;
			default:
				$image_res = null;
		}
		return $image_res;
	}
	
	/**
	 * This function will proportionally resize image
	 * @param imageSource $source
	 * @param string $destination
	 * @param imageType $image_type
	 * @param int $max_size
	 * @param int $image_width
	 * @param int $image_height
	 * @param jpegQuality $quality
	 * @return boolean
	 */
	public static function normalResizeImage($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality = 80){
	
		if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
	
		//do not resize if image is smaller than max size
		if($image_width <= $max_size && $image_height <= $max_size){
			if(ImageResize::saveImage($source, $destination, $image_type, $quality)){
				return true;
			}
		}
	
		//Construct a proportional size of new image
		$image_scale    = min($max_size/$image_width, $max_size/$image_height);
		$new_width      = ceil($image_scale * $image_width);
		$new_height     = ceil($image_scale * $image_height);
	
		$new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
	
		//Copy and resize part of an image with resampling
		if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
			ImageResize::saveImage($new_canvas, $destination, $image_type, $quality); //save resized image
		}
	
		return true;
	}
	
	/**
	 * This function corps image to create exact square, no matter what its original size!
	 * @param imageSourse $source
	 * @param string $destination
	 * @param imageType $image_type
	 * @param int $square_size
	 * @param int $image_width
	 * @param int $image_height
	 * @param jpegQuality $quality
	 * @return boolean
	 */
	public static function cropImageSquare($source, $destination, $image_type, $square_size, $image_width, $image_height, $quality){
		if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
	
		if( $image_width > $image_height )
		{
			$y_offset = 0;
			$x_offset = ($image_width - $image_height) / 2;
			$s_size     = $image_width - ($x_offset * 2);
		}else{
			$x_offset = 0;
			$y_offset = ($image_height - $image_width) / 2;
			$s_size = $image_height - ($y_offset * 2);
		}
		$new_canvas = imagecreatetruecolor( $square_size, $square_size); //Create a new true color image
	
		//Copy and resize part of an image with resampling
		if(imagecopyresampled($new_canvas, $source, 0, 0, $x_offset, $y_offset, $square_size, $square_size, $s_size, $s_size)){
			ImageResize::saveImage($new_canvas, $destination, $image_type, $quality);
		}
	
		return true;
	}
	
	/**
	 * Saves image resource to file
	 * @param imageResource $source
	 * @param imageDestination $destination
	 * @param imageType $image_type
	 * @param imageQuality $quality
	 * @return boolean
	 */
	public static function saveImage($source, $destination, $image_type, $quality = 80){
		switch(strtolower($image_type)){//determine mime type
			case 'image/png':
				imagepng($source, $destination); return true; //save png file
				break;
			case 'image/gif':
				imagegif($source, $destination); return true; //save gif file
				break;
			case 'image/jpeg': case 'image/pjpeg':
				imagejpeg($source, $destination, $quality); return true; //save jpeg file
				break;
			default: return false;
		}
	}
	
	
	public static function save_png_image($source, $destination, $quality = 1){
		return imagepng($source, $destination, $quality); 
	}
}