Advertisement
toritoesinocente

ejemplo de subida de imágenes para tinymce

Mar 21st, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2.    /* ejemplo de subida de imágenes para tinymce */
  3.    require_once(__DIR__."/config.php");
  4.    $imageFolder = __DIR__."/up/";
  5.    reset ($_FILES);
  6.    $temp = current($_FILES);
  7.    if (is_uploaded_file($temp['tmp_name'])){
  8.       if (isset($_SERVER['HTTP_ORIGIN'])) {
  9.          // same-origin requests won't set an origin. If the origin is set, it must be valid.
  10.          if (in_array($_SERVER['HTTP_ORIGIN'], $sitios)) {
  11.             header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  12.          } else {
  13.             header("HTTP/1.0 403 Origin Denied");
  14.             return;
  15.          }
  16.       }
  17.       // Verify extension
  18.       if(!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png","jpeg","gif"))) {
  19.          header("HTTP/1.0 500 Invalid extension.");
  20.          return;
  21.       }
  22.       // Accept upload if there was no origin, or if it is an accepted origin
  23.       $pato = $imageFolder.date('Y/m/');
  24.       $ext = strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION));
  25.       $file =    uniqid()."_o.".$ext;
  26.       $filet= str_replace("_o","",$file);
  27.       $filete=str_replace("_o","_t",$file);
  28.       //
  29.       if(!preg_match("/\.jpg$/",$filet)) { $filet = str_replace(".".$ext,".jpg",$filet); }
  30.       if(!preg_match("/\.jpg$/",$filete)) { $filete = str_replace(".".$ext,".jpg",$filete); }
  31.       //
  32.       if(!is_dir($pato)) mkdir($pato,0755,true);
  33.       //$filetowrite = $imageFolder . $temp['name'];
  34.       $filetowrite = $pato.$file;
  35.       move_uploaded_file($temp['tmp_name'], $filetowrite);
  36.       $t = `identify $filetowrite`;
  37.       $s = explode(" ",$t);
  38.       if(in_array($s[1],['JPG','JPEG','PNG','GIF'])) {
  39.          $ext = trim(strtolower($s[1]));
  40.          $ss = explode("x",$s[2]);
  41.          if($ss[0]>=720 && $ext!='gif') {
  42.             //
  43.             $cmd = "convert -thumbnail 720 -flatten -quality 70 -background white -auto-orient ".$filetowrite."[0] ".$pato.$filet; $cmd = `$cmd`;
  44.             $cmd = "convert -thumbnail 200 -flatten -quality 60 ".$pato.$filet." ".$pato.$filete; $cmd = `$cmd`;
  45.             $size=720;
  46.          } elseif($ss[0]>=200 && $ext!='gif') {
  47.             $cmd = "convert -thumbnail ".$ss[0]." -flatten -quality 70 -background white -auto-orient ".$filetowrite."[0] ".$pato.$filet; $cmd = `$cmd`;
  48.             $cmd = "convert -thumbnail 200 -flatten -quality 60 ".$pato.$filet." ".$pato.$filete; $cmd = `$cmd`;
  49.             $size=$ss[0];
  50.          } else {
  51.             if(!empty($ss[0])) $size=$ss[0];
  52.             copy($filetowrite,$filet);
  53.             $cmd = "convert -thumbnail 200 -flatten -quality 60 -background white ".$filetowrite."[0] ". $pato.$filete; $cmd = `$cmd`;
  54.          }
  55.          echo json_encode(array('location' => $sitio."up/".date('Y/m/').$filet));
  56.       }
  57.    } else {
  58.       header("HTTP/1.0 500 Server Error");
  59.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement