...

View Full Version : Unique Filename / Thumbnail Generation



obaluba
03-27-2007, 03:41 PM
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!? :)



<?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
03-27-2007, 04:22 PM
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...



<?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!

obaluba
03-28-2007, 07:44 AM
Does anyone have any ideas on this, I'm really stuck!

thanks for reading! :)

doozer
03-28-2007, 08:46 PM
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?

Fumigator
03-28-2007, 08:50 PM
What errors/output are you getting?

obaluba
03-28-2007, 11:16 PM
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!

rafiki
03-28-2007, 11:43 PM
/home/public_html/images/add.php
does this page exist?
thats what the errors saying..

Inigoesdr
03-29-2007, 03:14 AM
/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.

obaluba
03-29-2007, 06:57 AM
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! :)

Inigoesdr
03-29-2007, 09:33 AM
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?

obaluba
03-29-2007, 03:55 PM
There wasnt, I thought that was enough to do it, could you offer me any pointers?

many thanks for reading :)

Inigoesdr
03-29-2007, 04:58 PM
You can combine all of those $target lines to one:

$target = 'imagesrob/' . rand() . '.' . findexts ($_FILES['photo']['name']);
Also, you don't need quotes in the function:

resize($target, $target, 100, 100);

obaluba
03-29-2007, 11:00 PM
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
03-30-2007, 10:47 PM
Does anyone know if the function is in the right place in the code?

thanks again :)

Inigoesdr
03-30-2007, 10:50 PM
I just glanced at the code, but it looks to be in the right place.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum