matvespa Posted August 25, 2010 Share Posted August 25, 2010 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']; } ?> Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 25, 2010 Share Posted August 25, 2010 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! Quote Link to comment Share on other sites More sharing options...
matvespa Posted August 25, 2010 Author Share Posted August 25, 2010 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'); ?> Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 25, 2010 Share Posted August 25, 2010 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? Quote Link to comment Share on other sites More sharing options...
matvespa Posted August 25, 2010 Author Share Posted August 25, 2010 I am only interested in the thumbnail image. The code works fine in everything. It manages to save the big and thumbnail image onto my directory but not saved in my database. Im not really interested with the big image. Just the Thumbnail. Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 25, 2010 Share Posted August 25, 2010 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 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']; } ?> Quote Link to comment Share on other sites More sharing options...
matvespa Posted August 25, 2010 Author Share Posted August 25, 2010 The script didnt work. Maybe i'll attached the entire file here so u'll understand better. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 26, 2010 Share Posted August 26, 2010 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] Quote Link to comment Share on other sites More sharing options...
matvespa Posted August 26, 2010 Author Share Posted August 26, 2010 Hi. Thanks for your help. But do u have any IM so that you could assist me further. The application is using AJAX and it is totally out from my league. Hope to tackle this one! Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 26, 2010 Share Posted August 26, 2010 hey, i am sorry to say that i cant help you further, as i really dont know mutch about AJAX myself.. i only can help a little with php and SQL.. i really hope some other helper will jump in here to help you further! Quote Link to comment Share on other sites More sharing options...
matvespa Posted August 26, 2010 Author Share Posted August 26, 2010 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! Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 26, 2010 Share Posted August 26, 2010 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.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.