Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.87 KB | None | 0 0
  1. <?php
  2.     require_once('../../config/database.php');
  3.     require_once('utils.php');
  4.     /* Function useless pour toi , c'etait juste pour arranger mon tableau un peu comme je veux pour avoir un truc mieux structure*/
  5.     function rename_key($json_array){
  6.         $object = 0;
  7.         $rotate = 0;
  8.         $array = array();
  9.         foreach($json_array as $el)
  10.         {
  11.             if(gettype($el) == 'object')
  12.             {
  13.                 $array['filter' . $object] = get_object_vars($el);
  14.                 $object++;
  15.             }
  16.             if(gettype($el) == 'string')
  17.             {
  18.                 $array['rotate' . $rotate] = $el;
  19.                 $rotate++;
  20.             }
  21.         }
  22.         $rotate = $rotate - 1;
  23.         $array['video'] = $array['rotate' . $rotate];
  24.         unset($array['rotate' . $rotate]);
  25.         return $array;
  26.     }
  27. /* Ici je transforme ma base64 video que je recois du front en image reelle.*/
  28.     function string_to_img($img){
  29.         $img = str_replace('data:image/png;base64,', '', $img);
  30.         $img = str_replace(' ', '+', $img);
  31.         $uploaded = 0;
  32.         if((stripos($img,"uploaded")) !== FALSE)
  33.         {
  34.             $img = str_replace('uploaded', '', $img);
  35.             $uploaded = 1;
  36.         }
  37.         $data = base64_decode($img);
  38.         if(($result = imagecreatefromstring($data)) == FALSE)
  39.             return FALSE;
  40.         if($uploaded == 0)
  41.             imageflip($result,IMG_FLIP_HORIZONTAL);
  42.         return $result;
  43.     }
  44.     // Tout commence la
  45.     $json = rename_key(json_decode(file_get_contents('php://input'))); // je recupere l'input avec un get content (donc le json) que json_decode pour passer du json a un array php. Je passe a renamekey pour l'arranger un peu.
  46.     if(($video = string_to_img($json['video'])) == FALSE) // je mets ma video base64 en image reelle . je check le retour , si ca echoue je detruit tout , et je renvoi un 401 à l'ajax
  47.     {
  48.         imagedestroy($video);
  49.         header('HTTP/1.1 401 Unauthorized');
  50.         die();
  51.     }
  52.     $nb_filters = (count($json) - 1); // je recupere mon nombre de filtre , en comptant mon nombre de valeurs dans le tableau , je fais -1 parce que y'as la valeur de la video en base64.  
  53.     if($nb_filters <= 0) // si j'ai pas de filtre nique ta mere . erreur 204 a ajax.
  54.     {
  55.         header('HTTP/1.1 204 No Content');
  56.         die();
  57.     }
  58.     $largeur_video = 540;
  59.     $hauteur_video = 405;
  60.     $i = 0; // le compteur des familles
  61.    
  62.     while($i < $nb_filters) // tout le traitement de superposition est ici chef , je boucle sur mon nombre de filtres et je superpose a chaque iteration un nouveau filtre sur la photo finale
  63.     {
  64.         if(($filter = imagecreatefrompng($json['filter' . $i]['src'])) == FALSE) // je recupere mon filtre (j'ai la src de chaque filtre dans le json que j'envoi du front)
  65.         {
  66.             header('HTTP/1.1 204 No Content');
  67.             die();
  68.         }
  69.         $width = imagesx($filter); // je recupere la width reelle du fichier  
  70.         $height = imagesy($filter); // je recupere la height reelle du fichier
  71.         /* ici je recupere les valeurs css width et height que l'utilisateur a resize comme il voulait*/
  72.         $new_width = $json['filter' . $i]['width'];
  73.         $new_height = $json['filter' . $i]['height'];
  74.         $new_image = imagecreatetruecolor($new_width,$new_height); // je creer une image vide de la taille du filtre donnee par lutilisateur
  75.         /* je set la transparence */
  76.         imagealphablending($new_image,false);
  77.         imagesavealpha($new_image,true);
  78.         $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); // je rends tout transparent
  79.         /* les deux prochaines fonctions j'ai oublie depuis c'etait un peu de la magie parce que j'avais des contours noirs autour de mon image et parfois elle se resize pas bien donc je reforcais le resize*/
  80.         imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  81.         imagecopyresampled($new_image,$filter, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  82.         imagedestroy($filter);
  83.         $filter = $new_image;
  84.         /* je recupere la pos de mon filtre sur ma video*/
  85.         $destination_x = $json['filter' . $i]['offsetLeft'];
  86.         $destination_y = $json['filter' . $i]['offsetTop'];
  87.         /* je superpose le filtre sur la video */
  88.         imagecopy($video, $filter, $destination_x, $destination_y, 0, 0, $new_width, $new_height);
  89.         imagedestroy($filter);
  90.         $i++;
  91.     }
  92.     // TRAIMENT DIMAGE TERMINE ICI  . La je fais juste mon Traitement BDD :
  93.     session_start();
  94.     if($_SESSION['auth'] == "" || empty($_SESSION['auth']))
  95.     {
  96.         imagedestroy($video);
  97.         header('HTTP/1.1 401 Unauthorized');
  98.         die();
  99.     }
  100.     $pdo = pdo_connection();
  101.     $req1 = $pdo->prepare('SELECT img FROM usr WHERE username = ?');
  102.     $req1->execute([$_SESSION['auth']]);
  103.     $tot_img = $pdo->query('SELECT COUNT(*) FROM pics')->fetchColumn();
  104.     $nb_userimg = $req1->fetch();
  105.     $check_admin = 0;
  106.     if($_SESSION['auth'] == 'micka')
  107.     {
  108.         $check_admin = 1;
  109.     }
  110.     if(($tot_img > 150 || $nb_userimg->img >= 10) && $check_admin == 0)
  111.     {
  112.         if($nb_userimg->img >= 10)
  113.             echo "<div class='errors'><ul><li>Ton nombre limite de 10 photos est atteint. Tu peux en supprimer dans la galerie. </li></ul></div>";
  114.         if($tot_img > 150)
  115.             echo "<div class='errors'><ul><li>Mickamagru a atteint sa limite de stockage. Veuillez contacter l'administrateur mtordjma@student.42.fr pour résoudre ce soucis.</li></ul></div>";
  116.         imagedestroy($video);
  117.         header('HTTP/1.1 403 Forbidden');
  118.         die();
  119.     }
  120.     else
  121.     {
  122.         if(($user_id = get_user_id($_SESSION['auth'])) == FALSE)
  123.         {
  124.             imagedestroy($video);
  125.             header('HTTP/1.1 401 Unauthorized');
  126.             die();
  127.         }
  128.         date_default_timezone_set('Europe/Paris');
  129.         $new_nb = $nb_userimg->img + 1;
  130.         $req2 = $pdo->prepare("UPDATE usr SET img = ? WHERE username = ?");
  131.         $req2->execute([$new_nb,$_SESSION['auth']]);
  132.         $req3 = $pdo->prepare("INSERT INTO pics SET path = ?, user_id = ?, date_prise = ?");
  133.         $get_path = $pdo->query('SELECT MAX(id) FROM pics')->fetchColumn() + 1;
  134.         $path = "../public/img/picture/img" . $get_path . ".png";
  135.         $req3->execute([$path,$user_id->id,date("Y-m-d H:i:s")]);
  136.         if($get_path == "1")
  137.         {
  138.             $last_id = $pdo->query('SELECT MAX(id) FROM pics')->fetchColumn();
  139.             $path = "../public/img/picture/img" . $last_id . ".png";
  140.             $req4 = $pdo->prepare('UPDATE pics SET path = ? WHERE id = ?');
  141.             $req4->execute([$path,$last_id]);
  142.         }
  143.         imagepng($video,$path);
  144.         imagedestroy($video);
  145.         if($get_path == 1)
  146.             echo $last_id;
  147.         else
  148.             echo $get_path;
  149.         die();
  150.     }
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement