SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict suzkaw's Avatar
    Join Date
    Apr 2002
    Location
    North Carolina
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help with GD and PHP.

    I am trying to upload a pic to a directory and make a thumbnail in of the image in another directory. Then write both names to the database. I have the single image upload working but not the thumbnail.

    Here is what I have:
    PHP Code:
    //################# UPLOAD IMAGE TO FOLDER #################

    if ($action == "upload")
    {
         if (
    $userfile AND $userfile != "none") {
    $handle=opendir($full_path);
    while (list(
    $key,$val) = each($accepted_types))

     if(!
    in_array($userfile_type$accepted_types))
    {
       {
    $invalidext=true;break;}
    }


    if (
    $userfile_size $maxsize) {echo "ERR: File too large";}

    elseif (
    $invalidext) {echo "ERR: Forbiden file extension";}

    else {

    $extt substr($userfile_name, -44);
    $spaceless_name time() . $extt;


    $file_and_path $full_path $spaceless_name;


    $userfile_name $spaceless_name;


    if(
    move_uploaded_file($userfile$file_and_path)){

    echo 
    "<table width=600 align=center bgcolor=ffffff><tr><td>Your image has been added \"$userfile_name\" .</td></tr></table>";

    } else {


    echo 
    "File upload failed!";


    }
            
    $strDescription addslashes(nl2br($txtDescription));

            
    $query "INSERT INTO tbl_Files (description,filename, filesize, filetype, owner, prop_num) values ('$strDescription', '$userfile_name', '$userfile_size', '$userfile_type', '$current_user', '$propnum')";
            if (!
    mysql_query ($query$link) )
                    {
            die (
    mysql_error());
                    }
              echo 
    " uploaded succesfully";
                    }

        }


                          } 
    Now I have found a neat little resizing function that I would like to place in this so it does the resizing. My question is where do I place it? I can't get it to work for anything.

    Also here is the function:
    PHP Code:
    <?php
    function createthumb($userfile_name,$filename,$new_w,$new_h){ 
    ____global $gd2
    ____$system=explode(".",$userfile_name); 
    ____if (preg_match("/jpg|jpeg/",$system[1])){ 
    ________$src_img=imagecreatefromjpeg($userfile_name); 
    ____
    ____if (preg_match("/png/",$system[1])){ 
    ________$src_img=imagecreatefrompng($userfile_name); 
    ________
        $old_x
    =imageSX($src_img); 
    ____$old_y=imageSY($src_img); 
    ____if ($old_x $old_y) { 
    ________$thumb_w=$new_w
    ________$thumb_h=$old_y*($new_h/$old_x); 
    ____
    ____if ($old_x $old_y) { 
    ________$thumb_w=$old_x*($new_w/$old_y); 
    ________$thumb_h=$new_h
    ____
    ____if ($old_x == $old_y) { 
    ________$thumb_w=$new_w
    ________$thumb_h=$new_h
    _______

    if ($gd2==""){ 
    ________$dst_img=ImageCreate($thumb_w,$thumb_h); 
    ________imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    ____}else{ 
    ________$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); 
    ________imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    ____

    ____if (preg_match("/png/",$system[1])){ 
    ________imagepng($dst_img,$filename); 
    ____} else { 
    ________imagejpeg($dst_img,$filename); 
    ____
    ____imagedestroy($dst_img); 
    ____imagedestroy($src_img); 


    ?>
    Thanks Eric.

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    personally I'd go with using an external script and calling it to page within the src parameter of an <img> tag. This way you never have to worry about sending headers after content.

    Where you have echoed out the <table>your image has been uploaded</table> you can add the img tag (if you don't want the image displayed, just do it at height="1" width="1" ). Also sling the database updating scrippy there. eg

    echo '<table><tr><td><img src="thumbnail.php?image_ref=' .$file_and_path. '"> cheers</td></tr></table>';
    $arg = mysql_query("INSERT INTO `images` (some stuff)");

    On a side note: slinging the database bit before the image create call and using the auto-increment id field to name the thumbnail can often help loads for assuring no files are named identically and hence overwritten.

    If you are GD1.6ing (or even 2+), you could check the link in my signature for some simple thumbnailing scripts. Note though that DW on Mac has a nasty habit of converting the spaces to underscores as noted in your scriptlet above (you would NEED to remove them)
    teckis - that's news to me.


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
  •