Jump to content

PHP & AJAX Image Upload


matvespa

Recommended Posts

I found this code online, it works fine but i dont know where i should put the SQL statement to place the image path onto my database. Here is what i have...the SQL statement i place them at doesnt work. Any suggestions?

<?php
function resizeImg($arr){

//you can change the name of the file here
$date 		= md5(time());

//////////// upload image and resize

$uploaddir 	= $arr['uploaddir'];
$tempdir	= $arr['tempdir'];


$temp_name 	= $_FILES['photo']['tmp_name'];

//echo $temp_name;

$img_parts 	= pathinfo($_FILES['photo']['name']);
$new_name 	= strtolower($date.'.'.$img_parts['extension']);

$ext = strtolower($img_parts['extension']);

$allowed_ext = array('gif','jpg','jpeg','png');
if(!in_array($ext,$allowed_ext)){
	echo '<p class="uperror">Please upload again. Only GIF, JPG and PNG files please.</p>';
	exit;
}


	$temp_uploadfile = $tempdir . $new_name;
	$new_uploadfile = $uploaddir . $new_name;

// less than 1.3MB
	if($_FILES['photo']['size'] <   2097000 ){
				if (move_uploaded_file($temp_name, $temp_uploadfile)) {

				// add key value to arr
				$arr['temp_uploadfile'] = $temp_uploadfile;
				$arr['new_uploadfile'] = $new_uploadfile;

				asidoImg($arr);

				unlink($temp_uploadfile);
				exit;
				}
	}
	else
	{
		echo '<p class="uperror">Please upload again. Maximum filesize is 1.3MB.</p>';
		exit;
	}

}


function resizeThumb($arr){

$date = md5(time());	
$arr['temp_uploadfile'] = $arr['img_src'];
$arr['new_uploadfile'] = $arr['uploaddir'].strtolower($date).'.jpg';

asidoImg($arr);
exit;
}

function asidoImg($arr){

include('asido/class.asido.php');
asido::driver('gd');

$height		= $arr['height'];
$width		= $arr['width'];
$x			= $arr['x'];
$y			= $arr['y'];				

// process
$i1 = asido::image($arr['temp_uploadfile'], $arr['new_uploadfile']);	
// fit and add white frame										
if($arr['thumb'] === true){
	Asido::Crop($i1, $x, $y, $width, $height);

}
else{
	Asido::Frame($i1, $width, $height, Asido::Color(255, 255, 255));			
}

// always convert to jpg	
Asido::convert($i1, 'image/jpg');

$i1->Save(ASIDO_OVERWRITE_ENABLED);
	$data = array(
	'photo'=> $arr['new_uploadfile']
  );
	// echo $user_id;
// delete old file
echo $data['photo'];	
}

?>

Link to comment
Share on other sites

you should place the same name as the image is uploaded in your database, so you can get that name out of your database and show it on your website

 

if i see it corectly, your image is stored as $new_name, so after $new_name is made, put it in your database:

 

$sql="INSERT INTO tablename (photoname) VALUES ('$new_name')";

 

and where you want to show your picture, after getting it out of the database, place the directory + the photoname

 

echo '<img src="'.$uploaddir.$row['photoname'].'">';

 

of course you can also store the directory + the name in the database, but would not recommend that as after deciding to put all the images in an other directory, you have a lot to correct ;)

 

hope it helps!

Link to comment
Share on other sites

Hey hi, thanks for the help. I guess i pasted the wrong code there. Should paste upload.php instead.

As you can see, i've paste my SQL code in there as well. Pls advise. Thanks!

<?php
include('dbConnection/dbConfig.php');
include('dbConnection/dbOpen.php');
include('func.php');

if($_GET['act'] == 'thumb'){

$arr = array(
'uploaddir' 	=> 'uploads/',
'tempdir'		=> 'uploads/temp/',
'height'		=> $_POST['height'],
'width'			=> $_POST['width'],
'x'				=> $_POST['x'],
'y'				=> $_POST['y'],
'img_src'		=> $_POST['img_src'],
'thumb'			=> true
);
resizeThumb($arr);

//START OF SQL
$SQL = "INSERT INTO test (cropimg) VALUES ('resizeThumb($arr)')";

if (!mysql_query($SQL,$conn))
  {
  die('Error: ' . mysql_error());
  }
  //END OF SQL
exit;




}

elseif($_GET['act'] == 'upload'){

$big_arr = array(
'uploaddir'	=> 'uploads/big/',
'tempdir'	=> 'uploads/temp/',
'height'	=> $_POST['height'],
'width'		=> $_POST['width'],
'x'			=> 0,
'y'			=> 0
);

resizeImg($big_arr);	
}
else
{
//
}
include('dbConnection/dbClose.php');
?>

Link to comment
Share on other sites

i am a little confused, do you want to store only the thumbnail? Because this code makes two resizes, one thumbnail and one main picture, and uploads them, but with your SQL, you only seem interested in the thumbnail..

 

anyway, your code will not work as resizeThumb($arr) is no imagepath, but the function to process it ;) you should place the imagename here in stead. this imagename is made on the functionpage. i would make this imagename in your maincode and than pass it on within the function..

 

can you specify: what do you want your code to do? because this code might do more than that, only upload your image and put it into a database? or also make two resizes, upload them both and put them both into a database?

Link to comment
Share on other sites

EDIT: sorry, first time i forgot to place $date, changed it now

EDIT2: made an other change, sorry for not being an expert and doing it good right away :P

 

ok try this, i changed both files a little.

 

the name of the file was assigned within the function in this way: strtolower($date).'.jpg'. i now assigned that name earlyer, so you can use it to store in your database and transported it within the array to the function, so it can be used there, hope it works!

 

<?php
include('dbConnection/dbConfig.php');
include('dbConnection/dbOpen.php');
include('func.php');

if($_GET['act'] == 'thumb'){

$date = md5(time());
$name = strtolower($date).'.jpg';

$arr = array(
'uploaddir' 	=> 'uploads/',
'tempdir'		=> 'uploads/temp/',
'height'		=> $_POST['height'],
'width'			=> $_POST['width'],
'x'				=> $_POST['x'],
'y'				=> $_POST['y'],
'img_src'		=> $_POST['img_src'],
'thumb'			=> true,
'name'			=> $name,
        'date'                       => $date
);
resizeThumb($arr);

//START OF SQL
$SQL = "INSERT INTO test (cropimg) VALUES ('$name')";

if (!mysql_query($SQL,$conn))
  {
  die('Error: ' . mysql_error());
  }
  //END OF SQL
exit;




}

elseif($_GET['act'] == 'upload'){

$big_arr = array(
'uploaddir'	=> 'uploads/big/',
'tempdir'	=> 'uploads/temp/',
'height'	=> $_POST['height'],
'width'		=> $_POST['width'],
'x'			=> 0,
'y'			=> 0
);

resizeImg($big_arr);	
}
else
{
//
}
include('dbConnection/dbClose.php');
?>

 

and

 

<?php
function resizeImg($arr){

//you can change the name of the file here
$date 		= $arr['date'];

//////////// upload image and resize

$uploaddir 	= $arr['uploaddir'];
$tempdir	= $arr['tempdir'];


$temp_name 	= $_FILES['photo']['tmp_name'];

//echo $temp_name;

$img_parts 	= pathinfo($_FILES['photo']['name']);
$new_name 	= strtolower($date.'.'.$img_parts['extension']);

$ext = strtolower($img_parts['extension']);

$allowed_ext = array('gif','jpg','jpeg','png');
if(!in_array($ext,$allowed_ext)){
	echo '<p class="uperror">Please upload again. Only GIF, JPG and PNG files please.</p>';
	exit;
}


	$temp_uploadfile = $tempdir . $new_name;
	$new_uploadfile = $uploaddir . $new_name;

// less than 1.3MB
	if($_FILES['photo']['size'] <   2097000 ){
				if (move_uploaded_file($temp_name, $temp_uploadfile)) {

				// add key value to arr
				$arr['temp_uploadfile'] = $temp_uploadfile;
				$arr['new_uploadfile'] = $new_uploadfile;

				asidoImg($arr);

				unlink($temp_uploadfile);
				exit;
				}
	}
	else
	{
		echo '<p class="uperror">Please upload again. Maximum filesize is 1.3MB.</p>';
		exit;
	}

}


function resizeThumb($arr){

$date = $arr['date'];
$arr['temp_uploadfile'] = $arr['img_src'];
$arr['new_uploadfile'] = $arr['uploaddir'].$arr['name'];

asidoImg($arr);
exit;
}

function asidoImg($arr){

include('asido/class.asido.php');
asido::driver('gd');

$height		= $arr['height'];
$width		= $arr['width'];
$x			= $arr['x'];
$y			= $arr['y'];				

// process
$i1 = asido::image($arr['temp_uploadfile'], $arr['new_uploadfile']);	
// fit and add white frame										
if($arr['thumb'] === true){
	Asido::Crop($i1, $x, $y, $width, $height);

}
else{
	Asido::Frame($i1, $width, $height, Asido::Color(255, 255, 255));			
}

// always convert to jpg	
Asido::convert($i1, 'image/jpg');

$i1->Save(ASIDO_OVERWRITE_ENABLED);
	$data = array(
	'photo'=> $arr['new_uploadfile']
  );
	// echo $user_id;
// delete old file
echo $data['photo'];	
}

?>

 

Link to comment
Share on other sites

hmm.. it should not really be important what the rest of the code lookes like, the name of the picture should still just be stored within the database..

 

you do have code above this code to connect with your database right? and you also made a database with a table named 'test' with a field named cropimg?

 

i changed a littlebit of the code and just for sure also added code to connect to a database... fill in the first 4 variables with your own databaseproperties

 

EDIT: i just tested this part of the code on my website and i do get the name of the image within the database, so i hope it works for you too!

 

[attachment deleted by admin]

Link to comment
Share on other sites

Alright. Its okay. I've used your code, and it seem good. I guess the ajax side isnt working well with the php coding here. Coz when i tried to upload an image, it showed a cross mark which means invalid file. But all images i tested with is jpg which is capable with the coding. Just dont know why!

Link to comment
Share on other sites

i hope than that the name stored in the database is the same as the name of the file stored on the server.. for this piece of code it should be, but i dont know whats going on in that extra asidofiles that are called in..

 

check if the code for the image under that cross you see (right-click, imageinfo en check the directory + name of the image) is the same as the directory and imagename on your server

 

if not, see if the code for placing the image is allright, a different directory is easely changed, if there is a difference with the imagename, the code is messing with the imagename..

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.