<?php
#########################################
## @Author: Ahmed Aboelsaoud           ##
## E-mail: thefox70002000@yahoo.com    ##
## No Copyright To Be Reserved :) ^_^  ##
## Site: http://goo2pro.com			   ##
#########################################

class myResizeImage
{
	// متغير حفظ الكائن المنشأ من الفئة
	private static $classObj = NULL;
	// متغير لحفظ بيانات الصورة الحالية
	private $img = array();
	// متغير لحفظ بيانات الصورة الجديدة
	private $newImg = array();
	// متغير لحفظ نص الخطأ
	private $error;
	
	private function __construct() {}
	
	// دالة إنشاء كائن من نفس الفئة الحالية
	public static function getObj()
	{
        if(!self::$classObj)
		{
            self::$classObj = new self();
		}
		
        return self::$classObj;
    }
	
	/*
	* دالة خاصة لتحميل بيانات الصورة 
	* تأخذ وسيط وهو مسار الصورة
	* الدالة تعيد القيمة الصواب في حالة تحميل الصورة وتعيد القيمة الخطأ عند الفشل في تحميل الصورة
	*/
	private function load($image)
	{
		if(file_exists($image) !== true)
		{
			$this->error = 'لم يتم العثور على الصورة';
			return false;
		}
		if(($temp = getimagesize($image)) === false)
		{
			$this->error = 'تعذر التحقق من الصورة';
			return false;
		}
		if($temp[0] == 0 or $temp[1] == 0)
		{
			$this->error = 'تعذر الحصول على بيانات الصورة';
			return false;
		}
		$this->img['width'] = $temp[0];
		$this->img['height'] = $temp[1];
		$this->img['type'] = $temp[2];
		$temp = explode(".",$image);
		$this->img['ext'] = end($temp);
		$this->img['name'] = basename($image,'.'.$this->img['ext']);
		$this->newImg['saveDir'] = '';
		$this->newImg['name'] = $this->img['name'].'_resized';
		$this->newImg['ext'] = $this->img['ext'];
		$this->newImg['width'] = $this->img['width'];
		$this->newImg['height'] = $this->img['height'];
		$this->img['img'] = imagecreatefromstring(file_get_contents($image));
		return true;
	}
	
	/*
	* دالة عامة لتحجيم الصورة
	* الوسيط الأول اسم الصورة أو مسار الصورة كاملة
	* الوسيط الثاني العرض الجديد للصورة إما بالنسبة المئوية أو النقطية
	* الوسيط الثالث إرتفاع الصورة إما بالنسبة المئوية أو النقطية
	* الوسيط الرابع مجلد وضع الصورة الجديدة والإفتراضي سيكون نفس مجلد الملف
	* الوسيط الخامس اسم الصورة الجديدة والإفتراضي هو نفس اسم الصور المراد إعادة تحجيمها مضافاً إليها لاحقة حتى لايتم إستبدالها بالصورة الأصلية إذا كانت في نفس المجلد
	* الدالة تعيد القيمة الصواب في حالة إعادة تحجيم الصورة وتعيد القيمة الخطأ في حالة فشلها في تحجيم الصورة
	*/
	public function resize($image,$newWidth = null, $newHeight = null, $saveDir = null, $newImgName = null)
	{
		if(!$this->load($image))
		{
			return false;
		}
		if($saveDir !== null)
		{
			$this->newImg['saveDir'] = $saveDir.'/';
		}
		if($newImgName !== null)
		{
			$this->newImg['name'] = $newImgName;
		}
		
		if($newWidth !== null or $newHeight !== null)
		{
			if($newWidth === null)
			{
				return $this->resize_vertical($newHeight);
			}
			else if($newHeight === null)
			{
				return $this->resize_horizontal($newWidth);
			}
			else
			{
				return $this->resize_VH($newWidth ,$newHeight);
			}
		}
		else
		{
			$this->error = 'لم تقم بتحديد أبعاد الصورة الجديدة';
			return false;
		}
	}
	
	/*
	* دالة خاصة للتحجيم رأسياً فقط
	* تأخذ وسيط واحد وهو عرض الصورة الجديد بالنسبة المئوية أو النقطية
	* تعود بالقيمة المعادة من إستدعاء دالة أخرى
	*/
	private function resize_horizontal($newWidth)
	{
		if(preg_match('/\%/',$newWidth))
		{
			$newWidth = preg_replace('/[^0-9.]/','',$newWidth);
			$newWidth = (int)($this->img['width']*($newWidth/100));
		}
		else
		{
			$newWidth = preg_replace('/[^0-9.]/','',$newWidth);
		}
		$this->newImg['width'] = $newWidth;
		return $this->makeNewImg();
	}
	
	/*
	* دالة خاصة للتحجيم أفقياً فقط
	* تأخذ وسيط واحد وهو إرتفاع الصورة الجديد بالنسبة المئوية أو النقطية
	* تعود بالقيمة المعادة من إستدعاء دالة أخرى
	*/
	private function resize_vertical($newHeight)
	{
		if(preg_match('/\%/',$newHeight))
		{
			$newHeight = preg_replace('/[^0-9.]/','',$newHeight);
			$newHeight = (int)($this->img['width']*($newHeight/100));
		}
		else
		{
			$newHeight = preg_replace('/[^0-9.]/','',$newHeight);
		}
		$this->newImg['height'] = $newHeight;
		return $this->makeNewImg();
	}
	
	/*
	* دالة خاصة للتحجيم في الوضع الأفقي والرأسي معاً
	* الدالة تأخذ وسيطين الأول عرض الصورة الجديد والأخر إرتفاع الصورة الجديد إما بالنسبة المئوية أو النقطية .
	* تعود بالقيمة المعادة من إستدعاء دالة أخرى
	*/
	private function resize_VH($newWidth, $newHeight)
	{
		if(preg_match('/\%/',$newWidth))
		{
			$newWidth = preg_replace('/[^0-9.]/','',$newWidth);
			$newWidth = (int)($this->img['width']*($newWidth/100));
		}
		else
		{
			$newWidth = preg_replace('/[^0-9.]/','',$newWidth);
		}
		if(preg_match('/\%/',$newHeight))
		{
			$newHeight = preg_replace('/[^0-9.]/','',$newHeight);
			$newHeight = (int)($this->img['width']*($newHeight/100));
		}
		else
		{
			$newHeight = preg_replace('/[^0-9.]/','',$newHeight);
		}
		$this->newImg['width'] = $newWidth;
		$this->newImg['height'] = $newHeight;
		return $this->makeNewImg();
	}
	
	/*
	* دالة خاصة لتنفيذ عملية إعادة التحجيم
	* تعود بالقيمة الخطأ في حالة الفشل أو تعود بالقيمة المعادة من إستدعاء دالة أخرى
	*/
	private function makeNewImg()
	{
		$this->newImg['img'] = imagecreatetruecolor($this->newImg['width'],$this->newImg['height']);
		if($this->img['type'] == IMAGETYPE_GIF)
		{
			$transparent_color = imagecolortransparent($this->img['img']);
			if(!@imagecolorsforindex($this->img['img'],$transparent_color))
			{
				$black_transparent = imagecolorallocate($this->newImg['img'], 0, 0, 0);
				imagecolortransparent($this->newImg['img'],$black_transparent);
			}
			else
			{
				$transparent_color = imagecolorsforindex($this->img['img'],$transparent_color);
				$current_transparent = imagecolorallocate(	$this->newImg['img'], 
															$transparent_color['red'], 
															$transparent_color['green'], 
															$transparent_color['blue']);
				imagefill($this->newImg['img'], 0, 0, $current_transparent);
				imagecolortransparent($this->newImg['img'], $current_transparent);
			}
		}
		else if($this->img['type'] == IMAGETYPE_PNG)
		{
			imagealphablending($this->newImg['img'], false);
			$transparent_color = imagecolorallocatealpha($this->newImg['img'], 0, 0, 0, 127);
			imagefill($this->newImg['img'], 0, 0,$transparent_color);
			imagesavealpha($this->newImg['img'], true);
		}
		
		if(!imagecopyresampled(	$this->newImg['img'], $this->img['img'], 0, 0, 0, 0, 
								$this->newImg['width'], $this->newImg['height'],
								$this->img['width'], $this->img['height']))
		{
			$this->error = 'خطأ أثناء إعادة تحجيم الصورة';
			return false;
		}
							
		return $this->save();

	}
	
	/*
	* دالة خااصة لحفظ الصورة الجديدة بعد عملية التحجيم
	* تعود بالقيمة الصحيحة في حالة نجاحها أو القيمة الخطأ في حالة فشلها
	*/
	private function save()
	{
		$save = false;
		if($this->newImg['saveDir'] !== '' and !is_dir($this->newImg['saveDir']))
		{
			$this->error = 'خطأ في فتح مجلد الحفظ';
			return false;
		}
		if($this->img['type'] == IMAGETYPE_JPEG)
		{
			$save = imagejpeg($this->newImg['img'],$this->newImg['saveDir'].$this->newImg['name'].'.'.$this->newImg['ext']);
		}
		else if($this->img['type'] == IMAGETYPE_GIF)
		{
			$save = imagegif($this->newImg['img'],$this->newImg['saveDir'].$this->newImg['name'].'.'.$this->newImg['ext']);
		}
		else if($this->img['type'] == IMAGETYPE_PNG)
		{
			$save = imagepng($this->newImg['img'],$this->newImg['saveDir'].$this->newImg['name'].'.'.$this->newImg['ext']);
		}
		if(!$save)
		{
			$this->error = 'خطأ في حفظ الصورة';
			return false;
		}
		else
		{
			return true;
		}
	}
	
	/*
	* دالة عامة لجلب نص الخطأ في حالة وجوده
	* تعود الدالة بقيمة نص الخطأ المخزن في متغير الأخطاء
	*/
	public function getErorr()
	{
		return $this->error;
	}
	
	/*
	* دالة عامة لحذف الصورة
	* وتأخذ وسيط اسم الصورة المراد حذفها أو مسارها 
	* وإذا لم يتم إدخال وسيط ستقوم الدالة بحذف الصورة الحالية إن وجدت
	* تعود بالقيمة الصواب في حالة حذف الصورة وتعود بالقيمة الخطأ في حالة الفشل في حذف الصورة
	*/
	public function delete($image = null)
	{
		if($image === null)
		{
			$image = $this->newImg['saveDir'].$this->newImg['name'].'.'.$this->newImg['ext'];
		}
		if(unlink($image))
		{
			return true;
		}
		else
		{
			$this->error = 'لم يتم حذف الصورة';
			return false;
		}
	}
}
/*
* تم بحمد الله
*/
?>