/application/modules/geocontexter/models/ImageUpload.php

http://geocontexter.googlecode.com/ · PHP · 257 lines · 165 code · 40 blank · 52 comment · 26 complexity · b7824a026fdc7e6275824a6d4f941db6 MD5 · raw file

  1. <?php
  2. /**
  3. * GeoContexter
  4. * @link http://code.google.com/p/geocontexter/
  5. * @package GeoContexter
  6. */
  7. /**
  8. * Upload image in folder and create thumbnail from
  9. *
  10. USAGE:
  11. <pre>
  12. $image = new Geocontexter_Model_ImageUpload;
  13. $params = array('post_name' => string, // as defined in $_FILES
  14. 'data_folder' => string // full path to the image directory
  15. );
  16. $result = $image->upload( $params );
  17. if($result instanceof Mozend_ModelError)
  18. {
  19. return $this->error( $result->getErrorString(), __file__, __line__ );
  20. }
  21. </pre>
  22. * @package GeoContexter
  23. * @subpackage Module_Geocontexter
  24. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  25. * @author Armand Turpel <geocontexter@gmail.com>
  26. * @version $Rev: 768 $ / $LastChangedDate: 2010-12-16 16:11:56 +0100 (jeu., 16 d?Šc. 2010) $ / $LastChangedBy: armand.turpel $
  27. */
  28. class Geocontexter_Model_ImageUpload extends Mozend_Model
  29. {
  30. /**
  31. * upload image
  32. */
  33. public function upload( $params )
  34. {
  35. try
  36. {
  37. $this->adapter = new Zend_File_Transfer_Adapter_Http( array('ignoreNoFile' => true) );
  38. $this->adapter->addValidator('Size', false, 1000000);
  39. $this->adapter->addValidator('ImageSize', false,
  40. array('minwidth' => 100,
  41. 'maxwidth' => 2000,
  42. 'minheight' => 100,
  43. 'maxheight' => 2000)
  44. );
  45. $this->adapter->setValidators(array('Extension' => array('jpg', 'jpeg', 'gif', 'png')));
  46. $this->set_params( $params );
  47. $this->path = $params['data_folder'];
  48. $this->_file = $this->file_info[$params['post_name']];
  49. // test if image upload
  50. //
  51. if(empty($this->file_info[$params['post_name']]['tmp_name']) || empty($this->file_info[$params['post_name']]['name']))
  52. {
  53. throw new Exception('Upload image failed. Uploaded file dosent exists in $_FILES');
  54. }
  55. $this->create_files_folder( $params['data_folder'] );
  56. $this->adapter->setDestination( $params['data_folder'] );
  57. if (!$this->adapter->receive())
  58. {
  59. $this->set_error($this->adapter->getMessages());
  60. throw new Exception('File upload error');
  61. }
  62. else
  63. {
  64. $this->build_thumbnail($params['data_folder'], $this->file_info[$params['post_name']]);
  65. return $this->file_info[$params['post_name']];
  66. }
  67. }
  68. catch(Exception $e)
  69. {
  70. $this->removeFile();
  71. return new Mozend_ModelError( $this->get_error() );
  72. }
  73. }
  74. /**
  75. * validate parameters
  76. *
  77. * @param array $params
  78. */
  79. private function set_params( & $params )
  80. {
  81. if(!isset($params['post_name']))
  82. {
  83. throw new Exception('post_name field isnt defined');
  84. }
  85. if(false === is_string($params['post_name']))
  86. {
  87. throw new Exception('post_name isnt from type string');
  88. }
  89. $this->file_info = $this->adapter->getFileInfo();
  90. if(!isset($this->file_info[$params['post_name']]))
  91. {
  92. throw new Exception('post_name isnt defined in $_FILES array');
  93. }
  94. if(!isset($params['data_folder']))
  95. {
  96. throw new Exception('data_folder isnt defined');
  97. }
  98. }
  99. /**
  100. *
  101. */
  102. private function create_files_folder( $path )
  103. {
  104. if(!is_dir($path))
  105. {
  106. $oldumask = umask(0);
  107. if(false === mkdir($path, 0777))
  108. {
  109. umask($oldumask);
  110. throw new Exception('Couldnt create directory: ' . $path);
  111. }
  112. umask($oldumask);
  113. }
  114. }
  115. /**
  116. *
  117. */
  118. public function removeFile()
  119. {
  120. if(isset($this->path))
  121. {
  122. if(file_exists( $this->path . '/' . $this->_file['name'] ))
  123. {
  124. if(false === unlink($this->path . '/' . $this->_file['name']))
  125. {
  126. $this->set_error('Error: Couldnt remove file: ' . $this->path . '/' . $this->_file['name']);
  127. }
  128. }
  129. if(file_exists( $this->path . '/thumb-' . $this->_file['name'] ))
  130. {
  131. if(false === unlink($this->path . '/thumb-' . $this->_file['name']))
  132. {
  133. $this->set_error('Error: Couldnt remove file: ' . $this->path . '/thumb-' . $this->_file['name']);
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. *
  140. */
  141. private function build_thumbnail($path, & $file_info)
  142. {
  143. $image = getimagesize ( $path . '/' . $file_info['name'] );
  144. if(!isset($image[0]) || !isset($image[1]))
  145. {
  146. throw new Exception('Couldnt fetch image info: ' . var_export($image,true));
  147. }
  148. $file_info['image_width'] = $image[0];
  149. $file_info['image_height'] = $image[1];
  150. $newwidth = 140;
  151. $newheight = (int) ((140 / $image[0]) * $image[1]);
  152. if(false === ($thumb = imagecreatetruecolor($newwidth, $newheight)))
  153. {
  154. throw new Exception('imagecreatetruecolor failed with params: ' . var_export(array($newwidth, $newheight),true));
  155. }
  156. switch(strtolower($file_info['type']))
  157. {
  158. case 'image/jpeg':
  159. if(false === ($source = imagecreatefromjpeg($path . '/' . $file_info['name'])))
  160. {
  161. throw new Exception('imagecreatefromjpeg failed with param: ' . var_export($path . '/' . $file_info['name'],true));
  162. }
  163. // Resize
  164. if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1])))
  165. {
  166. $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1]);
  167. throw new Exception('imagecopyresized failed with params: ' . var_export($_params,true));
  168. }
  169. if(false === (imagejpeg($thumb, $path . '/thumb-' . $file_info['name'])))
  170. {
  171. $_params = array($thumb, $path . '/thumb-' . $file_info['name']);
  172. throw new Exception('imagejpeg failed with params: ' . var_export($_params,true));
  173. }
  174. return;
  175. case 'image/gif':
  176. if(false === ($source = imagecreatefromgif($path . '/' . $file_info['name'])))
  177. {
  178. throw new Exception('imagecreatefromgif failed with param: ' . var_export($path . '/' . $file_info['name'],true));
  179. }
  180. // Resize
  181. if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1])))
  182. {
  183. $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1]);
  184. throw new Exception('imagecopyresized failed with params: ' . var_export($_params,true));
  185. }
  186. if(false === (imagegif($thumb, $path . '/thumb-' . $file_info['name'])))
  187. {
  188. $_params = array($thumb, $path . '/thumb-' . $file_info['name']);
  189. throw new Exception('imagegif failed with params: ' . var_export($_params,true));
  190. }
  191. return;
  192. case 'image/png':
  193. if(false === ($source = imagecreatefrompng($path . '/' . $file_info['name'])))
  194. {
  195. throw new Exception('imagecreatefrompng failed with param: ' . var_export($path . '/' . $file_info['name'],true));
  196. }
  197. // Resize
  198. if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1])))
  199. {
  200. $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image[0], $image[1]);
  201. throw new Exception('imagecopyresized failed with params: ' . var_export($_params,true));
  202. }
  203. if(false === (imagepng($thumb, $path . '/thumb-' . $file_info['name'])))
  204. {
  205. $_params = array($thumb, $path . '/thumb-' . $file_info['name']);
  206. throw new Exception('imagepng failed with params: ' . var_export($_params,true));
  207. }
  208. return;
  209. default:
  210. throw new Exception('unknown image type: ' . var_export($file_info,true));
  211. }
  212. }
  213. }