<?php
class imagesAction extends FirstendAction {	
	public function _initialize() {
        parent::_initialize();			
    }
    public function index(){		
		$mid = I('id');
		$mid = str_replace('.png','',$mid);
		$mid = str_replace('.jpg','',$mid);
		if(C('ftx_a_link')=='id'){
		$item = D('article')->where(array('id'=>$mid,'status'=>1,'fabu'=>1))->find();	
		}else{
		$item = D('article')->where(array('mid'=>$mid,'status'=>1,'fabu'=>1))->find();	
		}
		!$item && $this->_404();
		$cinfo = D('article_cate')->where(array('id'=>$item['cate_id']))->find();
		!$cinfo && $this->_404();
		if($cinfo['ename']){	
		$cpath = $cinfo['ename'];
		}else{
		$cpath = 'pic';	
		}		
		$this->doimg($cpath,$item['title'],$item['id']);
	} 
	public function doimg($cpath,$title,$id){	
		    $con = "data/upload/peitu/".$cpath."/";
		    $imgname = scandir($con);
		    for($y=2;$y<count($imgname);$y++){
                $conname[] = $imgname[$y];
		    }
		    if(count($conname)){				
				if(S($id)){
				$pi = S($id);	
				}else{
				$num = count($conname)-1;
                $pi = rand(0,$num);	
				S($id,$pi,86400);
				}                
                $img = $conname[$pi];
                $src = C('ftx_site_url').$con.$img;		
                $path = "data/upload/article/".$cpath."/";		
                $this->make_dir($path);
                $newname = md5($title);		                
                $newimg = Image::image_hp($src,$title,$path,$newname);	                
				return $newimg;
			}	
	}
    public function make_dir($path)
    {
        if (!file_exists($path)) {
            $mk = @mkdir($path, 0777, true);
            @chmod($path, 0777, true);
        }
        return true;
    }  
}