PageRenderTime 232ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ php-ppcms/includes/classes/core.upload.image.class.php

http://php-ppcms.googlecode.com/
PHP | 175 lines | 128 code | 19 blank | 28 comment | 29 complexity | 7ec2c055cb51af808373261e9b858542 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. //
  9. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.upload.class.php');
  10. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.file.class.php');
  11. include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'thumb.function.php');
  12. include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');
  13. class CoreUploadImage {
  14. var $_inited = false;
  15. //
  16. var $save_path = '';
  17. var $thumb_path = '';
  18. var $resize_path = '';
  19. //
  20. //name
  21. //0 no
  22. //1 -%s
  23. var $name_type = 0;
  24. var $thumb_name = '-thumb';
  25. var $resize_name = '-resize';
  26. //thumb
  27. //0 no
  28. //1 generate
  29. var $generate_thumb = 1;
  30. var $generate_resize = 1;
  31. //constructor
  32. function CoreUploadThumb() {
  33. }
  34. function _init() {
  35. if( $this->_inited == true ) {
  36. return false;
  37. }
  38. $this->save_path = CONFIG_PATH . CONFIG_DIR_PRODUCTS_ORIGINIMAGES;
  39. if( $this->name_type == 1 ) {
  40. $this->thumb_path = CONFIG_PATH . CONFIG_DIR_PRODUCTS_THUMBIMAGES;
  41. $this->resize_path = CONFIG_PATH . CONFIG_DIR_PRODUCTS_RESIZEIMAGES;
  42. }
  43. if( $this->thumb_path == '' ) {
  44. $this->name_type = 1;
  45. $this->thumb_path = $this->save_path;
  46. }
  47. if( $this->resize_path == '' ) {
  48. $this->name_type = 1;
  49. $this->resize_path = $this->save_path;
  50. }
  51. $this->_inited = true;
  52. }
  53. //methods
  54. function upload() {
  55. $this->_init();
  56. $this->up_image_filename = '';
  57. $this->up_image_save_file = '';
  58. //
  59. $up_image_obj = new CoreUpload('field_imageupload');
  60. $up_image_obj->setDestination($this->save_path);
  61. if( $up_image_obj->parse() && $up_image_obj->save() ) {
  62. $up_image_filename = $up_image_obj->save_filename;
  63. $up_image_save_file = $up_image_obj->save_file;
  64. //thumbs
  65. if( $this->generate_thumb == 1 ) {
  66. $thumb_file = $this->thumb_path . $this->_thumb_name($up_image_filename);
  67. $thumb_width = CONST_IMAGE_THUMB_WIDTH;
  68. $thumb_height = CONST_IMAGE_THUMB_HEIGHT;
  69. func_thumb_generate_thumb($up_image_save_file, $thumb_file, $thumb_width, $thumb_height);
  70. }
  71. //resized
  72. if( $this->generate_resize == 1 ) {
  73. $thumb_file = CONFIG_PATH . CONFIG_DIR_UPLOADS_RESIZED . $this->_resize_name($up_image_filename);
  74. $thumb_width = CONST_IMAGE_RESIZE_WIDTH;
  75. $thumb_height = CONST_IMAGE_RESIZE_HEIGHT;
  76. func_thumb_generate_thumb($up_image_save_file, $thumb_file, $thumb_width, $thumb_height);
  77. }
  78. }
  79. //
  80. $this->up_image_filename = $up_image_filename;
  81. $this->up_image_save_file = $up_image_save_file;
  82. }
  83. //
  84. function uploads() {
  85. $up_images_data = array();
  86. $up_images_ids = array();
  87. $up_images_obj = new CoreMultiUpload('field_imagesupload');
  88. $up_images_obj->setDestination($this->save_path);
  89. if( $up_images_obj->parse() && $up_images_obj->save() ) {
  90. $up_images_filename = $up_images_obj->filename;
  91. $up_images_filetype = $up_images_obj->filetype;
  92. $up_images_filesize = $up_images_obj->file['size'];
  93. $up_images_file = $up_images_obj->save_filename;
  94. $up_images_save_file = $up_images_obj->save_file;
  95. for($i=0, $n=sizeof($up_images_save_file); $i<$n; $i++) {
  96. $up_fileObj = new CoreFile($up_images_save_file[$i]);
  97. $up_fileObj->parse();
  98. $up_images_extension = $up_fileObj->extension;
  99. $up_images_attribute = $up_fileObj->getFileAttribute();
  100. $up_images_width = $up_fileObj->getImageWidth();
  101. $up_images_height = $up_fileObj->getImageHeight();
  102. //thumbs
  103. if( $this->generate_thumb == 1 ) {
  104. $thumb_file = $this->thumb_path . $this->_thumb_name($up_images_file[$i]);
  105. $thumb_width = CONST_IMAGE_THUMB_WIDTH;
  106. $thumb_height = CONST_IMAGE_THUMB_HEIGHT;
  107. func_thumb_generate_thumb($up_images_save_file[$i], $thumb_file, $thumb_width, $thumb_height);
  108. }
  109. //resized
  110. if( $this->generate_resize == 1 ) {
  111. $thumb_file = $this->resize_path . $this->_resize_path($up_images_file[$i]);
  112. $thumb_width = CONST_IMAGE_RESIZE_WIDTH;
  113. $thumb_height = CONST_IMAGE_RESIZE_HEIGHT;
  114. func_thumb_generate_thumb($up_images_save_file[$i], $thumb_file, $thumb_width, $thumb_height);
  115. }
  116. //
  117. $up_images_data[] = array(
  118. 'images_name' => $up_images_filename[$i],
  119. 'images_description' => $_POST['field_imagesdesc'][$i],
  120. 'images_value' => $up_images_file[$i],
  121. 'images_filename' => $up_images_filename[$i],
  122. 'images_filetype' => $up_images_filetype[$i],
  123. 'images_filesize' => $up_images_filesize[$i],
  124. 'images_width' => $up_images_width,
  125. 'images_height' => $up_images_height,
  126. 'images_extension' => $up_images_extension,
  127. );
  128. }
  129. }
  130. //
  131. $this->up_images_data = $up_images_data;
  132. }
  133. //
  134. function _thumb_name($filename) {
  135. if( $this->name_type == 1 ) {
  136. if( ($pos = strrpos($filename, '.')) !== false ) {
  137. $filename = substr($filename, 0, $pos) . $this->thumb_name . substr($filename, $pos);
  138. } else {
  139. $filename .= $this->resize_name;
  140. }
  141. }
  142. return $filename;
  143. }
  144. function _resize_name($filename) {
  145. if( $this->name_type == 1 ) {
  146. if( ($pos = strrpos($filename, '.')) !== false ) {
  147. $filename = substr($filename, 0, $pos) . $this->resize_name . substr($filename, $pos);
  148. } else {
  149. $filename .= $this->resize_name;
  150. }
  151. }
  152. return $filename;
  153. }
  154. }
  155. //
  156. ?>