Source for file classes_image.php

Documentation is available at classes_image.php

  1. <?php
  2.  
  3. class image extends b_resource {
  4. //base resource definition
  5. var $image_type;
  6. var $derivative_paths;
  7. function image( $constructID = NULL ) {
  8. parent::b_resource( $constructID, 'images' );
  9. $this->derivative_paths = unserialize( $this->derivative_paths );
  10. $this->image_type = $this->set_image_type();
  11. }
  12. function get_index() {
  13. if( $this->name ) {
  14. return $this->name;
  15. } else if( $this->path ) {
  16. $path_parts = pathinfo($this->path);
  17. return $path_parts['basename'];
  18. } else {
  19. return 'upload images';
  20. }
  21. }
  22. function verify_set( $post ) {
  23. //catch uploaded images and their destination folder
  24. foreach($post as $key => $value){
  25. if( strstr( $key, 'f_browser_' ) && $value ) {
  26. //catch destination folder
  27. $IDs[] = substr( $key, ( strrpos( $key, '_' ) + 1 ) );
  28. unset( $post[$key] );
  29. }
  30. }
  31. //catch uploaded images
  32. if( $IDs && count( $IDs ) == 1 ) {
  33. if( count($_FILES) ) {
  34. //retrieve the destination folder
  35. $dest =& get_object( $IDs[0] );
  36. //copy the uploaded files to the destination folder
  37. foreach( $_FILES as $file ) {
  38. if( $file['name'] ) {
  39. $uploadfile = $dest->path.'/'.basename($file['name']);
  40.  
  41. if (!move_uploaded_file($file['tmp_name'], $uploadfile)) {
  42. $returnArray[] = 'uploaded image is corrupt';
  43. }
  44. }
  45. }
  46. //update the folder definition
  47. $dest->scan();
  48. } else {
  49. $returnArray[] = 'no images uploaded';
  50. }
  51. }
  52. //pass post data on for more verification
  53. if( !count($returnArray) ) {
  54. parent::verify_set($post);
  55. } else {
  56. $GLOBALS['errorArray'] = $returnArray;
  57. $this->set_object( $post );
  58. }
  59. }
  60. function set_image_type() {
  61. if( stristr( $this->path, '.jpg' ) ) { return 'jpg'; }
  62. else if( stristr( $this->path, '.gif' ) ) { return 'gif'; }
  63. else if( stristr( $this->path, '.png' ) ) { return 'png'; }
  64. }
  65. function resize_proportional( $max_width = NULL, $max_height = NULL, $dest_path, $derivative_name = NULL ) {
  66. //resize the given image to fit in the given sized box, then save to the destination file
  67. $src_parts = pathinfo( $this->path );
  68. $dest_parts = pathinfo($dest_path);
  69. //load the image
  70. if( $this->image_type == 'jpg' ) {
  71. $im = imagecreatefromjpeg( $this->path );
  72. } else if( $this->image_type == 'gif' ) {
  73. $im = imagecreatefromgif( $this->path );
  74. } else if( $this->image_type == 'png' ) {
  75. $im = imagecreatefrompng( $this->path );
  76. }
  77. //if that went ok
  78. if( isset( $im ) ) {
  79. //determine the output size based on max width and heights
  80. $x = imagesx( $im );
  81. $y = imagesy( $im );
  82. if( $max_width * ( $y / $x ) <= $max_height ) {
  83. $new_x = $max_width;
  84. $new_y = $max_width * ( $y / $x );
  85. } else {
  86. $new_y = $max_height;
  87. $new_x = $max_height * ( $x / $y );
  88. }
  89. //resize the image to an output image
  90. $output = imagecreate( $new_x, $new_y );
  91. imagecopyresized ( $output, $im, 0, 0, 0, 0, $new_x, $new_y, imagesx( $im), imagesy( $im) );
  92. //if the target directory doesn't exist create it
  93. if( !file_exists( $dest_parts['dirname'] ) ) {
  94. mkdir( $dest_parts['dirname'] );
  95. }
  96. //save the image and destroy the two working images
  97. //NOTE: if an output file is specified but is in a folder which isn't prefixed with "_", the image will be found by the next scan operation and a new image object will be created.
  98. $raw_file = $dest_parts['dirname'].'/'.$dest_parts['basename'];
  99. if( $this->image_type == 'jpg' && imagetypes() & IMG_GIF ) {
  100. $newpath = $raw_file;
  101. imagejpeg( $output, $newpath, 80 );
  102. } else if( $this->image_type == 'gif' && imagetypes() & IMG_GIF ) {
  103. $newpath = $raw_file;
  104. imagegif( $output, $newpath );
  105. } else if( $this->image_type == 'png' && imagetypes() & IMG_GIF ) {
  106. $newpath = $raw_file;
  107. imagepng( $output, $newpath );
  108. } else {
  109. echo "REQUESTED IMAGE FILE TYPE NOT SUPPORTED BY SERVER";
  110. }
  111. //register the derived image if specified
  112. if( $newpath && $derivative_name ) {
  113. $this->derivative_paths[$derivative_name] = $newpath;
  114. }
  115. imagedestroy( $im );
  116. imagedestroy( $output );
  117. }
  118. }
  119. function delete( $delete_file = TRUE ) {
  120. //remove resource if local
  121. if( $delete_file && $this->resource_type == 'file' && file_exists( $this->path) ) {
  122. unlink($this->path);
  123. }
  124. if( $this->derivative_paths ) {
  125. foreach( $this->derivative_paths as $path ) {
  126. if( file_exists( $path ) ) {
  127. unlink( $this->path );
  128. }
  129. }
  130. }
  131. //remove from DB
  132. parent::delete();
  133. }
  134. function get_object_table( $fieldArray = NULL) {
  135. if( $this->id == '*new*' ) {
  136. $return = new HTMLElement( 'div' );
  137. $return->set_attribute( 'class', 'two_column_container' );
  138. $browser =& $return->add_element( file_browser( false, true, NULL, true ) );
  139. $browser->set_attributes(array( 'id'=>'file_browser', 'class'=>'left_pane' ));
  140. $instructions =& $browser->add_element( new HTMLElement( 'p', 'select a destination folder for uploaded images' ), 0 );
  141. $uploader =& $return->add_element( uploader( true ) );
  142. $uploader->set_attributes(array( 'id'=>'uploader','class'=>'right_pane' ));
  143. return $return->to_html();
  144. } else {
  145. parent::get_object_table( $fieldArray );
  146. }
  147. }
  148. function get_title() {
  149. //instead of overloading the whole display function, i've just overloaded the title function and added code for displaying the image
  150. $returnString = '<h1>'.$this->type->get_attribute( 'type_name' ).'> '.$this->get_index().'</h1>';
  151. if( isset( $this->path ) ) {
  152. $returnString .= '<p>';
  153. if( $this->resource_type == 'file' ) {
  154. //format local files for URI syntax
  155. $returnString .= '<img src="'.file_to_html($this->path).'" />';
  156. } else if( $this->resource_type == 'href' ) {
  157. //insert path for links
  158. $returnString .= '<img src="'.$this->path.'" />';
  159. } else {
  160. //for anything else show a link
  161. $returnString .= '<p><a href="'.$this->path.'">image file</a>';
  162. }
  163. $returnString .= '</p>';
  164. }
  165. if( isset( $this->description ) ) {
  166. $returnString .= '<p>'.$this->description.'</p>';
  167. }
  168. $returnString .= '<p>';
  169. return $returnString;
  170. }
  171. }
  172.  
  173. function uploader( $returnElement = false ) {
  174. $return = new HTMLElement( 'div' );
  175. $instructions = $return->add_element( new HTMLElement( 'p', 'enter the files to be uploaded or click \'browse\' to select the location of the files on your computer' ) );
  176. for($i=0;$i<6;$i++) {
  177. $input =& $return->add_element( new HTMLElement( 'input' ) );
  178. $input->set_attributes( array( 'type'=>'file', 'name'=>'file_'.$i ) );
  179. $return->add_element( new HTMLElement( 'br' ) );
  180. }
  181. $submit =& $return->add_element( new HTMLElement( 'input' ) );
  182. $submit->set_attributes(array( 'type'=>'submit', 'value'=>'upload' ));
  183. return $returnElement?$return:$return->to_html();
  184. }
  185.  
  186. ?>

Documentation generated on Tue, 24 May 2005 03:57:09 -0400 by phpDocumentor 1.3.0RC3