PageRenderTime 83ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 38ms app.codeStats 1ms

/src/service/design/srv/PwDesignImage.php

https://github.com/cuijinquan/nextwind
PHP | 180 lines | 152 code | 17 blank | 11 comment | 34 complexity | 6f7ca0ca13dd9b7d5edf319a4cfadb2e MD5 | raw file
  1<?php
  2Wind::import('LIB:image.PwCutImage');
  3/**
  4 * the last known user to change this file in the repository  <$LastChangedBy: gao.wanggao $>
  5 * @author $Author: gao.wanggao $ Foxsee@aliyun.com
  6 * @copyright ?2003-2103 phpwind.com
  7 * @license http://www.phpwind.com
  8 * @version $Id: PwDesignImage.php 24487 2013-01-31 02:50:53Z gao.wanggao $ 
  9 * @package 
 10 */
 11class PwDesignImage {
 12	protected $store = '';
 13	protected $moduleid = 0;
 14	protected $thumbW = 0;
 15	protected $thumbH = 0;
 16	protected $ext = '';
 17	protected $image = '';
 18
 19	public function setInfo($moduleid, $image, $thumbW, $thumbH) {
 20		$this->moduleid = (int)$moduleid;
 21		$this->thumbW = (int)$thumbW;
 22		$this->thumbH = (int)$thumbH;
 23		$this->ext  = strtolower(substr(strrchr($image, '.'), 1));
 24		$this->image = $image;
 25		$this->store = Wind::getComponent('storage');
 26	}
 27	
 28	/*public function setStore() {
 29		$this->store = Wind::getComponent('storage');
 30	}*/
 31	
 32	public function cut() {
 33		static $isDel = false;
 34		$outFile = $this->getFileName();
 35		$outDir = $this->getSaveDir($this->moduleid);
 36		$cut = new PwCutImage();
 37		$image = $this->getRealPath($outFile);
 38		if (!$image) return array('', '',$this->store->get($this->image, 0)); //返回原图片
 39		$cut->image = $image;
 40		$cut->outImage = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/' .$outDir.$outFile;
 41		$cut->cutWidth = $this->thumbW;
 42		$cut->cutHeight = $this->thumbH;
 43		$cut->quality = 90;
 44		$cut->forceThumb = true;
 45		$cut->forceScale = true;
 46		if ($cut->cut() !== false) {
 47			if (!$this->store instanceof PwStorageLocal) {
 48				$localFile = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/' . $outDir. $outFile;
 49				$this->store->save($localFile, $outDir. $outFile);
 50				$attachUrl = $this->store->get('', 0);
 51						
 52				WindFile::del(Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/_tmp/' . $outFile);
 53				WindFile::del($localFile);
 54			} else {
 55				$attachUrl = Wekit::url()->attach . '/';
 56			}
 57			return array($outDir,$outFile, $attachUrl );
 58		}
 59		return array('', '',$this->store->get($this->image, 0)); //返回原图片
 60	}
 61	
 62	public function clearFolder($moduleid) {
 63		if (!$moduleid) return false;
 64		$dir = $this->getSaveDir($moduleid);
 65		$store = Wind::getComponent('storage'); //单独使用
 66		if (!$store instanceof PwStorageLocal ) { 
 67			$store->delete($dir, 0);
 68		} else {
 69			$dir = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/' . $dir;
 70			WindFolder::clearRecur($dir, true);
 71		}
 72		return true;
 73	}
 74	
 75	public function clearFiles($moduleid, $images) {
 76		if (!$images || !is_array($images)) return false;
 77		$dir = $this->getSaveDir($moduleid);
 78		$store = Wind::getComponent('storage'); //单独使用
 79		if (!$store instanceof PwStorageLocal ) { 
 80			foreach ($images AS $image) {
 81				$store->delete($dir. $image, 0);
 82			}
 83		} else {
 84			$dir = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/' . $dir;
 85			foreach ($images AS $image) {
 86				is_file($dir. $image) && WindFile::del($dir. $image);
 87			}
 88		}
 89	}
 90	
 91	protected function getRealPath($outFile) {
 92		if (!$this->store instanceof PwStorageLocal ) {
 93			$localDir = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/_tmp/';
 94			$path = $this->getImage($this->store->get($this->image, 0), $localDir, $outFile);
 95		} else {
 96			$path = Wind::getRealDir('PUBLIC:') . PUBLIC_ATTACH . '/' . $this->image;
 97		}
 98		return $path;
 99	}
100	
101	protected  function getFileName() {
102		$num = 6;
103		$filename = $this->thumbW .'_'. $this->thumbH.'_';
104		$str = '0123456789abcdefghjkmnopqrstuvwxyABCDEFGHJKLMNOPQRSTUVWXY';
105		$len = Pw::strlen($str)-1;
106		for($i = 0; $i < $num; $i++){
107		    $_num = mt_rand(0, $len);
108		    $filename .= substr($str, $_num, 1); 
109		}
110		$filename .='.'.$this->ext;
111		return $filename;
112	}
113	
114	protected  function getSaveDir($moduleid) {
115		return 'module/'.$moduleid.'/';
116	}
117	
118	protected function getImage($url, $path, $filename = "") {  
119		if($url == "" || $path == "") return false;
120		if (!$this->createFolder($path)) return false;
121		$ext = strrchr($url,".");
122		if($ext != ".gif" && $ext!= ".jpg" &&  $ext != ".png") return false;
123		$filename = $filename ? $filename : mt_rand(1, 999999).'.'.$ext; 
124		$filename = $path.$filename;
125		if ($this->store instanceof PwStorageFtp) {
126			$ftp = Wekit::load('design.srv.ftp.PwDesignFtp');
127	    	$ftp->download($this->image, $filename);
128		} else {
129			ob_start(); 
130			echo $this->getContents($url);
131			$img = ob_get_contents(); 
132			ob_end_clean();
133			WindFile::write($filename, $img);
134		}
135		return $filename; 
136	}
137	
138	
139	
140	protected function getContents($url) {
141		$timeout = 30;
142		$contents = false;
143		if (function_exists('curl_init')) {
144		    $ch = curl_init();
145		    curl_setopt($ch, CURLOPT_URL, $url);
146		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
147		    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
148		    $contents = curl_exec($ch);
149		    curl_close($ch);
150		    return $contents;
151	    }
152	    if (function_exists('file_get_contents')) {
153		    if (function_exists('stream_context_create')) {
154		    	$opts = array(  
155					'http'=>array(  
156						'method'=>"GET",  
157						'timeout'=>$timeout,  
158					)  
159				); 
160				$contents =  @file_get_contents($url, false, stream_context_create($opts));  
161			} else {
162				$contents =  @file_get_contents($url);  
163			}
164	    }
165	    
166	    return $contents;
167	}
168
169	
170	
171	private function createFolder($path ='') {
172		if (!$path) return false;
173		if (!is_dir($path)) {
174           $this->createFolder(dirname($path));
175           if (!@mkdir($path,0777)) return false;
176        }
177		return true;
178	}
179}
180?>