Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image upload and Create Thumbnail

Status
Not open for further replies.

WilliamMute007

Programmer
Sep 30, 2007
116
GB
Hi Guys,

I have a bit of problem here. I have a page which basically uploads a set of info together with an image. Now I now tried to implement image resizing to the image being uploaded so i can have a thumbnail but here comes disaster! Help Please.. It works without the image resizing. Please see code below and help.

Code:
    require_once("settings.php"); //Load the settings
    $alert = ""; //Alert message after actions
    // Upload action
	if(isset($_FILES['file'])) {
		$extension = pathinfo($_FILES['file']['name']);
		$extension = $extension["extension"];
		$allowed_paths = explode(", ", $allowed_ext); //if any file is uploaded loads the allowed_ext from settings.php and explode the array
		$valid = 0;
		for($i = 0; $i < count($allowed_paths); $i++) {
			if ($allowed_paths[$i] == "$extension") {
				$valid = 1; // valid = 1 only if the extension is allowed in "settings.php" file
			}
		}
		if ($valid == 1 && $_FILES["file"]["size"] <= $max_weight) {
			if (file_exists("upload/" . $_FILES["file"]["name"])) {
				$alert = '<p class="error">' . $_FILES["file"]["name"] . ' already exist!' . '</p>'; //the file already exists
			} else {
				move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
				


$scr = $_FILES["file"]["tmp_name"];

list($width,$height)=getimagesize($_FILES["file"]["tmp_name"]);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,

 $width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, 

$width,$height);

$filename = "upload/". $_FILES['file']['name'];
$filename1 = "upload/small". $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);

	


				$save = $_FILES["file"]["name"]; //It stores the name of the file in a variable
				$mysql = mysql_connect($db_host, $db_user, $db_password);
				mysql_select_db($db_name, $mysql);
				$sql = "INSERT INTO $table_name (FirstName, LastName, Title, summary, other, File) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[title]','$_POST[summary]','$_POST[other]','upload/$save')"; //stores records in database 
				mysql_query($sql, $mysql);
				$alert = '<p class="ok">' . 'Successfuly Added!' . '</p>';
				mysql_close($mysql);
			}
		} else {
			$alert = '<p class="error">' . 'Invalid file type!' . '</p>'; //the extension is not supported or the wight file is too big
		}
	}
?>


Thank you for your help in advance
 
i posted an image resampling script a couple of days ago (13/9).
 
Hi Justin,

I've seen the script that you uploaded few days back. The only problem is I only need to adapt resiezing to my existing script and to be honest with you, Looking through your extremely guru code, I have NO idea whats so ever where to start in terms of merging it with my existing code.

Any help please :(

Thank you
 
change this line
Code:
$scr = $_FILES["file"]["tmp_name"];
to
Code:
$src = imagecreatefromjpeg($_FILES['file']['tmp_name']);
 
Thanks for that.

I get the following errors

Code:
Warning: imagecreatefromjpeg(/tmp/php6UmK6r) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 23

Warning: getimagesize(/tmp/php6UmK6r) [function.getimagesize]: failed to open stream: No such file or directory in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 25

Warning: Division by zero in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 27

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 28

Warning: Division by zero in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 31

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 32

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 36

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 40

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 45

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 46

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 48

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 49

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/fhlinux132/m/miraclemakers.co.uk/user/htdocs/preview/upload/index.php on line 50

Any suggestions please?
 
Hi, never mind, sorted out now! Thank you for your contributions. I really really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top