Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_POST["PHPSESSID"])) {
- session_id($_POST["PHPSESSID"]);
- }
- session_start();
- ini_set("html_errors", "0");
- $sessionid = $_POST["PHPSESSID"];
- $dbhost = 'localhost';
- $dbuser = 'xxxxxxxxx';
- $dbpass = 'xxxxxxxxx';
- $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
- $dbname = 'admin_burgeri';
- mysql_select_db($dbname);
- // Muutettavat arvot
- $tmb_max_w = 75; // Thumb-kuvan max-leveys
- $tmb_max_h = 75; // Thumb-kuvan max korkeus
- $img_max_w = 800; // Med max leveys
- $img_max_h = 800; // Med max korkeus
- // Kuvan tiedot
- list($type,$ext,$width,$height) = image_information($_FILES["Filedata"]["tmp_name"]);
- $timestamp = microtime();
- $timestamp = explode(" ",$timestamp);
- $timestamp = $timestamp[1] + $timestamp[0];
- //echo $time;
- //$timestamp = time();
- $big_image = $timestamp.'.'.$ext;
- $med_image = $timestamp.'_med.'.$ext;
- $th_image = $timestamp.'_th.'.$ext;
- $name = $_FILES["Filedata"]["name"];
- $size = ($_FILES["Filedata"]["size"] / 1024);
- if(file_exists($session_dir))
- {
- // Tallennetaan medium
- // Onko kansio olemassa?
- $destination_file_med = $session_dir.'/'.$timestamp.'_med.'.$ext;
- create_resized_image($_FILES['Filedata']['tmp_name'],$destination_file_med,$img_max_w,$img_max_h);
- // Tallennetaan thumb
- $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
- create_resized_image($destination_file_med,$destination_file,$tmb_max_w,$tmb_max_h);
- // Tallennetaan alkuperäinen kuva
- move_uploaded_file($_FILES["Filedata"]["tmp_name"], $session_dir.'/'.$timestamp.'.'.$ext);
- }
- else
- {
- // Tehdään uusi kansio
- mkdir($session_dir, 0777);
- chmod($session_dir, 0777);
- $ourFileName = "index.php";
- $ourFileHandle = fopen($session_dir.'/'.$ourFileName, 'w') or die("can't write file");
- fclose($ourFileHandle);
- $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
- $destination_file_med = $session_dir.'/'.$timestamp.'_med.'.$ext;
- create_resized_image($_FILES['Filedata']['tmp_name'],$destination_file_med,$img_max_w,$img_max_h);
- // Tallennetaan thumb
- $destination_file = $session_dir.'/'.$timestamp.'_th.'.$ext;
- create_resized_image($destination_file_med,$destination_file,$tmb_max_w,$tmb_max_h);
- // Tallennetaan alkuperäinen kuva
- move_uploaded_file($_FILES["Filedata"]["tmp_name"], $session_dir.'/'.$timestamp.'.'.$ext);
- }
- list($type2,$ext2,$width2,$height2) = image_information($destination_file);
- $target_width = 50;
- $target_height = 50;
- $target_ratio = $target_width / $target_height;
- $img_ratio = $width / $height;
- if ($target_ratio > $img_ratio) {
- $new_height = $target_height;
- $new_width = $img_ratio * $target_height;
- } else {
- $new_height = $target_width / $img_ratio;
- $new_width = $target_width;
- }
- if ($new_height > $target_height) {
- $new_height = $target_height;
- }
- if ($new_width > $target_width) {
- $new_height = $target_width;
- }
- if($width2 > $height2){
- $portrait = 0;
- }else{
- $portrait = 1;
- }
- $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());
- $row = mysql_fetch_array( $result );
- $product_name = $row["product_name"];//take out the product_name
- $product_id = $row["product_id"];//take out the product_name
- echo "FILEID:" . $destination_file; // Return the file id to the script
- $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
- ('$vendor_dir','$vendor_id','$sessionid','','','','$product_id','$product_name','1', '$name', '$th_image','$med_image', '$big_image', '$timestamp', '$ext', '$size', '$width', '$height', '$portrait')";
- mysql_query($query) or die('Error, query failed : ' . mysql_error());
- function image_information($original_file)
- {
- // Ottaa tarkistettavan tiedoston nimen ja tarkistaa sen tiedostopäätteen välittämättä tiedostonimestä, toimii kuville
- $type = getimagesize($original_file);
- $filesize = filesize($original_file);
- // Tarkistetaan tiedoston tyyppi
- if($type[2] == 1) // GIF
- {
- $file_extension = "gif";
- }
- elseif($type[2] == 2) // JPEG
- {
- $file_extension = "jpg";
- }
- elseif($type[2] == 3) // PNG
- {
- $file_extension = "png";
- }
- else // Tiedostomuoto ei ole tuettu, palauttaa FALSE
- {
- $file_extension = FALSE;
- }
- // Funktio palauttaa arvot, jos ok
- if($file_extension)
- {
- // palauttaa type,tiedostopääte,leveys,korkeus,tiedostokoko
- return array($type[2],$file_extension,$type[0],$type[1],$filesize);
- }
- else
- {
- // Tiedostotyyppi ei ole tuettu tai jotain häiriöö
- return array(FALSE,FALSE,FALSE,FALSE);
- }
- }
- function create_resized_image($original_file,$destination_file,$resized_width,$resized_height)
- {
- // Ottaa syötteenä vastaan (alkuperäinen tiedosto), (uuden kuvan hakemisto/tiedosto ilman päätettä), (uusi leveys), (uusi korkeus)
- // Selvitetään kuvan koko ja tyyppi
- list($original_width, $original_height, $type) = getimagesize($original_file);
- // Tarkistetaan tiedoston tyyppi
- if($type == 1) // GIF
- {
- $original_image = imagecreatefromgif($original_file);
- // Läpinäkyvyys -> valkoinen
- $white = imagecolorallocate($original_image, 255, 255, 255);
- $transparent = imagecolortransparent($original_image, $white);
- }
- elseif($type == 2) // JPEG
- {
- $original_image = imagecreatefromjpeg($original_file);
- }
- elseif($type == 3) // PNG
- {
- $original_image = imagecreatefrompng($original_file);
- }
- else // Tiedostomuoto ei ole tuettu, palauttaa FALSE
- {
- $type = FALSE;
- }
- if($type)
- {
- // Lasketaan kuvalle uusi koko siten, että kuvasuhde säilyy
- $new_w = $original_width/$resized_width; // Kuvasuhde: leveys
- $new_h = $original_height/$resized_height; // Kuvasuhde: korkeus
- if($new_w > $new_h || $new_w == $new_h)
- {
- if($new_w < 1)
- {
- // Jos alkuperäinen kuva on pienempi kuin luotava, luodaan alkuperäisen kokoinen kuva
- $new_w = 1;
- }
- // Käytetään sitä suhdetta, jolla tulee max. asetettu leveys, korkeus on alle max.
- $new_width = $original_width / $new_w;
- $new_height = $original_height / $new_w;
- }
- elseif($new_w < $new_h)
- {
- if($new_h < 1)
- {
- // Jos alkuperäinen kuva on pienempi kuin luotava, luodaan alkuperäisen kokoinen kuva
- $new_h = 1;
- }
- // Käytetään sitä suhdetta, jolla tulee max. asetettu korkeus, leveys on alle max.
- $new_width = $original_width / $new_h;
- $new_height = $original_height / $new_h;
- }
- // Luodaan kuva, joka on määrätyn kokoinen
- $image = imagecreatetruecolor($new_width, $new_height);
- // Resample, luo uuden kuvan tiedostoon
- imagecopyresampled($image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
- // Tallennetaan uusi kuva määriteltyyn tiedostoon ja annetaan sopiva tiedostopääte
- if($type == 1) // GIF
- {
- imagegif($image, $destination_file);
- }
- elseif($type == 2) // JPEG
- {
- imagejpeg($image, $destination_file);
- }
- elseif($type == 3) // PNG
- {
- imagepng($image, $destination_file);
- }
- }
- // Poistetaan kuva muistista, ei tuhoa alkuperäistä tiedostoa!
- // Palauttaa tiedostotyypin onnistuessaan, FALSE jos ei onnistu
- return $type;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement