Documentation is available at classes_image.php
- <?php
- class image extends b_resource {
- //base resource definition
- var $image_type;
- var $derivative_paths;
- function image( $constructID = NULL ) {
- parent::b_resource( $constructID, 'images' );
- $this->derivative_paths = unserialize( $this->derivative_paths );
- $this->image_type = $this->set_image_type();
- }
- function get_index() {
- if( $this->name ) {
- return $this->name;
- } else if( $this->path ) {
- $path_parts = pathinfo($this->path);
- return $path_parts['basename'];
- } else {
- return 'upload images';
- }
- }
- function verify_set( $post ) {
- //catch uploaded images and their destination folder
- foreach($post as $key => $value){
- if( strstr( $key, 'f_browser_' ) && $value ) {
- //catch destination folder
- $IDs[] = substr( $key, ( strrpos( $key, '_' ) + 1 ) );
- unset( $post[$key] );
- }
- }
- //catch uploaded images
- if( $IDs && count( $IDs ) == 1 ) {
- if( count($_FILES) ) {
- //retrieve the destination folder
- $dest =& get_object( $IDs[0] );
- //copy the uploaded files to the destination folder
- foreach( $_FILES as $file ) {
- if( $file['name'] ) {
- $uploadfile = $dest->path.'/'.basename($file['name']);
- if (!move_uploaded_file($file['tmp_name'], $uploadfile)) {
- $returnArray[] = 'uploaded image is corrupt';
- }
- }
- }
- //update the folder definition
- $dest->scan();
- } else {
- $returnArray[] = 'no images uploaded';
- }
- }
- //pass post data on for more verification
- if( !count($returnArray) ) {
- parent::verify_set($post);
- } else {
- $GLOBALS['errorArray'] = $returnArray;
- $this->set_object( $post );
- }
- }
- function set_image_type() {
- if( stristr( $this->path, '.jpg' ) ) { return 'jpg'; }
- else if( stristr( $this->path, '.gif' ) ) { return 'gif'; }
- else if( stristr( $this->path, '.png' ) ) { return 'png'; }
- }
- function resize_proportional( $max_width = NULL, $max_height = NULL, $dest_path, $derivative_name = NULL ) {
- //resize the given image to fit in the given sized box, then save to the destination file
- $src_parts = pathinfo( $this->path );
- $dest_parts = pathinfo($dest_path);
- //load the image
- if( $this->image_type == 'jpg' ) {
- $im = imagecreatefromjpeg( $this->path );
- } else if( $this->image_type == 'gif' ) {
- $im = imagecreatefromgif( $this->path );
- } else if( $this->image_type == 'png' ) {
- $im = imagecreatefrompng( $this->path );
- }
- //if that went ok
- if( isset( $im ) ) {
- //determine the output size based on max width and heights
- $x = imagesx( $im );
- $y = imagesy( $im );
- if( $max_width * ( $y / $x ) <= $max_height ) {
- $new_x = $max_width;
- $new_y = $max_width * ( $y / $x );
- } else {
- $new_y = $max_height;
- $new_x = $max_height * ( $x / $y );
- }
- //resize the image to an output image
- $output = imagecreate( $new_x, $new_y );
- imagecopyresized ( $output, $im, 0, 0, 0, 0, $new_x, $new_y, imagesx( $im), imagesy( $im) );
- //if the target directory doesn't exist create it
- if( !file_exists( $dest_parts['dirname'] ) ) {
- mkdir( $dest_parts['dirname'] );
- }
- //save the image and destroy the two working images
- //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.
- $raw_file = $dest_parts['dirname'].'/'.$dest_parts['basename'];
- if( $this->image_type == 'jpg' && imagetypes() & IMG_GIF ) {
- $newpath = $raw_file;
- imagejpeg( $output, $newpath, 80 );
- } else if( $this->image_type == 'gif' && imagetypes() & IMG_GIF ) {
- $newpath = $raw_file;
- imagegif( $output, $newpath );
- } else if( $this->image_type == 'png' && imagetypes() & IMG_GIF ) {
- $newpath = $raw_file;
- imagepng( $output, $newpath );
- } else {
- echo "REQUESTED IMAGE FILE TYPE NOT SUPPORTED BY SERVER";
- }
- //register the derived image if specified
- if( $newpath && $derivative_name ) {
- $this->derivative_paths[$derivative_name] = $newpath;
- }
- imagedestroy( $im );
- imagedestroy( $output );
- }
- }
- function delete( $delete_file = TRUE ) {
- //remove resource if local
- if( $delete_file && $this->resource_type == 'file' && file_exists( $this->path) ) {
- unlink($this->path);
- }
- if( $this->derivative_paths ) {
- foreach( $this->derivative_paths as $path ) {
- if( file_exists( $path ) ) {
- unlink( $this->path );
- }
- }
- }
- //remove from DB
- parent::delete();
- }
- function get_object_table( $fieldArray = NULL) {
- if( $this->id == '*new*' ) {
- $return = new HTMLElement( 'div' );
- $return->set_attribute( 'class', 'two_column_container' );
- $browser =& $return->add_element( file_browser( false, true, NULL, true ) );
- $browser->set_attributes(array( 'id'=>'file_browser', 'class'=>'left_pane' ));
- $instructions =& $browser->add_element( new HTMLElement( 'p', 'select a destination folder for uploaded images' ), 0 );
- $uploader =& $return->add_element( uploader( true ) );
- $uploader->set_attributes(array( 'id'=>'uploader','class'=>'right_pane' ));
- return $return->to_html();
- } else {
- parent::get_object_table( $fieldArray );
- }
- }
- function get_title() {
- //instead of overloading the whole display function, i've just overloaded the title function and added code for displaying the image
- $returnString = '<h1>'.$this->type->get_attribute( 'type_name' ).'> '.$this->get_index().'</h1>';
- if( isset( $this->path ) ) {
- $returnString .= '<p>';
- if( $this->resource_type == 'file' ) {
- //format local files for URI syntax
- $returnString .= '<img src="'.file_to_html($this->path).'" />';
- } else if( $this->resource_type == 'href' ) {
- //insert path for links
- $returnString .= '<img src="'.$this->path.'" />';
- } else {
- //for anything else show a link
- $returnString .= '<p><a href="'.$this->path.'">image file</a>';
- }
- $returnString .= '</p>';
- }
- if( isset( $this->description ) ) {
- $returnString .= '<p>'.$this->description.'</p>';
- }
- $returnString .= '<p>';
- return $returnString;
- }
- }
- function uploader( $returnElement = false ) {
- $return = new HTMLElement( 'div' );
- $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' ) );
- for($i=0;$i<6;$i++) {
- $input =& $return->add_element( new HTMLElement( 'input' ) );
- $input->set_attributes( array( 'type'=>'file', 'name'=>'file_'.$i ) );
- $return->add_element( new HTMLElement( 'br' ) );
- }
- $submit =& $return->add_element( new HTMLElement( 'input' ) );
- $submit->set_attributes(array( 'type'=>'submit', 'value'=>'upload' ));
- return $returnElement?$return:$return->to_html();
- }
- ?>
Documentation generated on Tue, 24 May 2005 03:57:09 -0400 by phpDocumentor 1.3.0RC3