0

New user to Daniweb in the hope i can get some help cus my brain is fried.

Im relitively a n00b at php but i can handle the basics, working with images is beyond me at the moment but im learning.

I have the below code, which works. it uploads image, renames and adds data to my sql database all fine as well as running checks on filesize, extensions etc.

What i wanted to do next was to resize the file in proportion to a dedicated set width.

But at same time create a copy of the file and change its name to include "_thumb" after the filename and make that even smaller.

IS this possible and i dont suppose anyone can post me some code to use and learn with.

my current working code:

<?php
$rand = mt_rand(1,9999999);
$member_id = $_SESSION['SESS_MEMBER_ID'];
$caption = $_POST["caption"];

if(isset($_FILES['uploaded']['name']))
{
	$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg');
	$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB) 
    $fileName = basename($_FILES['uploaded']['name']);
	$errors = array();
	$target = "gallery/";
	$fileBaseName = substr($fileName, 0, strripos($fileName, '.'));
	
   // Get the extension from the filename.
   $ext = substr($fileName, strpos($fileName,'.'), strlen($fileName)-1); 
 
   //$newFileName = md5($fileBaseName) . $ext;
   $newFileName = $target . $rand . "_" . $member_id.$ext;
   
   // Check if filename already exists
   if(file_exists("gallery/" . $newFileName)) 
    {
	     $errors[] = "The file you attempted to upload already exists, please try again.";
    }
   // Check if the filetype is allowed.
   if(!in_array($ext,$allowed_filetypes))
    {
         $errors[] = "The file you attempted to upload is not allowed.";
    }
    
	// Now check the filesize.
   if(!filesize($_FILES['uploaded']['tmp_name']) > $max_filesize)
    {
         $errors[] = "The file you attempted to upload is too large.";
    }
	  
    // Check if we can upload to the specified path.
   if(!is_writable($target))
    {
         $errors[] = "You cannot upload to the specified directory, please CHMOD it to 777.";
    }
 
    //Here we check that no validation errors have occured.
    if(count($errors)==0)
    {

		//Try to upload it.
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $newFileName))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    //Lets INSERT database information here
    if(count($errors)==0)
    {
	$result = mysql_query("INSERT INTO `gallery` (`image`, `memberid`, `caption`) VALUES ('$newFileName', '$member_id', '$caption')")
    or die (mysql_error()); 
		
	}
    //If no errors show confirmation message
    if(count($errors)==0)
    {
         echo "<div class='notification success png_bg'>
				<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
				<div>
					Image has been uploaded.<br>\n
				</div>
			</div>";
		
			
			//echo "The file {$fileName} has been uploaded";
		echo "<br>\n";
		echo "<a href='gallery.php'>Go Back</a>\n";
		
   }
    else
    {
        //show error message
        echo "<div class='notification attention png_bg'>
				<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
				<div>
					Sorry your file was not uploaded due to the following errors:<br>\n
				</div>
			</div>";
		//echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
		echo "<br>\n";
		echo "<a href='gallery.php'>Go Back</a>\n";
    }
    
}
else
{
    //Show the form
	echo "Use the following form below to add a new image to your gallery;<br /><br />\n";
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file:<br /><input class='text' name='uploaded' type='file' /><br />\n";
	echo "Image Caption:<br /><input class='text' name='caption' type='text' value='' /><br /><br />\n";
    echo "<input class='Button' type='submit' value='Upload' />\n";
    echo "</form>\n";
	
}
?>

Many thanx.

Edited by jacko_162: n/a

1
Contributor
1
Reply
5
Views
5 Years
Discussion Span
Last Post by jacko_162
0

i found this code on my travels, but thus far struggle to intergrate it without major errors.

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

can anyone help me impliment it into my original code above? im trying to learn this as i go.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.