<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Cron_avatar extends CI_Controller
{
	public function __construct()
	{ 
		parent:: __construct();
		
		
	}
	public function dirToArray($dir){
  
			$result = array();
			$cdir = scandir($dir);
		   foreach ($cdir as $key => $value)
		   {
			  if (!in_array($value,array(".","..")))
			  {
				 if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
				 {
					$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
				 }
				 else
				 {
					$result[] = $value;
				 }
			  }
		   }
  		return $result;
	}
	
	public function createThumb($spath, $dpath, $maxd) {
	$info = getimagesize($spath);
    $mime = $info['mime'];
	//var_dump($mime);
	if($mime=='image/png'){
		
			$src = ImageCreateFromPNG($spath);
			if (!$src) {return false;} else {
		  $srcw=imagesx($src);
		  $srch=imagesy($src);
		  if ($srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
		  else {$width=$maxd;$height=floor($srch*$width/$srcw);}
		  if ($width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;}  //if image is actually smaller than you want, leave small (remove this line to resize anyway)
		  $thumb=imagecreatetruecolor($width, $height);
			imagealphablending($thumb, false);
		 //if ($height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
		 // else {
			imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
			//  }
			
				imagepng($thumb, $dpath,9);
			}
		 return true;
	}else{
	
		$src=@imagecreatefromjpeg($spath);

		if (!$src) {return false;} else {
		  $srcw=imagesx($src);
		  $srch=imagesy($src);
		  if ($srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
		  else {$width=$maxd;$height=floor($srch*$width/$srcw);}
		  if ($width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;}  //if image is actually smaller than you want, leave small (remove this line to resize anyway)
		  $thumb=imagecreatetruecolor($width, $height);
		// if ($height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
		//  else {
			imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
			//  }
			
				imagejpeg($thumb, $dpath,90);
			}
			 return true;
		}
	  
	 
	}
	//avater
	public function avater_eighty(){
		//$sizes=array('60','80','120','160','240','320');
		$sizes='80';
	    $picdir[]=$this->dirToArray('assets');
		$avatar=$picdir[0]['userfiles']['avatars'];
		$toWidth=$toHeight=$sizes;
		//destination path
		$folderpath = 'assets/userfiles/avatars/'.$toWidth;

		$org_path = 'assets/userfiles/avatars'; //source path
		if (!file_exists($folderpath)) { // file exist in target folder
			mkdir($folderpath, 0777, true);
		}
		
		foreach($avatar as $picval){
			$source_path = $org_path.'/'.$picval;
			$d_path = $folderpath.'/'.$picval;
			$this->createThumb($source_path,$d_path,$toWidth);
			
		}
		return '1';
	}
	public function avater_onesixty(){
		//$sizes=array('60','80','120','160','240','320');
		$sizes='160';
	    $picdir[]=$this->dirToArray('assets');
		$avatar=$picdir[0]['userfiles']['avatars'];
		$toWidth=$toHeight=$sizes;
		//destination path
		$folderpath = 'assets/userfiles/avatars/'.$toWidth;

		$org_path = 'assets/userfiles/avatars'; //source path
		if (!file_exists($folderpath)) { // file exist in target folder
			mkdir($folderpath, 0777, true);
		}
		
		foreach($avatar as $picval){
			$source_path = $org_path.'/'.$picval;
			$d_path = $folderpath.'/'.$picval;
			$this->createThumb($source_path,$d_path,$toWidth);
			
		}
		return '1';
	}
	public function avater_threeTwenty(){
		//$sizes=array('60','80','120','160','240','320');
		$sizes='320';
	    $picdir[]=$this->dirToArray('assets');
		$avatar=$picdir[0]['userfiles']['avatars'];
		$toWidth=$toHeight=$sizes;
		//destination path
		$folderpath = 'assets/userfiles/avatars/'.$toWidth;

		$org_path = 'assets/userfiles/avatars'; //source path
		if (!file_exists($folderpath)) { // file exist in target folder
			mkdir($folderpath, 0777, true);
		}
		
		foreach($avatar as $picval){
			$source_path = $org_path.'/'.$picval;
			$d_path = $folderpath.'/'.$picval;
			$this->createThumb($source_path,$d_path,$toWidth);
			
		}
		return '1';
	}
}