PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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