Jump to content


Photo

images please help


  • Please log in to reply
12 replies to this topic

#1 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 22 August 2006 - 05:31 PM

I want to be able to upload images into a database a thumbnail of the image also and i want it to how would i go about doing that?
44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 05:52 PM

First you need to write a script to upload the file onto server. Once you've got it there you can use functions from the GD library to create thumbnails.

You can find out if you've got GD library installed by using this simple script:
<?php
  phpinfo();
?>
Upload that onto the server and call it as a script and look at the output.

You can search php.net for the GD functions, how to call them and how they work.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 22 August 2006 - 05:53 PM

upload image is easy
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='ufile' /><br />
<input type='submit' value='Upload File' />
</form>

Then the upload

<?php
if($_FILES["ufile"]["name"]){

$dir = "images/".$_FILES["ufile"]["name"];

if(move_uploaded_file($_FILES["ufile"]["tmp_name"], $dir){

$query = "INSERT INTO table SET image = '".$dir."'";
$result = mysql_query($query);

if($result){
echo "The image has been uploaded";
}else{
echo "There was an error uploading to the database.<br />".mysql_error();
}

}else{
echo "There was an error uploading the file";

}
}
?>


For the TN, check out
http://es2.php.net/m...copyresized.php
Tell me the problem, I will try tell you the solution

#4 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 22 August 2006 - 05:55 PM

see i dont know how to write the upload script out of all my time php coding i cant do upload scripts i can do any other script just about execpt uploads

44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#5 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 22 August 2006 - 05:55 PM

yes but i dont want it to resize or the image will look messed up
44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#6 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 05:57 PM

Have a peek at this as well...

http://uk2.php.net/f...res.file-upload
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#7 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:00 PM

It depends what you mean by messed up.

If you constrain the image then it shouldn't look that bad. There are scripts around which you can use as functions which will read the dimensions of the image and resize it accordingly to make it fit a set size. This way you upload only one file and can get two as a result - the original file and the thumbnail. If you don't need the original then you can delete it.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#8 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 22 August 2006 - 06:12 PM

this is my code

<?php
include("db.php");
if($_FILES["file"]["name"]){

$dir = "pictures/".$_FILES["file"]["name"];

if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir){
$file = $dir;
$filename = $file;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$newwidth = 150;
$newheight = 150;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
$dir2 = "thumbnails/".$_FILES["file"]["name"];
$thumbnail = move_uploaded_file(imagejpeg($thumb), $dir2);
$query = "INSERT INTO gallery SET image = '$dir', thumbnail = '$thumbnail'";
$result = mysql_query($query);
if($result){
echo "The image has been uploaded";
}else{
echo "There was an error uploading to the database.<br />".mysql_error();
}

}else{
echo "There was an error uploading the file";

}
}


?>

i get this error

Parse error: syntax error, unexpected '{' in /home/.giro/chriscloyd/idoliztic-designs.com/upload.php on line 7

44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#9 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:18 PM

This line:
if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir){
is missing a closing bracket and should be
if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir)){

Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#10 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 22 August 2006 - 06:33 PM

okay it wont upload the thumbnail and it wont add it into the database help me please
44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#11 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:41 PM

I wouldn't store the thumbnails in the database directly, I'd store them on the server instead.

I've opted for this method myself and it works great although I'm using pre-set filenames for the images and storing them in separate folders for each user that registers.

You could, however, store the filenames in the database instead. Upon uploading check the filename and see if it exists in the database - if it does then decline the file.

Loads of options are open.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#12 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 22 August 2006 - 06:53 PM

<?php
include('db.php');



if(isset($_POST['Submit'])) 

{ 
     $size = 90; // the thumbnail height 
	 $width = 110; // size of width


     $filedir = 'images/'; // the directory for the original image 
     $thumbdir = 'thumbs/'; // the directory for the thumbnail image 
	 $largedir = 'large/'; // the directory for the large image 
     $prefix = ''; // the prefix to be added to the original name 

     $maxfile = '1000000'; // max file size
     $mode = octdec('0666'); // octdec -- Octal to decimal.
	 						// The mode parameter consists of three octal number components specifying access restrictions for the owner,
							// the user group in which the owner is in, and to everybody else in this order. e.g (666)
      
     $userfile_name = $_FILES['image']['name']; 
     $userfile_tmp = $_FILES['image']['tmp_name']; 
     $userfile_size = $_FILES['image']['size']; 
     $userfile_type = $_FILES['image']['type'];
	 
      
	  // if you have a the image and name then carry on
     if (isset($_FILES['image']['name'])) 
     { 
	 
	 
	 //random digits to add to the file name
	 $random_digit=rand(00000000,99999999); 
	 $ext = '.jpg';
	 $new_file_name=$random_digit.$ext;
	 
	 	// $prod_img = the image folder and the image and name
         $prod_img = $filedir.$new_file_name; 
		
		// $prod_img_thumb = the thumb folder, the prefix (if you want it) and the image and name
         $prod_img_thumb = $thumbdir.$prefix.$new_file_name; 
		 
		 //move_uploaded_file -- Moves an uploaded file to a new location.
		 //move the uploaded file to the image folder with a tempory name.
         move_uploaded_file($userfile_tmp, $prod_img);
		 // chmod -- Changes file mode, 
		 // set the user interface for the image
         chmod ($prod_img, $mode); 
         // getimagesize -- Get the size of an image
		 // find the size of the original image
         $sizes = getimagesize($prod_img); 

		
         $aspect_ratio = $sizes[1]/$sizes[0]; 
		// if its less than the size you want it, dont change
         if ($sizes[1] <= $size) 
         { 
             $new_width = $sizes[1]; 
             $new_height = $sizes[0]; 
		// else, change image size
         }else{ 
             $new_height = $size; 
			 $new_width = $width;
           //  $new_width = abs($new_height/$aspect_ratio); 
			// this code will only change the height, and make the width in ratio
         } 


         $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image'); 
         $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image'); 
         ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); 
		 ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
         ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving'); 
         imagedestroy($destimg); 
     } 

	  $description = $_POST['description'];
	 $name = $_POST['name'];
	 $cat = $_POST['cat'];
	 
	 
	 
	 
			$sql = mysql_query("INSERT INTO image (description, name, new_file_name, cat) VALUES ('$description', '$name', '$new_file_name', '$cat')");
			
		
     echo 'Files Uploaded'; 

} 

     echo '
	 <center>
	  <table cellpadding="0" cellspacing="0" width="400" class="TLRB_border">
		<tr>
		<td align="center">
		<br><strong>"Please make sure all images are \'NO BIGGER\' than 600px wide"</strong><br><br>
		<strong>"All images must be .JPG files"</strong><br><br>
     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
	 Thumbnail tag (Please keep it short):<br>
	 <input type="text" name="name"  size="40" value="'.$_POST['name'].'"><br><br>
	 File Description:<br>
     <input type="text" name="description"  size="40" value="'.$_POST['description'].'"><br><br>
	 <input type="hidden" name="id" value="'.$_POST['id'].'">
	 <br>File to upload/store in database:<br>
     <input type="file" name="image"><p> 
     <input type="Submit" name="Submit" value="Submit"> 
     </form>
	 </td>
	 </tr>
	 </table><br>
	 </center>';
?>

How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#13 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 22 August 2006 - 06:57 PM

The post above is exactly what i use.

Its resizes the images then restores the quality.

you can choose the size of the thumbnails.
give them names, and discriptions.
it also generates a random name for the image so you dont have to worry about two images the same.

just create two folders on you server
thumbs and images

i've tryed to explain whats gong on next to each line.

Hope this helps.
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users