Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. <?php
  2.  
  3. if (isset($_POST["PHPSESSID"])) {
  4. session_id($_POST["PHPSESSID"]);
  5. }
  6.  
  7. session_start();
  8. ini_set("html_errors", "0");
  9. $sessionid = $_POST["PHPSESSID"];
  10.  
  11. $dbhost = 'localhost';
  12. $dbuser = 'xxxxxxxxx';
  13. $dbpass = 'xxxxxxxxx';
  14.  
  15. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
  16. $dbname = 'admin_burgeri';
  17. mysql_select_db($dbname);
  18. // Muutettavat arvot
  19. $tmb_max_w = 75; // Thumb-kuvan max-leveys
  20. $tmb_max_h = 75; // Thumb-kuvan max korkeus
  21. $img_max_w = 800; // Med max leveys
  22. $img_max_h = 800; // Med max korkeus
  23.  
  24. // Kuvan tiedot
  25. list($type,$ext,$width,$height) = image_information($_FILES["Filedata"]["tmp_name"]);
  26. $timestamp = microtime();
  27. $timestamp = explode(" ",$timestamp);
  28. $timestamp = $timestamp[1] + $timestamp[0];
  29. //echo $time;
  30.  
  31. //$timestamp = time();
  32. $big_image = $timestamp.'.'.$ext;
  33. $med_image = $timestamp.'_med.'.$ext;
  34. $th_image = $timestamp.'_th.'.$ext;
  35. $name = $_FILES["Filedata"]["name"];
  36. $size = ($_FILES["Filedata"]["size"] / 1024);
  37. if(file_exists($session_dir))
  38. {
  39. // Tallennetaan medium
  40. // Onko kansio olemassa?
  41. $destination_file_med = $session_dir.'/'.$timestamp.'_med.'.$ext;
  42. create_resized_image($_FILES['Filedata']['tmp_name'],$destination_file_med,$img_max_w,$img_max_h);
  43. // Tallennetaan thumb
  44. $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
  45. create_resized_image($destination_file_med,$destination_file,$tmb_max_w,$tmb_max_h);
  46. // Tallennetaan alkuperäinen kuva
  47. move_uploaded_file($_FILES["Filedata"]["tmp_name"], $session_dir.'/'.$timestamp.'.'.$ext);
  48. }
  49. else
  50. {
  51. // Tehdään uusi kansio
  52. mkdir($session_dir, 0777);
  53. chmod($session_dir, 0777);
  54. $ourFileName = "index.php";
  55. $ourFileHandle = fopen($session_dir.'/'.$ourFileName, 'w') or die("can't write file");
  56. fclose($ourFileHandle);
  57. $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
  58. $destination_file_med = $session_dir.'/'.$timestamp.'_med.'.$ext;
  59. create_resized_image($_FILES['Filedata']['tmp_name'],$destination_file_med,$img_max_w,$img_max_h);
  60. // Tallennetaan thumb
  61. $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
  62. create_resized_image($destination_file_med,$destination_file,$tmb_max_w,$tmb_max_h);
  63. // Tallennetaan alkuperäinen kuva
  64. move_uploaded_file($_FILES["Filedata"]["tmp_name"], $session_dir.'/'.$timestamp.'.'.$ext);
  65. }
  66.  
  67. list($type2,$ext2,$width2,$height2) = image_information($destination_file);
  68. $target_width = 50;
  69. $target_height = 50;
  70. $target_ratio = $target_width / $target_height;
  71. $img_ratio = $width / $height;
  72. if ($target_ratio > $img_ratio) {
  73. $new_height = $target_height;
  74. $new_width = $img_ratio * $target_height;
  75. } else {
  76. $new_height = $target_width / $img_ratio;
  77. $new_width = $target_width;
  78. }
  79. if ($new_height > $target_height) {
  80. $new_height = $target_height;
  81. }
  82. if ($new_width > $target_width) {
  83. $new_height = $target_width;
  84. }
  85. if($width2 > $height2){
  86. $portrait = 0;
  87. }else{
  88. $portrait = 1;
  89. }
  90.  
  91. $result = mysql_query("SELECT `product_id`,`product_name` FROM `jos_vm_product` WHERE `vendor_id` = '$vendor_id' ORDER BY `jos_xx_product`.`product_name` LIMIT 1") or die(mysql_error());
  92. $row = mysql_fetch_array( $result );
  93. $product_name = $row["product_name"];//take out the product_name
  94. $product_id = $row["product_id"];//take out the product_name
  95.  
  96. echo "FILEID:" . $destination_file; // Return the file id to the script
  97. $query = "INSERT INTO files_cart (vendor_dir, vendor_id, sessionid, zenid, customers_id, order_id, product_id, product_name, qty, filename, thumbnail, medium, large, timestamp, ext, filesize, widht, height, portrait) "."VALUES
  98. ('$vendor_dir','$vendor_id','$sessionid','','','','$product_id','$product_name','1', '$name', '$th_image','$med_image', '$big_image', '$timestamp', '$ext', '$size', '$width', '$height', '$portrait')";
  99. mysql_query($query) or die('Error, query failed : ' . mysql_error());
  100.  
  101.  
  102. function image_information($original_file)
  103. {
  104. // Ottaa tarkistettavan tiedoston nimen ja tarkistaa sen tiedostopäätteen välittämättä tiedostonimestä, toimii kuville
  105. $type = getimagesize($original_file);
  106. $filesize = filesize($original_file);
  107. // Tarkistetaan tiedoston tyyppi
  108. if($type[2] == 1) // GIF
  109. {
  110. $file_extension = "gif";
  111. }
  112. elseif($type[2] == 2) // JPEG
  113. {
  114. $file_extension = "jpg";
  115. }
  116. elseif($type[2] == 3) // PNG
  117. {
  118. $file_extension = "png";
  119. }
  120. else // Tiedostomuoto ei ole tuettu, palauttaa FALSE
  121. {
  122. $file_extension = FALSE;
  123. }
  124. // Funktio palauttaa arvot, jos ok
  125. if($file_extension)
  126. {
  127. // palauttaa type,tiedostopääte,leveys,korkeus,tiedostokoko
  128. return array($type[2],$file_extension,$type[0],$type[1],$filesize);
  129. }
  130. else
  131. {
  132. // Tiedostotyyppi ei ole tuettu tai jotain häiriöö
  133. return array(FALSE,FALSE,FALSE,FALSE);
  134. }
  135. }
  136.  
  137. function create_resized_image($original_file,$destination_file,$resized_width,$resized_height)
  138. {
  139. // Ottaa syötteenä vastaan (alkuperäinen tiedosto), (uuden kuvan hakemisto/tiedosto ilman päätettä), (uusi leveys), (uusi korkeus)
  140.  
  141. // Selvitetään kuvan koko ja tyyppi
  142. list($original_width, $original_height, $type) = getimagesize($original_file);
  143.  
  144. // Tarkistetaan tiedoston tyyppi
  145. if($type == 1) // GIF
  146. {
  147. $original_image = imagecreatefromgif($original_file);
  148. // Läpinäkyvyys -> valkoinen
  149. $white = imagecolorallocate($original_image, 255, 255, 255);
  150. $transparent = imagecolortransparent($original_image, $white);
  151. }
  152. elseif($type == 2) // JPEG
  153. {
  154. $original_image = imagecreatefromjpeg($original_file);
  155. }
  156. elseif($type == 3) // PNG
  157. {
  158. $original_image = imagecreatefrompng($original_file);
  159. }
  160. else // Tiedostomuoto ei ole tuettu, palauttaa FALSE
  161. {
  162. $type = FALSE;
  163. }
  164. if($type)
  165. {
  166. // Lasketaan kuvalle uusi koko siten, että kuvasuhde säilyy
  167. $new_w = $original_width/$resized_width; // Kuvasuhde: leveys
  168. $new_h = $original_height/$resized_height; // Kuvasuhde: korkeus
  169. if($new_w > $new_h || $new_w == $new_h)
  170. {
  171. if($new_w < 1)
  172. {
  173. // Jos alkuperäinen kuva on pienempi kuin luotava, luodaan alkuperäisen kokoinen kuva
  174. $new_w = 1;
  175. }
  176. // Käytetään sitä suhdetta, jolla tulee max. asetettu leveys, korkeus on alle max.
  177. $new_width = $original_width / $new_w;
  178. $new_height = $original_height / $new_w;
  179. }
  180. elseif($new_w < $new_h)
  181. {
  182. if($new_h < 1)
  183. {
  184. // Jos alkuperäinen kuva on pienempi kuin luotava, luodaan alkuperäisen kokoinen kuva
  185. $new_h = 1;
  186. }
  187. // Käytetään sitä suhdetta, jolla tulee max. asetettu korkeus, leveys on alle max.
  188. $new_width = $original_width / $new_h;
  189. $new_height = $original_height / $new_h;
  190. }
  191. // Luodaan kuva, joka on määrätyn kokoinen
  192. $image = imagecreatetruecolor($new_width, $new_height);
  193. // Resample, luo uuden kuvan tiedostoon
  194. imagecopyresampled($image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
  195.  
  196. // Tallennetaan uusi kuva määriteltyyn tiedostoon ja annetaan sopiva tiedostopääte
  197. if($type == 1) // GIF
  198. {
  199. imagegif($image, $destination_file);
  200. }
  201. elseif($type == 2) // JPEG
  202. {
  203. imagejpeg($image, $destination_file);
  204. }
  205. elseif($type == 3) // PNG
  206. {
  207. imagepng($image, $destination_file);
  208. }
  209. }
  210. // Poistetaan kuva muistista, ei tuhoa alkuperäistä tiedostoa!
  211.  
  212. // Palauttaa tiedostotyypin onnistuessaan, FALSE jos ei onnistu
  213. return $type;
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement