SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2003
    Location
    P Town
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A couple GD image questions

    I have been working on the below snippet of code I found some place and have a couple questions on it since I am not real femiliar with the GD image commands.

    1. How would I define a custom path to save the pictures in instead of just putting the finished pictures in the same directory as the php file.

    2. How could I use the original file name of the picture and store that in the database rather than renaming the file.

    This is what I am working with
    Code:
    <?php include 'db_connect.php'; ?>
    <html>
    <head>
    <title>Upload a picture to your article</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    $articleid = $artid;
    
    //begin photo upload code
    if (isset($_POST['photosubmit'])) {
    // Filename to store image as (no extention)
    $FILENAME= rand(0,999999999);
    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=300;
    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=400;
    //start image function
    function ResizeImage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
     if($maxwidth && $width > $maxwidth){
      $widthratio = $maxwidth/$width;
      $RESIZEWIDTH=true;
     }
     if($maxheight && $height > $maxheight){
      $heightratio = $maxheight/$height;
      $RESIZEHEIGHT=true;
     }
     if($RESIZEWIDTH && $RESIZEHEIGHT){
      if($widthratio < $heightratio){
       $ratio = $widthratio;
      }else{
       $ratio = $heightratio;
      }
     }elseif($RESIZEWIDTH){
      $ratio = $widthratio;
     }elseif($RESIZEHEIGHT){
      $ratio = $heightratio;
     }
        $newwidth = $width * $ratio;
           $newheight = $height * $ratio;
     if(function_exists("imagecopyresampled")){
           $newim = imagecreatetruecolor($newwidth, $newheight);
           imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
     }else{
      $newim = imagecreate($newwidth, $newheight);
           imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
     }
           ImageJpeg ($newim,$name . ".jpg");
     ImageDestroy ($newim);
    } else {
     ImageJpeg ($im,$name . ".jpg");
    }
    }
    
    //end image function
    if($_FILES['image']['size']){
    if($_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
     $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
    }elseif($_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
     $im = imagecreatefrompng($_FILES['image']['tmp_name']);
    }elseif($_FILES['image']['type'] == "image/gif"){
     $im = imagecreatefromgif($_FILES['image']['tmp_name']);
    }
    if($im){
     if(file_exists("$FILENAME.jpg")){
      unlink("$FILENAME.jpg");
     }
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
        ImageDestroy ($im);
    }
    //Submit picture
    
    
    $insert = "INSERT INTO pictures (
    article_ref,
    pic_name) 
    VALUES (
    '$articleid', 
    '".$FILENAME.".jpg')";
    
    $article_pic = $db_object->query($insert);
    
    if (DB::isError($article_pic)) {
     die($article_pic->getMessage());
    }
    
    $db_object->disconnect();
    
    ?>
    <B>Picture added successfully!</B> You can either close this window or upload more pictures. 
    
    <?php
    
    }
    }
    //end upload photo
    ?>
    
    <form enctype="multipart/form-data" method="post">
    <b>Upload Image</b><br>
    <input type="file" name="image" size="50"><br>
    <input type="submit" name="photosubmit" value="Upload Photo" onclick='refreshIframe()'>
    </form>
    
    </body>
    </html>
    Thanks a million for any help on this problem!!!

  2. #2
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Setting name to actual file name uploaded:
    PHP Code:
    $picname $HTTP_POST_FILES['upload']['name'];
    //The ['upload'] is the field name from the upload portion of your form 
    To store the name of the file in the database just do an INSERT with the column that holds the filename and '$picname' as the value.

    Setting the path to save the file to:
    PHP Code:
    $path "../images/images/";
    $res copy($HTTP_POST_FILES['upload']['tmp_name'], $path .
    //The above is the initial upload.
    //Below is for a resized version that is created
    $new_name "../images/images/t_".$HTTP_POST_FILES['upload']['name'];
    $src_img imagecreatefromjpeg($HTTP_POST_FILES['upload']['tmp_name']);
    $new_img imagecreatetruecolor($maxwidth$new_height);
    imagecopyresized($new_img$src_img0,0,0,0,$maxwidth$new_heightimagesx($src_img),imagesy($src_img));
    imagejpeg($new_img$new_name80); 
    Hope this helps!
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  3. #3
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for clarity. On the resized version..

    $new_name : This is the name of the resized file. As you will notice I've put a 't_' this just adds the prefix to the actual filename.

    $new_img : This is the resized image created.

    imagejpeg($new_img, $new_name, 80); : This is basically in the following order (1) the image itself; (2) the path and file name where it is saved; (3) the jpeg quality.

    I'm sure you may know the above but I just wanted to be sure!
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2003
    Location
    P Town
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks KD your awsome!

  5. #5
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad it helped! Thanks for the comment!
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!


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
  •