Jump to content

help with writing image upload script with form data to be sent to mySQL


Guber-X

Recommended Posts

so what i have going on is that i need help writing a basic script to upload original and copy with resize for thumbnail. then also need to rename both image files with content in the form.

My server supports GD Library and that imagemagik or whatever it is lol.

 

upload dirs

orig: ../media/photos/

thumb: ../media/photos/thumb/

 

mySQL DB:

name: m_photos

fields: id(INT)

          m_cat(varchar)

          m_sub_cat(varchar)

          pic(varchar)

          description(varchar)

          p_group(varchar)

 

I have the form written up for how it should look like:

<form action="upload.php" method="post" enctype="multipart/form-data">
  Upload an image for processing<br />
  <input type="file" name="Image"><br />
  <select name="sub_group">
    <option value="Photo Shoot">Photo Shoot</option>
    <option value="Live Performances">Live Performances</option>
    <option value="Randoms">Randoms</option>
    <option value="Fan Photos">Fan Photos</option>
  </select><br />
  Location: 
  <input type="text" name="p_group" /><br />
  Date of Pic:
  <input type="text" name="date" /><br />
  Description:
  <input type="text" name="description" /><br />
  <input type="submit" value="Upload">
</form>

 

so what needs to happen is for the file upload name. it needs to grab a count from "p_group" database to give it a starting number in a "00" pattern and then the date of pic needs to go in there next then the p_group name. so when the files gets uploaded it would look something like this after upload. "03 - Oct 21, 2011 - The Mex.jpg"

NOTE: all pics must be converted to ".jpg" extension.

 

both the orig and thumb will use that same file name cuz they just go into different dirs

 

re-sizing for the thumbnail should be done by aspect ratio and needs to either be 300px width or 400px height.

 

so if anyone would like to help me out please do. im not the greatest at writing in php yet

Link to comment
Share on other sites

use move_uploaded_file() to put the file in your photos folder.

 

This is anexample of resizing code

<?php
  $target_ht = 150;
  $file = "images/web.jpg";
  $im1 = imagecreatefromjpeg($file);
  $w = imagesx($im1);
  $h = imagesy($im1);
  $target_wid = $w * $target_ht / $h;
  $file = basename($file);
  $file = 'images/new2'.$file;
  $im = imagecreatetruecolor($target_wid, $target_ht);
  
  imagecopyresized($im, $im1, 0, 0, 0, 0, $target_wid, $target_ht, $w, $h);
  imagejpeg($im, $file);
  imagedestroy($im1);
  imagedestroy($im); 
?>

Link to comment
Share on other sites

okay well i will try that out when i get to it, i am actually trying to get the renaming piece working first before i continue on.

 

so basicly i have it semi working but of course it needs to be fixed some how.

there are 3 pieces that involve in renaming my file and i got 1 working(aka the harder one) and now i dont know why the rest is just missing from it.

so lets say this is what i input in my form:

location: bridge

date: Apr 30, 2012

 

and the other piece comes from a count off the database. so when i submit my file it will be renamed to "01 - Apr 30, 2012 - bridge.jpg" but its not working like that its only getting the number, so it kinda looks like this "01 - - .jpg" like ugh... :/

 

here is my entire code for all this

<?php
include('../connect.php');
if (isset($_POST["submit"])) 
{
	for($i=1;$i<=$_POST["hdnLine"];$i++)
	{
		if($_POST["sub_group$i"] != "")
		{
			$dbcount = mysql_query('SELECT * FROM m_photos WHERE p_group="$_POST[\'p_group$i\']"') or die('dbcount failed: '.mysql_error());
			$piccount = mysql_num_rows($dbcount);
			$piccount = $piccount + 1;
			$piccount = str_pad((int) $piccount,"2","0",STR_PAD_LEFT);
			$newname = $piccount.' - '.$_POST['date$i'];
			$picnewname = $newname.' - '.$_POST['p_group$i'].'.jpg';
			if (file_exists("../media/photos/" . $picnewname))
			  {
			  echo $picnewname . " already exists. ";
			  }
			else
			  {
			  move_uploaded_file($_FILES["Image$i"]["tmp_name"],
			  "../media/photos/" . $picnewname);
			  echo "Stored in: " . "media/photos/" . $picnewname."<br />";
			  }
			$strSQL = "INSERT INTO m_photos ";
			$strSQL .="(id,m_cat,m_sub_cat,pic,description,p_group) ";
			$strSQL .="VALUES ";
			$strSQL .="('NULL','Photos','".$_POST['sub_group$i']."','".$newname."','".$_POST["description$i"]."','".$_POST["p_group$i"]."')";
			$objQuery = mysql_query($strSQL) or die('Add error Query: '.mysql_error());
		}
	}
	echo 'Products Added!';
	echo '<br /><button type="button" onclick="parent.location=\'photoupload.php\'">Back</button>';
	die;
}
   ?>
       	<form name="frmAdd" method="post" enctype="multipart/form-data">
	How Many Image Lines? :
	<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
	<?php
	  for($i=1;$i<=50;$i++) {
	   if($_GET["Line"] == $i) {
		$sel = "selected";
	   } else {
		$sel = "";
	   }
	?>
	<option value="<?=$_SERVER["PHP_SELF"];?>?action=add&Line=<?=$i;?>" <?=$sel;?>><?=$i;?></option>
	<?
	  }
	?>
	</select>
	<br /><br />
	New Images<br />
	<?php
	$line = $_GET["Line"];
	if($line == 0){$line=1;}
	for($i=1;$i<=$line;$i++)
	{
	?>
	<input type="file" name="Image<?=$i;?>"><br />
	<select name="sub_group<?=$i;?>">
	  <option value="Photo Shoot">Photo Shoot</option>
	  <option value="Live Performances">Live Performances</option>
	  <option value="Randoms">Randoms</option>
	  <option value="Fan Photos">Fan Photos</option>
	</select><br />
	Location: 
	<input type="text" name="p_group<?=$i;?>" /><br />
	Date of Pic:
	<input type="text" name="date<?=$i;?>" /><br />
	Description:
	<input type="text" name="description<?=$i;?>" /><br />
        <hr size="1" width="700" />
	<?
	}
	?>
	<input type="submit" name="submit" value="Submit">
	<input type="hidden" name="hdnLine" value="<?=$i;?>">
	</form>

 

so for some reason its not getting a few values from $_POST for rename, but some of it goes into the database... its got me all confused, like my drop box(select) dont $_POST value at all, and p_group dont go into rename but goes into database... any suggestions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.