Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2007, 03:41 PM   PM User | #1
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
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
obaluba is offline   Reply With Quote
Old 03-27-2007, 04:22 PM   PM User | #2
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
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..
obaluba is offline   Reply With Quote
Old 03-28-2007, 07:44 AM   PM User | #3
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
Does anyone have any ideas on this, I'm really stuck!

thanks for reading!
obaluba is offline   Reply With Quote
Old 03-28-2007, 08:46 PM   PM User | #4
doozer
New Coder

 
Join Date: Feb 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
doozer is an unknown quantity at this point
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?
doozer is offline   Reply With Quote
Old 03-28-2007, 08:50 PM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
What errors/output are you getting?
__________________
Fumigator is offline   Reply With Quote
Old 03-28-2007, 11:16 PM   PM User | #6
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
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!
obaluba is offline   Reply With Quote
Old 03-28-2007, 11:43 PM   PM User | #7
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
Code:
/home/public_html/images/add.php
does this page exist?
thats what the errors saying..
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 03-29-2007, 03:14 AM   PM User | #8
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
Inigoesdr is offline   Reply With Quote
Old 03-29-2007, 06:57 AM   PM User | #9
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
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!
obaluba is offline   Reply With Quote
Old 03-29-2007, 09:33 AM   PM User | #10
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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?
Inigoesdr is offline   Reply With Quote
Old 03-29-2007, 03:55 PM   PM User | #11
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
There wasnt, I thought that was enough to do it, could you offer me any pointers?

many thanks for reading
obaluba is offline   Reply With Quote
Old 03-29-2007, 04:58 PM   PM User | #12
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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); 
Inigoesdr is offline   Reply With Quote
Old 03-29-2007, 11:00 PM   PM User | #13
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
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!
obaluba is offline   Reply With Quote
Old 03-30-2007, 10:47 PM   PM User | #14
obaluba
New Coder

 
Join Date: Aug 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
obaluba is an unknown quantity at this point
Does anyone know if the function is in the right place in the code?

thanks again
obaluba is offline   Reply With Quote
Old 03-30-2007, 10:50 PM   PM User | #15
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
I just glanced at the code, but it looks to be in the right place.
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 PM.


Advertisement
Log in to turn off these ads.