PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/qcodo/qform/QImageFileControl.class.php

http://tracmor.googlecode.com/
PHP | 307 lines | 204 code | 27 blank | 76 comment | 46 complexity | 33e51964e33d913c2d7e59030d711687 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Copyright (c) 2009, Tracmor, LLC
  4. *
  5. * This file is part of Tracmor.
  6. *
  7. * Tracmor is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tracmor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tracmor; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. ?>
  22. <?php
  23. // This class will render an HTML File input for images.
  24. // * "FileName" is the name of the file that the user uploads
  25. // * "Type" is the MIME type of the file
  26. // * "Size" is the size in bytes of the file
  27. // * "File" is the temporary full file path on the server where the file physically resides
  28. class QImageFileControl extends QFileControl {
  29. ///////////////////////////
  30. // Private Member Variables
  31. ///////////////////////////
  32. // MISC
  33. protected $strWebPath = null;
  34. protected $strThumbWebPath = null;
  35. protected $strUploadPath = null;
  36. protected $strThumbUploadPath = null;
  37. protected $boolBuildThumbs = false;
  38. protected $intThumbWidth = 150;
  39. protected $intThumbHeight = 150;
  40. protected $intThumbQuality = 60;
  41. protected $strThumbPrefix = null;
  42. protected $strPrefix = null;
  43. protected $strSuffix = null;
  44. //////////
  45. // Methods
  46. //////////
  47. public function ParsePostData() {
  48. // Check to see if this Control's Value was passed in via the POST data
  49. // Added the tmp_name condition because it was submitting the Control even when empty
  50. // Mike Ho recognized it as a bug and will be fixing it in the next release
  51. if (array_key_exists($this->strControlId, $_FILES) && ($_FILES[$this->strControlId]['tmp_name'])) {
  52. // It was -- update this Control's value with the new value passed in via the POST arguments
  53. if ($_FILES[$this->strControlId]['name']) {
  54. $explodedFilename = explode(".",$_FILES[$this->strControlId]['name']);
  55. $this->strFileName = "asset_model.".$explodedFilename[count($explodedFilename)-1];
  56. $this->strType = $_FILES[$this->strControlId]['type'];
  57. $this->intSize = QType::Cast($_FILES[$this->strControlId]['size'], QType::Integer);
  58. $this->strFile = $_FILES[$this->strControlId]['tmp_name'];
  59. }
  60. }
  61. }
  62. /**
  63. * Save the uploaded file to the file system
  64. *
  65. */
  66. public function ProcessUpload() {
  67. if((strpos($this->strType, "image")) !== false) {
  68. move_uploaded_file($this->strFile, $this->strUploadPath.$this->strFileName);
  69. if ($this->boolBuildThumbs) {
  70. $this->CreateThumbnail( $this->strUploadPath.$this->strFileName,
  71. $this->strThumbUploadPath.$this->strThumbPrefix.$this->strFileName,
  72. $this->intThumbWidth,
  73. $this->intThumbHeight,
  74. $this->intThumbQuality);
  75. }
  76. }
  77. if (AWS_S3) {
  78. QApplication::MoveToS3($this->strUploadPath, $this->strFileName, $this->strType, '/images/asset_models');
  79. if ($this->boolBuildThumbs) {
  80. if (file_exists($this->strThumbUploadPath.$this->strFileName)) {
  81. QApplication::MoveToS3($this->strThumbUploadPath, $this->strFileName, $this->strType, '/images/asset_models/thumbs');
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * Creates a thumbnail of the image passed and saves it to $thumbnail
  88. * Thumbnail uses either width or height, whichever is larger in the original, and then maintains aspect ratio
  89. *
  90. * @param string $original path and filename of original/uploaded image
  91. * @param string $thumbnail location to store thumbnail image
  92. * @param integer $width
  93. * @param integer $height
  94. * @param integer $quality
  95. */
  96. protected function CreateThumbnail ($original, $thumbnail, $width, $height, $quality) {
  97. list($width_orig, $height_orig) = getimagesize($original);
  98. if ($width && ($width_orig < $height_orig)) {
  99. $width = ($height / $height_orig) * $width_orig;
  100. }
  101. else {
  102. $height = ($width / $width_orig) * $height_orig;
  103. }
  104. $image_p = imagecreatetruecolor($width, $height);
  105. switch ($this->strType) {
  106. case 'image/gif':
  107. $image = imageCreateFromGIF($original);
  108. break;
  109. case 'image/jpeg':
  110. case 'image/pjpeg':
  111. $image = imageCreateFromJPEG($original);
  112. break;
  113. case 'image/png':
  114. case 'image/x-png':
  115. $image = imageCreateFromPNG($original);
  116. break;
  117. case 'image/wbmp':
  118. $image = imageCreateFromWBMP($original) ;
  119. break;
  120. }
  121. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  122. imagejpeg($image_p, $thumbnail, $quality);
  123. }
  124. /**
  125. * Returns the HTML IMG tag to display with a link to a copy of the image
  126. *
  127. * @param string $strImagePath
  128. * @return string $strToReturn An HTML image tag with link
  129. */
  130. public function GetDisplayHtml($strImagePath) {
  131. if ($strImagePath) {
  132. if (AWS_S3) {
  133. $href = 'http://s3.amazonaws.com/' . AWS_BUCKET . '/images/asset_models/' . $strImagePath;
  134. $src = 'http://s3.amazonaws.com/' . AWS_BUCKET . '/images/asset_models/thumbs/' . $strImagePath;
  135. }
  136. else {
  137. $href = $this->strWebPath . $strImagePath;
  138. $src = $this->strThumbWebPath . $this->strThumbPrefix . $strImagePath;
  139. }
  140. $strToReturn = sprintf('<a href="%s"><img src="%s" border="0" /></a>', $href, $src);
  141. }
  142. else {
  143. $strToReturn = "";
  144. }
  145. return $strToReturn;
  146. }
  147. /**
  148. * Deletes the image and thumbnail from the filesystem
  149. *
  150. * @param string $strImagePath path of the image as stored in the db
  151. */
  152. public function Delete($strImagePath) {
  153. if ($strImagePath) {
  154. $filename = $this->strUploadPath.$strImagePath;
  155. if (is_file($filename)) {
  156. unlink($this->strUploadPath.$strImagePath);
  157. }
  158. else {
  159. // throw new Exception('File '.$filename.' does not exist to delete.');
  160. }
  161. $filename_thumb = $this->strThumbUploadPath.$this->strThumbPrefix.$strImagePath;
  162. if (is_file($filename_thumb)) {
  163. unlink($this->strThumbUploadPath.$this->strThumbPrefix.$strImagePath);
  164. }
  165. else {
  166. // throw new Exception('File '.$filename_thumb.' does not exist to delete.');
  167. }
  168. }
  169. }
  170. public function Validate() {
  171. // there is no error until we find one
  172. $returnValue = true;
  173. $this->strValidationError = "";
  174. if ($this->strFileName) {
  175. // Check the MIME type of the file
  176. if(strpos($this->strType, "image") === false) {
  177. $this->strValidationError = sprintf("%s is not an image", $this->strFileName);
  178. return false;
  179. }
  180. // Check that there is only one period in the filename, separating name from extension
  181. $explosion = explode(".", $this->strFileName);
  182. if (count($explosion) != 2) {
  183. $this->strValidationError = "Please upload a well formed filename: xxxxx.xxx";
  184. return false;
  185. }
  186. /*$fileTemp = fopen($this->strFile, "r");
  187. $fileBinary = fread($fileTemp, filesize($this->strFile));*/
  188. if (!@getimagesize($this->strFile)) {
  189. $this->strValidationError = "That is not a valid image file.";
  190. return false;
  191. }
  192. // Check for jpg, jpeg, gif, or png extensions
  193. if ($explosion[count($explosion) -1] != "jpg" && $explosion[count($explosion) -1] != "JPG" && $explosion[count($explosion) -1] != "jpeg" && $explosion[count($explosion) -1] != "JPEG" && $explosion[count($explosion) -1] != "png" && $explosion[count($explosion) -1] != "PNG" && $explosion[count($explosion) -1] != "gif" && $explosion[count($explosion) -1] != "GIF") {
  194. $this->strValidationError = "Invalid file type. Image must be either .jpg, .gif, or .png";
  195. return false;
  196. }
  197. }
  198. // Only if it is a required field, check if it is empty.
  199. if ($this->blnRequired) {
  200. if (strlen($this->strFileName) < 0) {
  201. $this->strValidationError = sprintf("%s is required", $this->strName);
  202. $returnValue = false;
  203. }
  204. }
  205. return $returnValue;
  206. }
  207. /////////////////////////
  208. // Public Properties: GET
  209. /////////////////////////
  210. public function __get($strName) {
  211. switch ($strName) {
  212. // MISC
  213. case "FileName": return $this->strFileName;
  214. case "Type": return $this->strType;
  215. case "Size": return $this->intSize;
  216. case "File": return $this->strFile;
  217. case "UploadPath": return $this->strUploadPath;
  218. case "WebPath": return $this->strWebPath;
  219. case "ThumbUploadPath": return $this->strThumbUploadPath;
  220. case "ThumbWebPath": return $this->strThumbWebPath;
  221. case "BuildThumbs": return $this->boolBuildThumbs;
  222. case "ThumbWidth": return $this->intThumbWidth;
  223. case "ThumbHeight": return $this->intThumbHeight;
  224. case "ThumbPrefix": return $this->strThumbPrefix;
  225. case "Prefix": return $this->strPrefix;
  226. case "Suffix": return $this->strSuffix;
  227. default:
  228. try {
  229. return parent::__get($strName);
  230. } catch (QCallerException $objExc) {
  231. $objExc->IncrementOffset();
  232. throw $objExc;
  233. }
  234. }
  235. }
  236. /////////////////////////
  237. // Public Properties: SET
  238. /////////////////////////
  239. public function __set($strName, $mixValue) {
  240. switch ($strName) {
  241. case "FileName": $this->strFileName = $mixValue;
  242. break;
  243. case "Type": $this->strType = $mixValue;
  244. break;
  245. case "Size": $this->intSize = $mixValue;
  246. break;
  247. case "File": $this->strFile = $mixValue;
  248. break;
  249. case "UploadPath": $this->strUploadPath = $mixValue;
  250. break;
  251. case "WebPath": $this->strWebPath = $mixValue;
  252. break;
  253. case "ThumbUploadPath": $this->strThumbUploadPath = $mixValue;
  254. break;
  255. case "ThumbWebPath": $this->strThumbWebPath = $mixValue;
  256. break;
  257. case "BuildThumbs": $this->boolBuildThumbs = (bool) $mixValue;
  258. break;
  259. case "ThumbWidth": $this->intThumbWidth = (int) $mixValue;
  260. break;
  261. case "ThumbHeight": $this->intThumbHeight = (int) $mixValue;
  262. break;
  263. case "ThumbPrefix": $this->strThumbPrefix = $mixValue;
  264. break;
  265. case "Prefix": $this->strPrefix = $mixValue;
  266. break;
  267. case "Suffix": $this->strSuffix = $mixValue;
  268. break;
  269. default:
  270. try {
  271. parent::__set($strName, $mixValue);
  272. } catch (QCallerException $objExc) {
  273. $objExc->IncrementOffset();
  274. throw $objExc;
  275. }
  276. break;
  277. }
  278. }
  279. }
  280. ?>