/library/WeFlex/Image.php

https://github.com/rocknoon/Stack · PHP · 293 lines · 164 code · 74 blank · 55 comment · 4 complexity · bbe702496d6fb4699a92a0b40a41efbc MD5 · raw file

  1. <?php
  2. require_once 'Image/Processor.php';
  3. require_once 'Image/Uploader.php';
  4. require_once 'Image/Thumbnail.php';
  5. class WeFlex_Image
  6. {
  7. protected $_tempFolder = 'temp';
  8. protected $_tempCropFolder = 'crop';
  9. protected $_uploadUrl;
  10. /**
  11. * array(
  12. * array( 111 , 222 ),
  13. * array( 333 , 444 )
  14. * )
  15. *
  16. *
  17. * @var array
  18. */
  19. protected $_regularImageStandards;
  20. /**
  21. * get a image by it's size
  22. * @return String
  23. */
  24. public static function GetImageUrlBySize($surl,$width,$height) {
  25. $durl = substr($surl,0,strrpos($surl,'.')).'_'.$width . 'x' . $height . substr($surl,strrpos($surl,'.'));
  26. return $durl;
  27. }
  28. /**
  29. * Image download
  30. * @return String
  31. *
  32. */
  33. public static function Download( $url , $filePath ){
  34. if(!$url){
  35. return false;
  36. }
  37. ob_start();
  38. readfile( $url );
  39. $img = ob_get_contents();
  40. ob_end_clean();
  41. file_put_contents( $filePath , $img );
  42. // $fp2=@fopen($filePath, $img) ;
  43. // fclose($fp2) ;
  44. return $filePath;
  45. }
  46. public function __construct( $uploadUrl = null , $resize = null ){
  47. $this->_uploadUrl = $uploadUrl;
  48. $this->_regularImageStandards = $resize;
  49. }
  50. /**
  51. * Image upload
  52. *
  53. * @return String tempUrl
  54. */
  55. public function upload(){
  56. $options = array();
  57. $options['target_path'] = $this->_getTempPath();
  58. $options['field_name'] = 'fileField';
  59. $options['save_name'] = array('random');
  60. $options['allow_type'] = array('jpeg','jpg','gif','bmp','png');
  61. $upload = new WeFlex_Image_Uploader($options);
  62. $rtn = $upload->upload();
  63. if(0!=$rtn){
  64. throw new Exception( "image upload error" );
  65. }
  66. $file_name = $upload->get_file_name();
  67. $rtn = $this->_getTempUrl() . '/' . $file_name;
  68. return $rtn;
  69. }
  70. public function cropImage( $tempUrl , $cropParam ){
  71. $imageFileName = $this->_getImageFileName( $tempUrl );
  72. $tempImagePath = WeFlex_Application::GetInstance()->getPublicPath() . $tempUrl;
  73. $tempCropImageDir = $this->_getTempPath() . '/' . $this->_tempCropFolder . '/';
  74. $cropImageUrl = $this->_getTempUrl() . '/' . $this->_tempCropFolder . '/' . $imageFileName;
  75. $this->_mkDir($tempCropImageDir);
  76. //$this->_copy( $tempImagePath , $tempCropImagePath );
  77. $this->_cropImage( $tempImagePath , $cropParam['x1'] , $cropParam['x2'] , $cropParam['y1'] , $cropParam['y2'] , $tempCropImageDir );
  78. return $cropImageUrl;
  79. }
  80. /**
  81. * change a temp image into a regular image;
  82. *
  83. * @param String || int $index (image belong to which index( product or else ))
  84. * @param String $imageTempUrl
  85. * @pram $cropParam array(
  86. * x1,y1,x2,y2
  87. * );
  88. * @throws Exception( 'temp url is not exist' )
  89. *
  90. */
  91. public function regular( $index , $tempUrl , $cropParam = null ){
  92. if( !$cropParam ){
  93. $regularImage = $this->_moveTempToRegular( $index , $tempUrl);
  94. }else{
  95. $regularImage = $this->_moveTempToRegularCrop( $index , $tempUrl , $cropParam );
  96. }
  97. $this->_generStandard($regularImage['path']);
  98. return $regularImage['url'];
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function getRegularImageStandards() {
  104. return $this->_regularImageStandards;
  105. }
  106. /**
  107. * @return unknown
  108. */
  109. public function getRegularPath() {
  110. return $this->_getRegularPath();
  111. }
  112. /**
  113. * @return unknown
  114. */
  115. public function getRegularUrl() {
  116. return $this->_getRegularUrl();
  117. }
  118. /**
  119. * @return unknown
  120. */
  121. public function getTempCropDir() {
  122. return $this->_tempCropFolder;
  123. }
  124. /**
  125. * @return unknown
  126. */
  127. public function getTempPath() {
  128. return $this->_getTempPath();
  129. }
  130. /**
  131. * @return unknown
  132. */
  133. public function getTempUrl() {
  134. return $this->_getTempUrl();
  135. }
  136. protected function _moveTempToRegular( $index , $tempUrl ){
  137. $imageFileName = $this->_getImageFileName( $tempUrl );
  138. $tempImagePath = WeFlex_Application::GetInstance()->getPublicPath() . $tempUrl;
  139. $regularDir = $this->_getRegularPath() . '/' . $index . '/' ;
  140. $regularImagePath = $regularDir . $imageFileName;
  141. $regularImageUrl = $this->_getRegularUrl() . '/' . $index . '/' . $imageFileName;
  142. $this->_mkDir($regularDir);
  143. $this->_copy ($tempImagePath , $regularImagePath );
  144. return array( 'path' => $regularImagePath , 'url' => $regularImageUrl );
  145. }
  146. protected function _moveTempToRegularCrop( $index , $tempUrl , $cropParam ){
  147. $imageFileName = $this->_getImageFileName( $tempUrl );
  148. $tempImagePath = WeFlex_Application::GetInstance()->getPublicPath() . $tempUrl;
  149. $regularDir = $this->_getRegularPath() . '/'. $index . '/' ;
  150. $regularImagePath = $regularDir . $imageFileName;
  151. $regularImageUrl = $this->_getRegularUrl() . '/' . $index . '/' . $imageFileName;
  152. $this->_mkDir($regularDir);
  153. $this->_cropImage( $tempImagePath , $cropParam['x1'] , $cropParam['x2'] , $cropParam['y1'] , $cropParam['y2'] , $regularDir );
  154. return array( 'path' => $regularImagePath , 'url' => $regularImageUrl );
  155. }
  156. protected function _cropImage( $originalImage , $x1 , $x2 ,$y1 , $y2 , $targetDir ){
  157. list($width, $height, $type, $attr) = getimagesize($originalImage);
  158. $left = $x1;
  159. $right = $width - $x2;
  160. $top = $y1;
  161. $bottom = $height - $y2;
  162. $thumbWidth = $x2 - $x1;
  163. $thumbHeight = $y2 - $y1;
  164. $imageProc = new WeFlex_Image_Thumbnail();
  165. $imageProc -> Cropimage = array(1, 1,
  166. (int)$left,(int)$right,(int)$top,(int)$bottom);
  167. $imageProc -> Thumbwidth = (int)$thumbWidth;
  168. $imageProc -> Thumbheight = (int)$thumbHeight;
  169. $imageProc -> Thumblocation = $targetDir;
  170. $imageProc -> Thumbprefix = '';
  171. $imageProc -> Createthumb($originalImage, 'file');
  172. }
  173. protected function _mkDir( $dirPath ){
  174. if( !is_dir( $dirPath ) ){
  175. mkdir($dirPath,0777,true);
  176. @chmod($dirPath,0777);
  177. }
  178. }
  179. protected function _copy( $orginalFile , $toFile ){
  180. copy($orginalFile,$toFile);
  181. @chmod($toFile,0777);
  182. }
  183. protected function _getImageFileName( $imageUrl ){
  184. $fileName = substr($imageUrl,strrpos($imageUrl,'/'));
  185. $fileName = substr($fileName,1);
  186. return $fileName;
  187. }
  188. protected function _changeFileName( $fileName ){
  189. $fileNames = explode( '.' , $fileName );
  190. $newName = md5( time() . $fileNames[0] );
  191. return $newName . '.' . $fileNames[1];
  192. }
  193. protected function _generStandard( $regularPath ){
  194. foreach ( $this->_regularImageStandards as $standard ){
  195. $this->_generImageFile( $regularPath , $standard[0] , $standard[1] , $standard[2] , $standard[3] , $standard[4]);
  196. }
  197. }
  198. protected function _generImageFile( $sFile , $width , $height , $transparent = 0 , $backcolor = null , $focus = null ){
  199. $dFile = WeFlex_Image_Processor::switchpic( $sFile , $width , $height , $transparent , $backcolor , $focus );
  200. return $dFile;
  201. }
  202. protected function _getTempPath(){
  203. $tempPath = WeFlex_Application::GetInstance()->config->public_path . $this->_uploadUrl . '/' . $this->_tempFolder;
  204. return $tempPath;
  205. }
  206. protected function _getTempUrl(){
  207. $tempUrl = $this->_uploadUrl . '/' . $this->_tempFolder;
  208. return $tempUrl;
  209. }
  210. protected function _getRegularPath(){
  211. $regularPath = WeFlex_Application::GetInstance()->config->public_path . $this->_uploadUrl;
  212. return $regularPath;
  213. }
  214. protected function _getRegularUrl(){
  215. $regularUrl = $this->_uploadUrl;
  216. return $regularUrl;
  217. }
  218. }
  219. ?>