Zend - The PHP Company




Graphics

Add Code


Dynamic image preview end easy info retrieve.  

Type: class
Added by: buffaloBuff
Entered: 24/01/2002
Last modified: 01/12/2001
Rating: - (fewer than 3 votes)
Views: 7763
Class that utilizes gd library to retrieve image info and preview an image dynamically. Also have a look at the example code


<?

/* Author:     Johan Hultgren                                                     */
/* E-mail:     johan@6thgear.se                                                 */
/* Copyright:    ...nah                                                         */

class ImgUtil {
    var 
$max_height;
    var 
$max_width;
    var 
$thePath;

/* Path can be set in the constructor                                         */
    
function ImgUtil($str NULL) {
        if(
$str != NULL$this->thePath $str;
        else 
$this->thePath NULL;
    }
    
/* ...otherwise use this method                                             */
    
function set_path($path) {
        
$this->thePath $path
    }

/* Retrive path from object if set                                             */
    
function get_path() {
        if(
$this->thePath == NULL)
            return 
"No path set.";
        else
            return 
$this->thePath
    }

/*  Get formated info regarding the image. Usage:                             */
/*             img_info((string) str)                                             */
/*             str = "w"        returns image width                             */
/*             str = "h"        returns image height                             */
/*             str = "s"        returns image formated file size(byte, kb, Mb)     */
/*             str = "t"        returns image type (GIF, JPEG, PNG, SWF)         */
/*             str = "w x w"    returns width x height                             */
/*             etc...                                                             */

    
function img_info($str) {
        if(!
$str) return "Specify return string: 't', 'w', 'h', 'w x h', etc.";
        else 
            if(
$this->thePath " ") return "No path set.";
        else {
            if(
$imgArr getimagesize($this->thePath)) {
                switch(
$imgArr[2]) {
                    case 
1:
                        
$type "GIF";
                        break;
                    case 
2:
                        
$type "JPEG";
                        break;
                    case 
3:
                        
$type "PNG";
                        break;
                    case 
4:
                        
$type "SWF";
                        break;
                }
                
$size filesize($this->thePath);
                if (
$size 1000000) {
                    
$size $size/1000000;
                    
$size $size." Mb";
                } else if (
$size 1000) {
                    
$size $size/1000;
                    
$size $size." Kb";
                } else
                    
$size $size." byte";
    
                
$w $imgArr[0];
                
$h $imgArr[1];
                
$t $type;
                
$s $size;
                
                
$str str_replace("w"$w$str);
                
$str str_replace("h"$h$str);
                
$str str_replace("t"$t$str);
                
$str str_replace("s"$s$str);
                
                return 
$str;
            } 
            else return 
"No image file.";
        }
    }
    
/* Method to set maximum image preview size                                     */
    
function set_max_preview_size($width$height) {
        
$this->max_width $width;
        
$this->max_height $height;
    }
/* Image preview method. Variables max_width, max_height and thePath must     */
/* be set                                                                     */
    
function img_preview() {
        if(
$this->max_width == NULL || $this->max_height == NULL)
            return 
"Variables max_width and max_height has to be set.";
        else if(
$this->thePath " ") echo "Path has to be set.";
        else {
            if(
$imgArr getimagesize($this->thePath)) {
                if(
$imgArr[0] > $imgArr[1]) {
                    if(
$imgArr[0] <= $this->max_width)
                        return 
'<img src="'.$this->thePath.'" '.$imgArr[3].'>';
                    else return 
'<img src="'.$this->thePath.'"width="'.$this->max_width.'" height="'.($this->max_width/$imgArr[0])*$imgArr[1].'">';
                }
                else if(
$imgArr[0] < $imgArr[1]) {
                    if(
$imgArr[1] <= $max_height)
                        return 
'<img src="'.$this->thePath.'" '.$imgArr[3].'>';
                    else return 
'<img src="'.$this->thePath.'"height="'.$this->$max_height.'" width="'.($this->max_height/$imgArr[1])*$imgArr[0].'">';
                }
                else
                    return 
'<img src="'.$this->thePath.'" '.$imgArr[3].'>';
            }
        }
    }


}
?>


Usage Example


<?
require("./classes/ImgUtil.class"); // relative classpath

// In this case set the directory you wish to list
$path "./images/";

// Create an empty ImgUtil object
$img = new ImgUtil();

// Setting maximum preview size:
$img->set_max_preview_size("200","150"); // width, height

// If file chosen, set its path
if(isset($img_chosen)) $img->set_path($img_chosen);

// If erase button is clicked, unlink the file
if(isset($img_chosen) && isset($erase)) {
    if(
unlink("".$img->get_path()))
        echo 
'<div align="center" class="arial_11_normal_svart"><font color="#CC0000">Image <b>'.basename($img_chosen).'</b> erased.</font></div>';
    
$img_chosen NULL;
}            
?>
<html>
<body>
    <table width="564" height="100" cellpadding="0" cellspacing="0">
        <tr>
        <form name="file_form" action="<? echo $PHP_SELF?>" method="post">
            <td width="227" valign="top">
            <b>Administrate images.</b><br><br>
            Choose the imge you want to erase or get information for.<br><br>
            <select name="img_chosen" onchange="javaScript:document.file_form.submit();">
            <option disabled>Choose image here</option>


<?    
// File chooser, got to have a file for the example    =)                
            
$handle opendir($path);
            while (
$file readdir($handle)) {
                if (
$file != "." && $file != "..") {
                    if (
is_dir($file) == FALSE && is_link($file) == FALSE) {
                        if (
basename($img_chosen) == $file)
                            echo 
'<option value="'$path.$file.'" selected>'.$file.'</option>';
                        else 
                            echo 
'<option value="'$path.$file.'">'.$file.'</option>';
                    }
                }
            }
            
closedir($handle);
?>    
            </select><br><br>
<?
// Displaying some info regarding the chosen image
        
if (isset($img_chosen)) {
            echo 
'Filename: <b>'.basename($img_chosen).'</b><br>';
            
// Derives info from object using its img_info method            
            
echo 'File size (bytes): '.$img->img_info("s").'<br>';
            echo 
'Image type: '.$img->img_info("t").'<br>';
            echo 
'Dimensions (w x h): '.$img->img_info("w x h").'<br>';
        }
?>
            </td>
              <td width="20">
                  &nbsp;
              </td>
              <td width="227" valign="bottom">
<?     

    
if (isset($img_chosen)) {
        echo 
'Preview.<br><table bgcolor="#cc0000" cellpadding="1" cellspacing="0"><tr><td>
                <table width="227" height="150" bgcolor="#CCCCCC"><tr><td align="center" valign="middle">
                '
// Previewing image using the objects img_preview method
                
.$img->img_preview().
                
'
                </td></tr></table></td></tr></table><br><br>
                <input type="submit" name="erase" value="erase image">'
;
    }
?>
            </td>
        </form>
        </tr>
    </table>    
</body>
</html>


Rate This Script





Search



This Category All Categories