SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to turn image/thumbnail function into a class

    I have used a function to handle images and thumbnails for a while which has proved quite useful. You can have non-technical users upload images which are the rescaled or resized on the server etc. To increase reusability of this function, i would like to redefine it in an object-oriented way. As I am quite new to practical oo programming, can you give me a few hints about how to design this class? But first, I'll post the function(s) I have used so far (feel free to use it if you want):

    PHP Code:
     
    /* IMAGE & THUMBNAIL LIBRARY BY MK
    ** v.0.5.1
    */

    function ScaleImage ($src_width$src_height$max_width)
    /* Scales an image to a given max width and computes
    ** the correct height
    **
    ** INPUT: source width, source height and max width of destination img
    ** OUTPUT: an array containing destination width and height
    */
    {
    $dst_width $src_width;
    $dst_height $src_height
    if (
    $src_width $max_width) { // if img is to big, scale it down to max_width
    $image_prop=($max_width*100)/$src_width;
    $image_vsize= ($src_height*$image_prop)/100 ;
    $dst_width $max_width
    $dst_height ceil($image_vsize);
    }

    $result = array ("dst_width"=>"$dst_width""dst_height"=>"$dst_height); 
    return 
    $result;
    }


    function 
    CreateErrorImage ($img_name$width
    /* creates a square image of the given width
    ** with an error message stating that
    ** the image with the name img_name is
    ** not a valid jpg/rgb file
    **
    ** INPUT: name of original image and width of the error img 
    ** OUTPUT: a resource handle for the created error image
    */
    {
    $im imagecreate ($width$width); /* Create a blank image */
    $bgc imagecolorallocate ($im255255255);
    $tc imagecolorallocate ($im000);
    imagefilledrectangle ($im0015030$bgc);
    /* Output an errmsg */
    imagestring ($im155"$img_name:"$tc);
    imagestring ($im1515"Not found"$tc);
    imagestring ($im1525"or not a vaild"$tc);
    imagestring ($im1535"JPG/RGB file!"$tc);
    return 
    $im;
    }


    function 
    CreateThumbnail($path$img$tn_width$err_img 1
    {
    /*
    ** Takes a jp(e)g file ($img) in the $path and 
    ** checks if it valid (it must be a jpg in rgb mode).
    ** If the original image is valid and there is no 
    ** thumbnail yet, it creates a correctly sized thumbnail of $tn_width
    ** and stores it in the same directory 
    ** by prepending the prefix "tn_" to the filename.
    **
    ** INPUT: absolute path (with or w/o trailing slash), name of original image,
    ** width of thumbnail and whether to create an error image when $img is not valid
    ** (otherwise a simple error message will be printed out)
    ** OUTPUT: an array containing width, height and name of the thumbnail
    *
    ** NB: If the original image is not valid, the script will not exit.
    ** Instead, in order not to disturb the design of the web page concerned,
    ** the script will create 
    ** a) an error image and will return an array with the the corresponding info
    ** or
    ** b) an error message and will return a result array with empty values
    **
    ** dependencies: needs function ScaleImage & function CreateErrorImage
    */
    //cut trailing slash in path name (if any)
    if (substr ($path, -1) == '/'$path substr ($path0strlen($path)-1);


    if (!(
    is_writable ($path))) {
    die (
    "Error: Path $path is not writable! " );
    }
    // determine src img width and height and
    // check if it is a valid jpg/rgb file

    $image_hw = @GetImageSize("$path/$img);
    $src_width $image_hw[0];
    $src_height $image_hw[1];
    $img_is_jpg = ($image_hw[2] == 2); // Type: JPG
    $jpg_is_rgb = ($image_hw['channels'] == 3) || ($image_hw['channels'] == 1); //mode: RGB or grey scale
    $name_ok eregi(".+\.jp[e]{0,1}g$"$img );

    if (!(
    $img_is_jpg) || !($jpg_is_rgb) || !($image_hw) || !($name_ok)) {

    if (!
    $err_img) {
    echo (
    "<strong>Fehler</strong>: \"$img\" is not a valid JPG/RGB file!" );
    $result = array ("dst_width"=>"0""dst_height"=>"0""thumb_name"=>"" );
    }
    else {

    $im CreateErrorImage($img$tn_width);
    $thumb_name "tn_error_$img";
    if (!
    imagejpeg ($im"$path/$thumb_name"90)) {
    ImageDestroy($im);
    die (
    "Error image could not be created! (probably incorrect access rights)" );

    ImageDestroy($im);
    $result = array ("dst_width"=>"$tn_width""dst_height"=>"$tn_width""thumb_name"=>"$thumb_name);
    }

    return 
    $result;
    }

    $scaled_image ScaleImage ($src_width$src_height$tn_width);
    extract ($scaled_image);

    $prefix "tn_";
    $thumb_name $prefix $img;

    if(!
    file_exists("$path/$thumb_name)) {


    $src_img imagecreatefromjpeg("$path/$img);
    $dst_img imagecreatetruecolor($dst_width,$dst_height);
    imagecopyresampled($dst_img$src_img0000$dst_width$dst_height$src_width$src_height);
    if (![
    email=@imagejpeg($dst_img]!@imagejpeg($dst_img[/email], "$path/$thumb_name"90)) {
    echo (
    "Error creating thumbnail file (probably incorrect access rights)" );
    }
    imagedestroy($src_img);
    imagedestroy($dst_img);
    chmod("$path/$thumb_name"octdec(766));
    }

    $result = array ("dst_width"=>"$dst_width""dst_height"=>"$dst_height""thumb_name"=>"$thumb_name);
    return 
    $result;
    // func 
    I might use it like this (unchecked code):

    PHP Code:

    // define the upload path where stored images are too be found
    // define a width for thumbnails
    // do some db query and get an image name 

    if ($img_name) {
    $tn_image_info CreateThumbnail ($img_path$img_name$max_width);
    extract ($tn_image_info); 
    echo (
    "
    <span style=\"float: left\">
    <img class=\"border\" src=""\"
    $img_url/" rawurlencode($thumb_name) . "\" alt=\"$uniquebandname\" width=\"$dst_width\" height=\"$dst_height\" hspace=\"10\" />
    </span>" 
    );

    Now, where to start? A few constraints and things I want to be able to do with an Image object:

    1) i get an image name, e.g. from a db query and construct an Image object using this information, e.g. $image = new Image($name, $path);
    2) maybe there is a corresponding file on the server,maybe not (I may have deleted it before because i only needed the thumbnail)
    3) maybe there is already a thumbnail file, maybe not
    4) i need to create error images when necessary

    i) create a thumbnail (file)
    ii) delete a thumbnail (file)
    iii) check if there is a thumbnail (file)
    iv) check if there is a corresponding (original) image file
    v) delete the (original) image file
    vi) create and delete an error image
    vii) set and get the name of an image (really necessary?)
    vii) set a thumbnail name prefix (necessary?)
    viii) rescale an image (w/o creating a thumbnail)

    Should i make a class Thumbnail that extends Image? Or is 1 class sufficient here? Should i return an Image object when creating a thumbnail? And what about "error images" (they are essentially thumbnails with an error image)?

    I imagine a public properties like "hasThumbnail" or "hasFile". Should i also provide a corresponding accessor method like "checkhasThumbail"? But if i do, is the "hasThumbnail" property not superfluous? Should I leave it out then or simply consider it as a private property?

    Can somebody outline a possible interface for this class please? I just need some sort of kick start, as I'm not shure how "original" image files, thumbnails and error images should interact here.
    Last edited by mkrz; Apr 17, 2003 at 12:35.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excuse me ? I am not going to attemp to sort through this posted script mate.

    Post the script again though use
    PHP Code:
     ... 
    instead so all the script is properly formatted and colour syntaxed.

    Thank you LoL

    -- EDIT -- Sorted 8)

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dont think you'd need a child class for 'hasThumbnail', though you'd need one class to set/get the thumbnail first.

    Second a class to report/log errors - such as image is not there or wrong image type for example.

    Your image resizing and actual creation of the thumbnail would proberly extend (or be extended to) the first part - checking image existence/type.

    BTW - first post was in relation to sitepoint's forum not working properly (again) - your script wasn't properly displayed 8(

    It's annoying now and it's only begun to happen since they upgraded their vBulletin - it's still in Beta and it shows...

  4. #4
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    BTW - first post was in relation to sitepoint's forum not working properly (again) - your script wasn't properly displayed 8(
    Sorry, this was actually my own fault as i didn't know that using the "code" marker was not enough here. Figured it out a minute later.

    Dont think you'd need a child class for 'hasThumbnail', though you'd need one class to set/get the thumbnail first.
    I can't really imagine using a "Child class" for a property? Did you mean "method"?

    And can i not actually consider a thumbnail to be an "ordinary" image? But maybe not, i realize just now, because it doesn't really make sense to create a thumbnail for a thumbnail...

    Mmh, perhaps it's better to create one generic Image class and extend it with sth. like "OriginalImage" (with additional methods like "create thumbnail") and "ThumbnailImage"?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your suggestion would work - my actual OOP design skills are limited as well.

    It's strange - I can sit down and put together a class that conformes to proper OOP within a few hours but ask me to do the paper work before hand and I defy gravity 8)

    On the note of formatting your script - at least you managed to fix it 8)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •