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 15 of 15
  1. #1
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unique Filename / Thumbnail Generation

    Hi,

    I was wondering if anyone would be able to advise me, I have a script which a user can fill out some simple fields and upload an image to the server the name of which is put into a mysql db. The script also generates a random name for the file (to avoid the same filename twice!)

    What I am trying to make it do is generate a thumbnail of the same random name it generates for the larger image and store it in a subdirectory on the server.. Which is the bit that is confusing me.. Could someone perhaps take a look at my code and see what I'm doing wrong!?

    Code:
    <?php
    //This is the directory where images will be saved 
    $target = "imagesr/";
    $target = $target . basename( $_FILES['photo']['name']);
    // Connects to your Database 
    $host = "localhost";
    $name = "name";
    $pass = "pass";
    $dbname = "dbname";
    $dbi = mysql_connect($host,$name,$pass) or
    die("I cannot connect to the database. Error :" . mysql_error());
    mysql_select_db($dbname,$dbi);
    //This gets all the other information from the form 
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $pic=($_FILES['photo']['name']);
    
    //random file stuff
    //This function separates the extension from the rest of the file name and returns it 
    function findexts ($filename) 
    { 
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
    } 
    //This applies the function to our file 
    $ext = findexts ($_FILES['photo']['name']) ; 
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
    $ran = rand () ;
    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";
    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "imagesrob/";
    //This combines the directory, the random file name, and the extension
    $target = $target . $ran2.$ext; 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    {
    echo "The file has been uploaded as ".$ran2.$ext;
    } 
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    //endofrandom
    
    //resizing image
    //Name you want to save your file as
    $save = '$ran2$ext';
    
    $file = '$ran2$ext'; 
    echo "Creating file: $save"; 
    $size = 0.45; 
    //header('Content-type: image/jpeg') ; 
    list($width, $height) = getimagesize($file) ; 
    $modwidth = $width * $size; 
    $modheight = $height * $size; 
    $tn = imagecreatetruecolor($modwidth, $modheight) ; 
    $image = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
    
    // Here we are saving the .jpg, you can make this gif or png if you want
    //the file name is set above, and the quality is set to 100%
    imagejpeg($tn, $save, 100) ; 
    ?>
    Thanks

  • #2
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have just added this code, that I found on the site, well this function, although it doesnt seem to be working, its not a million miles off i dont think...

    Code:
    <?php
    $dest_pict = "imagesrob/thumbs/$ran2.jpg";
    $pict = "$ran$ext";
    $size_in_pixel_x = "100";
    $size_in_pixel_y = "100";
    function resize ( $pict, $dest_pict, $size_in_pixel_x, $size_in_pixel_y ) 
    { 
        if ( !file_exists ( $pict ) ) 
        { 
            die ( trigger_error ( 'No such file or directory', E_USER_ERROR ) ); 
        }    
        $path = pathinfo($pict); 
        switch ( strtolower ( $path["extension"] ) ) 
        { 
            case "jpeg": 
            case "jpg": 
                $handle = imagecreatefromjpeg ( $pict ); 
            break; 
            case "gif": 
                $handle = imagecreatefromgif ( $pict ); 
            break; 
            case "png": 
                $handle = imagecreatefrompng ( $pict ); 
            break; 
            default: 
                die ( trigger_error ( 'Supplied file is not a valid image resource (' . $path['basename'] . ')', E_USER_ERROR ) ); 
            break; 
        } 
         
        if ( empty ( $handle ) ) 
        { 
            die ( trigger_error ( 'Unable to create image (' . $path['basename'] . ')', E_USER_ERROR ) ); 
        } 
         
        $x = imagesx ( $handle ); 
        $y = imagesy ( $handle ); 
         
        if ( $x / $size_in_pixel_x > $y / $size_in_pixel_y ) 
        { 
            $rate = $x / $size_in_pixel_x; 
        } 
        else 
        { 
            $rate = $y / $size_in_pixel_y; 
        } 
         
        $final_x = $x / $rate; 
        $final_y = $y / $rate; 
    
        if ( $final_x > $x ) 
        { 
            $final_x = $x; 
            $final_y = $y; 
        } 
    
        $final_x = ceil ( $final_x ); 
        $final_y = ceil ( $final_y ); 
    
        $black_picture = imageCreatetruecolor ( $final_x, $final_y ); 
        imagefill ( $black_picture, 0, 0, imagecolorallocate ( $black_picture, 255, 255, 255 ) ); 
        imagecopyresampled ( $black_picture, $handle, 0, 0, 0, 0,$final_x, $final_y, $x, $y ); 
    
        if ( !@imagejpeg ( $black_picture, $dest_pict.'/mini_'.$pict, $size_in_pixel_x ) ) 
        { 
            imagejpeg ( $black_picture, $dest_pict, '100' ); 
            imagedestroy ( $handle ); 
            imagedestroy ( $black_picture ); 
        } 
    }
    //This is the directory where images will be saved 
    $target = "imagesrob/";
    $target = $target . basename( $_FILES['photo']['name']);
    // Connects to your Database 
    $host = "localhost";
    $name = "dbpw";
    $pass = "pw";
    $dbname = "dbname";
    $dbi = mysql_connect($host,$name,$pass) or
    die("I cannot connect to the database. Error :" . mysql_error());
    mysql_select_db($dbname,$dbi);
    //This gets all the other information from the form 
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $pic=($_FILES['photo']['name']);
    
    //random file stuff
    //This function separates the extension from the rest of the file name and returns it 
    function findexts ($filename) 
    { 
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
    } 
    //This applies the function to our file 
    $ext = findexts ($_FILES['photo']['name']) ; 
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
    $ran = rand () ;
    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";
    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "imagesrob/";
    //This combines the directory, the random file name, and the extension
    $target = $target . $ran2.$ext; 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    {
    resize('$ran2$ext', '$ran2$ext', 100, 100);
    echo "The file has been uploaded as ".$ran2.$ext;
    //{
    //echo "Sorry, there was a problem uploading your file.";
    //}
    //endofrandom
    }
    ?>
    thanks for looking!
    Last edited by obaluba; 03-28-2007 at 11:27 PM.

  • #3
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does anyone have any ideas on this, I'm really stuck!

    thanks for reading!

  • #4
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This may be a dead end but I notice you have variable declarations inside single quotes e.g. '$ran2$ext' at several places in both pieces of code.

    I thought that all variables had to be in double quoted ("?") strings to be parsed as variables instead if simple characters.

    Does replacing '$ran2$ext' with "$ran2$ext" help?

  • #5
    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
    What errors/output are you getting?

  • #6
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In the latest version I'm getting

    'Fatal error: No such file or directory in /home/public_html/images/add.php on line 10'

    I'm not sure if the function is in the wrong place or even if it will work like I want it to, Is it possible for the random name created when the image is saved to be passed to the thumbnail function and saved with the same name in a different location?

    thanks so much for your replys & looking!

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Code:
    /home/public_html/images/add.php
    does this page exist?
    thats what the errors saying..

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by rafiki View Post
    Code:
    /home/public_html/images/add.php
    does this page exist?
    thats what the errors saying..
    The error is saying $pict isn't a valid filename. The file that he's running is add.php.

  • #9
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it possible in php for the code to do what I am trying to make it do? by that I mean pass the random filename it has generated to the thumbnail function which renames the file and saves it in a different directory?

    thanks for reading!

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by obaluba View Post
    Is it possible in php for the code to do what I am trying to make it do? by that I mean pass the random filename it has generated to the thumbnail function which renames the file and saves it in a different directory?

    thanks for reading!
    Yes. But you aren't really doing that with the code you posted. Was there more to it?

  • #11
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There wasnt, I thought that was enough to do it, could you offer me any pointers?

    many thanks for reading

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You can combine all of those $target lines to one:
    PHP Code:
    $target 'imagesrob/' rand() . '.' findexts ($_FILES['photo']['name']); 
    Also, you don't need quotes in the function:
    PHP Code:
    resize($target$target100100); 

  • #13
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is the positioning of the function in the right place?

    Should that code work do you think? I've had the upload/create random name and submit to db bit working fine just not the thumbnail to seperate folder bit!

    thanks for reading!

  • #14
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does anyone know if the function is in the right place in the code?

    thanks again

  • #15
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    I just glanced at the code, but it looks to be in the right place.


  •  

    Posting Permissions

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