Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
Hi there can anyone make me a php script that will upload a image form the end users hard drive to a folder on my server.
And send the information to my database with the image name and automatically resize the image so somthing like 80x80 pixels.
 
Ok so this is kinda it, however on upload it displays the picutre with a link to the larger version of the picutre, i do not want the larger picture uploaded and i want the $username tag added to the picture as well as a tag indecating its the avatar and not the face pic, i also whan it to post the image name with tags to the database field "avatar" so that the display page can call the image. Any help please?

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Profile Edit</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
<?php
include("include/session.php");
?>
<?php
$usrname = $session->username;
$numbers = $database->getNumMembers();
?>
<?php
include('includes-blocks/scripts.php');
?>
<script src="AC_RunActiveContent.js" language="javascript"></script>
<script language="javascript" type="text/javascript" src="../jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
	mode : "textareas",
	theme : "simple"
});
</script>
</head>
<body>
<div id="container">
<?
if($session->logged_in){
include('includes-blocks/header.php');
}
else{
include('includes-blocks/header_notlogin.php');
}
?>
<div id="Layer_content">
<?php
include('includes-blocks/sidebar.php');
?>
<br />
<div id="content_outer">
<div>
<?
/* Requested Username error checking */
$req_user = trim($_GET['user']);
if(!$req_user || strlen($req_user) == 0 ||
   !eregi("^([0-9a-z])+$", $req_user) ||
   !$database->usernameTaken($req_user)){
   die("Username not registered");
}
/* Display requested user information */
$req_user_info = $database->getUserInfo($req_user);
?>
<?php
// do stuff //
$host = "";
$username = "";
$password = "";
$database = "";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$sql = mysql_query("SELECT * FROM users WHERE username = '$usrname'");
$row = mysql_fetch_row($sql);
?>
<?php
if ($req_user_info['gender'] == 'Female'){ 
	print '<img src="images/content/story_top.png" align="bottom" alt="cont_top" />';
}
Else {
	print '<img src="images/content/home_page.png" align="bottom" alt="cont_top" />';

}
?>
</div>
<div id="content_decor">
<div id="content">
<div id="contentarea">
<?php
if($session->logged_in){
?>
<?php
print "<form action=\"edit/profileedit.php?username=$row[0]\" method=\"post\">";
?>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td width="94%" class="<?php
if ($req_user_info['gender'] == 'Female'){ 
	print "story_title";
}
Else {
	print "page_title";

}
?>">Profile: <?php echo "".$req_user_info['username'].""; ?></td>
    <td width="6%" class="story_title"><?php
if ($req_user_info['class'] == 'Baby'){ 
?>
<select name="class" id="class">
  <option value="Baby">Baby</option>
  <option value="Carer">Carer</option>
  <option value="Furry">Furry</option>
</select>
<?php
}
Elseif ($req_user_info['class'] == 'Carer'){
?>
<select name="class" id="class">
  <option value="Carer">Carer</option>
  <option value="Baby">Baby</option>
  <option value="Furry">Furry</option>
</select>
<?php
}
Elseif ($req_user_info['class'] == 'Furry'){
?>
<select name="class" id="class">
  <option value="Furry">Furry</option>
  <option value="Baby">Baby</option>
  <option value="Carer">Carer</option>
</select>
<?php
}
?></td>
	</tr>
  <tr>
    <td class="profile_subtitle"><?php
if ($req_user_info['userlevel'] == 9) { 
    print "Admin"; 
}
Else {
	print "Member";
}
?></td>
<td class="profile_subtitle"></td>
	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td width="85%" class="pink"><br />Msn: <input type="text" name="msn" size="30" value="<?php print "$row[22]"; ?>" /><br />Yahoo: <input type="text" name="yahoo" size="30" value="<?php print "$row[23]"; ?>" /><br />iChat: <input type="text" name="ichat" size="30" value="<?php print "$row[24]"; ?>" /><br />e-mail: <?php echo "".$req_user_info['email'].""; ?><br /></td>
    <td width="15%" class="pink"><?php /*________________________________________________________________


lixlpixel PHParadise

_______________________________________________________________________

category :		image handling

snippet :		image upload and resize

downloaded :	07.12.2007 - 16:14

file URL :		http://www.fundisom.com/phparadise/php/image_handling/image_upload_and_resize

description :	this little script resizes your uploaded image on the fly and saves it as thumbnail.

 respects aspect ratio and doesn't distort your image

___________________________START___COPYING___HERE__________________*/ ?>


<?php

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

{
	$size = 80; // the thumbnail height

	$filedir = 'profile_img/'; // the directory for the original image
	$thumbdir = 'profile_img/'; // the directory for the thumbnail image
	$prefix = 'small_'; // the prefix to be added to the original name

	$maxfile = '2000000';
	$mode = '0666';
	
	$userfile_name = $_FILES['image']['name'];
	$userfile_tmp = $_FILES['image']['tmp_name'];
	$userfile_size = $_FILES['image']['size'];
	$userfile_type = $_FILES['image']['type'];
	
	if (isset($_FILES['image']['name'])) 
	{
		$prod_img = $filedir.$userfile_name;

		$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
		move_uploaded_file($userfile_tmp, $prod_img);
		chmod ($prod_img, octdec($mode));
		
		$sizes = getimagesize($prod_img);

		$aspect_ratio = $sizes[1]/$sizes[0]; 

		if ($sizes[1] <= $size)
		{
			$new_width = $sizes[0];
			$new_height = $sizes[1];
		}else{
			$new_height = $size;
			$new_width = abs($new_height/$aspect_ratio);
		}

		$destimg=ImageCreateTrueColor($new_width,$new_height)
			or die('Problem In Creating image');
		$srcimg=ImageCreateFromJPEG($prod_img)
			or die('Problem In opening Source Image');
		if(function_exists('imagecopyresampled'))
		{
			imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing');
		}else{
			Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
			or die('Problem In resizing');
		}
		ImageJPEG($destimg,$prod_img_thumb,90)
			or die('Problem In saving');
		imagedestroy($destimg);
	}

	echo '
	<a href="'.$prod_img.'">
		<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'">
	</a>';

}else{

	echo '
	<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
	<input type="file" name="image"><p>
	<input type="Submit" name="Submit" value="Submit">
	</form>';
}

?>


<?php /*_____________________END___OF___THE___CODE______________________


get more code from http://www.fundisom.com/phparadise/


___________________________________________________________________*/ ?></td>
  </tr>
</table>
</div>
<div class="profileabout">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
	<td class="profile_title" width="100%">About Me: </td>
	</tr>
  <tr>
    <td>
      <textarea name="about" rows="4" cols="52" ><?php
print nl2br($row[15]);?></textarea>
      </td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
	<td class="profile_title" width="100%">Hobby's: </td>
	</tr>
  <tr>
    <td>
<textarea name="hobbys" rows="3" cols="52" ><?php
print nl2br($row[21]);?></textarea>  </tr>
</table>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
	<td class="profile_title" width="100%">Seeking: </td>
	</tr>
  <tr>
    <td>
      <input type="text" name="seeking" value="<?php print "$row[13]"; ?>" />
</td>
  </tr>
  <tr>
      <td>
      <textarea name="other" rows="5" cols="52" ><?php
print nl2br($row[14]);?></textarea>
  </tr>
</table>
</div>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
	<td class="profile_title" width="100%">My interests: </td>
	</tr>
  <tr>
    <td>
      <textarea name="interests" rows="5" cols="52" ><?php
print nl2br($row[20]);?></textarea>
  </tr>
</table>
</div>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
	<td class="profile_title" width="100%">My Friends: </td>
	</tr>
  <tr>
    <td>
      Coming Soon</td>
  </tr>
</table>
</div>
</div>
<div class="profileaboutsm">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td class="profile_title">My Stats:</td>
	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="39%">Age:</td>
        <td width="61%"><input type="text" name="age" value="<?php print "$row[6]"; ?>" />
</td>
    </tr>
      <tr class="pink">
        <td>Gender:</td>
        <td>
<?php
if ($req_user_info['gender'] == 'Female'){ 
?>
<select name="gender" id="gender">
  <option value="Female">Female</option>
  <option value="Male">Male</option>
</select>
<?php
}
Else {
?>
<select name="gender" id="gender">
  <option value="Male">Male</option>
  <option value="Female">Female</option>
</select>
<?php
}
?>
</td>
      </tr>
      <tr>
        <td>Location:</td>
        <td><input type="text" name="location" value="<?php print "$row[9]"; ?>" />
</td>
      </tr>
      <tr class="pink"v>
        <td>Height:</td>
        <td><input type="text" name="height" value="<?php print "$row[10]"; ?>" /></td>
      </tr>
      <tr>
        <td>Eye Colour:</td>
        <td><input type="text" name="eyes" value="<?php print "$row[11]"; ?>" /></td>
      </tr>
      <tr class="pink">
        <td>Hair Colour:</td>
        <td><input type="text" name="hair" value="<?php print "$row[12]"; ?>" /></td>
      </tr>
      <tr>
        <td>Race:</td>
        <td><input type="text" name="race" value="<?php print "$row[8]"; ?>" /></td>
      </tr>
      <tr class="pink">
        <td>Occupation:</td>
        <td><input type="text" name="ocupation" value="<?php print "$row[18]"; ?>" />
</td>
      </tr>
    </table>
    <div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td class="profile_title">Profile Image: </td>
	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td><img src="profile_img/<?php echo "".$req_user_info['facepic'].""; ?>" alt="facepic" width="100%" /></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td class="profile_title">Save Changes: </td>
	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td>      <input type="submit" value="Submit"></form>		
</td>
  </tr>
</table>

</div>
</div>
<?php
}
else{
include("includes-blocks/frontpage.html");}
?>
</div>
</div>
</div>
<img src="images/content/content_bottom.png" alt="cont_btm" />
</div>
<div id="footer">
<img src="images/content/page_footer.png" alt="footer" />
</div>
</div></div>
</body>
</html>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.