Jump to content

Unique Filename / Thumbnail Creation


obaluba

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/44494-unique-filename-thumbnail-creation/
Share on other sites

You need the complete path from '/' I expect.

 

The easy way to do it is:

 

$_SERVER['DOCUMENT_ROOT']. '/robs-images/image.png';

 

 

Random image names is a poor idea, one day your random number will produce two 55's in a row (for example) then your code will crash unexpectedly and you won't be able to remember why or what may have gone wrong  :P :P

 

Use the time as ticks, then you can be sure you won't get duplicates ( until the ticks wraps in 2035 or something)

 

monk.e.boy

 

Thanks for your reply, although I am still having issues, 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!

 

The code is below:

 

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

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.