Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    move image to another folder and write path to database

    when i upload a image it gets sent to my folder called images a script is run called thumb save and creates a thumbnail of that original file but places it in the same folder. what i need it to do is get the thumbnail and move it to a folder called thumbnails or something similiar and then write the path of the thumbnail in the database where it is stored.

    the actual large image has its path stored in the database already. but i dont know how to move the thumbnail to the folder and create the image path in the database. ive tried adding my own target path and changing a few things but kept coming up with errors. so not to sure where to go from there. the image path would be just an extra row in my images table so it would have the same gallery_id as the large image and then i can call it in my loop to display them. please help!

    thanks for listning

    here is the thumbnail script:

    PHP Code:
    <?php

    // $image, $maxHeight, $maxWidth

    $pinfo pathinfo($image);

    $tmb_name $pinfo['dirname'].'/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

    if(!isset(
    $maxWidth)){

        
    $maxWidth 100;

    }

    if(!isset(
    $maxHeight)){

        
    $maxHeight 150;

    }

    switch(
    strtolower(substr($image, -3))) {

    case 
    "jpg" :

        
    $fileType "jpeg";

        
    $imageCreateFunction "imagecreatefromjpeg";

        
    $imageOutputFunction "imagejpeg";

        break;

    case 
    "jpeg" :

        
    $fileType "jpeg";

        
    $imageCreateFunction "imagecreatefromjpeg";

        
    $imageOutputFunction "imagejpeg";

        break;

    case 
    "png" :

        
    $fileType "png";

        
    $imageCreateFunction "imagecreatefrompng";

        
    $imageOutputFunction "imagepng";

        break;

    case 
    "gif" :

        
    $fileType "gif";

        
    $imageCreateFunction "imagecreatefromgif";

        
    $imageOutputFunction "imagegif";

        break;

    }

    $size GetImageSize($image);

    $originalWidth $size[0];

    $originalHeight $size[1];

    $x_ratio $maxWidth $originalWidth;

    $y_ratio $maxHeight $originalHeight;

    // check that the new width and height aren't bigger than the original values.

    // the new values are higher than the original, don't resize or we'll lose quality

    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {

        
    $newWidth $originalWidth;

        
    $newHeight $originalHeight;

    } else if ((
    $x_ratio $originalHeight) < $maxHeight) {

        
    $newHeight ceil($x_ratio $originalHeight);

        
    $newWidth $maxWidth;

    } else {

        
    $newWidth ceil($y_ratio $originalWidth);

        
    $newHeight $maxHeight;

    }

    $src $imageCreateFunction($image);

    $dst imagecreatetruecolor($newWidth$newHeight);

    // Resample

    imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);

    // Save image

    $imageOutputFunction($dst$tmb_name);

    ImageDestroy($src);

    ImageDestroy($dst);

    ?>
    it gets the path for the image from this file:

    PHP Code:
    <?php

     $max_size
    =5*1024*1024;

     
    // Check if a file has been uploaded

    if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)

    {

         
    // Make sure the file was sent without errors

         
    if($_FILES['uploaded_file']['error'] == 0)

         {

            
    $target_path "images/";

            
    $target_path $target_path basename$_FILES['uploaded_file']['name']);     

            if(!
    file_exists($target_path)){

        if(
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))

            {

                
    $image $target_path;

                
    $maxHeight 50$maxWidth 50;

                include 
    'thumb_save.php'// Save a thumb of uploaded pic 

                
    echo "The file ".  basename($_FILES['uploaded_file']['name']). " has been uploaded";

                
    $dbLink = new mysqli('localhost''root''''gallery');

                 if(
    mysqli_connect_errno()) {

                die(
    "MySQL connection failed: "mysqli_connect_error());

                                            }             

                  
    // Gather all required data

                  
    $name $dbLink->real_escape_string($_FILES['uploaded_file']['name']);

                  
    $mime $dbLink->real_escape_string($_FILES['uploaded_file']['type']);

                  
    $size intval($_FILES['uploaded_file']['size']);

                  
    $image_path $dbLink->real_escape_string($target_path);

                  
    $gallery_type $dbLink->real_escape_string($_POST['gallery_type']);

                 
    $desc $dbLink->real_escape_string($_POST['desc']);

                 
    $image_path $dbLink->real_escape_string($target_path);

                 
    //query to insert the data i had gathered into the database

                 
    $query "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc` )

                 VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}')"
    ;

                 
    //executes the query

                 
    $dbLink->query($query);

            }

        }

        else 

             {

                echo 
    'A file with the same name exists please change the file name and try again';

              }

    }

      else

              {

          echo 
    'A file was not sent';

            }

    }

    else

              {

          echo 
    'The file is too large';

            }

     
    // Echo a link back to the main page

     
    echo '<p>Click <a href="member-index.php">here</a> to go back</p>'

     
    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    how would that help? i want to keep the thumbnail the same name and at the end of it there is _tmb which gets added already. i just need that to be moved to a folder and written where the path is on the database

  • #4
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Check out the link from Fumigator.
    He knows his stuff.

    (The point is that you can "rename" it into whichever dir you desire. )

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i did this instead as it works just as good. thanks for the link. everything im doing is a learning curve.

    PHP Code:
    $tmb_name $pinfo['dirname'].'/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

    // to...

    $tmb_name $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; 
    my only problem is i cant seem to display my thumbnail images:

    this is how i create them on the fly:

    PHP Code:
    while ($row $result->fetch_assoc()) {

    echo 
    "<a href=\"image.php?id=" $row['id'] . "\">";
    echo 
    "<img src=\"imageResize.php?imageFilename=" $row['name'] . "&maxHeight=90&maxWidth=150\" />\n";
    echo 
    "</a>\n";


    but this is what i thought it would be to display the thumbnail images:

    PHP Code:
    while ($row $result->fetch_assoc()) {

    echo 
    "<a href=\"image.php?id=" $row['id'] . "\">";
    echo 
    "<img src=\"" $row['thumbnail_path'" />\n";
    echo 
    "</a>\n";


    but i was wrong and got errors. any ideas?

  • #6
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    rename() is the way to go. It will allow you to specify the old name and the new name and also a new location.

    I believe it is something like:

    rename (\images\filea.aaa, \images\images\filea.aaa);

  • #7
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry if this thread has long ben inactive but i seem to have a similar problem and i'm using rename function..

    i'm trying to make a function in order to move images to another folder, i'm using rename function but i have problems on getting it to work.
    this is what the error says..

    Warning: rename(wp-content/gallery/shirts-gallery/thumbs/thumbs_1288084826.jpg,wp-content/gallery/shirts-gallery/1288084826.jpg) [function.rename]: No error in E:\Xampp\xampp\htdocs\wordpress2\wp-content\plugins\nextgen-gallery\view\gallery.php on line 83

    can anyone please help? I don't understand the error

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It looks like maybe you're renaming a file to the same name which is throwing a warning but the. Wordpress is intercepting the warning to say it's not an error. (maybe )

  • #9
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, it is a wordpress problem. I tried it out of wordpress and it works! But my problem is that i need to get it to work in wordpress.. anyone know how? Thanks.


  •  

    Posting Permissions

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