Advertisement
Guest User

Bildbearbeitung

a guest
Apr 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.43 KB | None | 0 0
  1. <?php
  2.     // FUNCTION IRGENDWO DEFINIEREN
  3.     function save_thumbnail($file, $uploaddir, $thumbwidth) {
  4.         $imageinfo = getimagesize($uploaddir.$file);
  5.  
  6.         $img = null;
  7.         switch($imageinfo[2]){
  8.             case IMAGETYPE_PNG:
  9.                 $img = imagecreatefrompng($uploaddir.$file);
  10.                 break;
  11.             case IMAGETYPE_JPEG:
  12.                 $img = imagecreatefromjpeg($uploaddir.$file);
  13.                 break;
  14.             case IMAGETYPE_GIF:
  15.                 $img = imagecreatefromgif($uploaddir.$file);
  16.                 break;
  17.         }
  18.  
  19.         $width = imagesx($img);
  20.         $height = imagesy($img);
  21.  
  22.         $thumbheight = floor($height * ($thumbwidth / $width));
  23.  
  24.         $thumbnail = imagecreatetruecolor($thumbwidth, $thumbheight);
  25.  
  26.         imagecopyresampled($thumbnail, $img, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
  27.  
  28.         $filename_expl = explode(".",$file);
  29.         $thumb_filename = $filename_expl[0]."_thumbnail.".$filename_expl[1];
  30.         switch ($imageinfo[2]) {
  31.             case IMAGETYPE_PNG:
  32.                 imagepng($thumbnail, $uploaddir.$thumb_filename);
  33.                 break;
  34.             case IMAGETYPE_JPEG:
  35.                 imagejpeg($thumbnail, $uploaddir.$thumb_filename);
  36.                 break;
  37.             case IMAGETYPE_GIF:
  38.                 imagegif($thumbnail, $uploaddir.$thumb_filename);
  39.                 break;
  40.         }
  41.  
  42.         imagedestroy($thumbnail);
  43.         imagedestroy($img);
  44.     }
  45. ?>
  46.  
  47. // edit_product.php
  48. <?php
  49.     $file = $_GET['file'];
  50.     $_SESSION['imageedit_file'] = $file;
  51.     if(!isset($_SESSION['imageedit_actions'])) {
  52.         $_SESSION['imageedit_actions'] = array();
  53.     }
  54.     if(isset($_POST['action'])){
  55.         switch($_POST['action']){
  56.             case 'undo':
  57.                 array_pop($_SESSION['imageedit_actions']);
  58.                 break;
  59.             case 'crop':
  60.                 $_SESSION['imageedit_actions'][] = array('crop',$_POST['x'],$_POST['y'],$_POST['width'],$_POST['height']);
  61.                 break;
  62.             default:
  63.                 $_SESSION['imageedit_actions'][] = $_POST['action'];
  64.         }
  65.     }
  66. ?>
  67. <div id="imageedit_div">
  68.  
  69.     <div class="row">
  70.         <div class="col-md-2"></div>
  71.         <div class="col-md-6">
  72.             <div id="editimage_show">
  73.                 <img src="content/editimage.php?sid=<?php echo session_id(); ?>" />
  74.                 <div id="editimage_croparea" hidden=true></div>
  75.             </div><br />
  76.         </div>
  77.  
  78.         <div class="col-md-2">
  79.             <button class="imageedit_action button" onclick="changeAction('gray')">
  80.                 <i class="fa fa-paint-brush" aria-hidden="true"></i> Grayscale
  81.             </button><br />
  82.             <button class="imageedit_action button" onclick="changeAction('turn_left')">
  83.                 <i class="fa fa-rotate-left" aria-hidden="true"></i> Rotate Left
  84.             </button><br />
  85.             <button class="imageedit_action button" onclick="changeAction('turn_right')">
  86.                 <i class="fa fa-rotate-right" aria-hidden="true"></i> Rotate Right
  87.             </button><br />
  88.             <button class="imageedit_action button" onclick="changeAction('mirror')">
  89.                 <i class="fa fa-exchange" aria-hidden="true"></i> Mirror
  90.             </button><br />
  91.             <button class="imageedit_action button" onclick="showCrop()" id="editimage_crop">
  92.                 <i class="fa fa-crop" aria-hidden="true"></i> Crop
  93.             </button>
  94.  
  95.             <button class="imageedit_action button crop_active_button" onclick="sendCropInfo()" hidden=true>
  96.                 <i class="fa fa-crop" aria-hidden="true"></i> Save
  97.             </button>
  98.             <button class="imageedit_action button crop_active_button" onclick="hideCrop()" hidden=true>
  99.                 <i class="fa fa-crop" aria-hidden="true"></i> Cancel
  100.             </button>
  101.             <br />
  102.  
  103.             <button class="imageedit_action button" onclick="changeAction('undo')">
  104.                 <i class="fa fa-history" aria-hidden="true"></i> Undo
  105.             </button><br /><br />
  106.  
  107.             <button class="imageedit_button button" onclick="toIndexPage()">Cancel</button>
  108.             <button class="imageedit_button button" onclick="changeAction('save')">Save Image</button>
  109.         </div>
  110.         <div class="col-md-2"></div>
  111.     </div>
  112. </div>
  113.  
  114.  
  115. // editimage.php
  116. <?php
  117.     session_id($_REQUEST['sid']);
  118.     session_start();
  119.     include_once "../conf.php";
  120.     include_once "../helpers.php";
  121.     $file = $_SESSION['imageedit_file'];
  122.     $actions = $_SESSION['imageedit_actions'];
  123.     $imageinfo = getimagesize($UPLOAD_DIR.$file);
  124.  
  125.     $img = null;
  126.     switch($imageinfo[2]){
  127.         case IMAGETYPE_PNG:
  128.             $img = imagecreatefrompng($UPLOAD_DIR.$file);
  129.             break;
  130.         case IMAGETYPE_JPEG:
  131.             $img = imagecreatefromjpeg($UPLOAD_DIR.$file);
  132.             break;
  133.         case IMAGETYPE_GIF:
  134.             $img = imagecreatefromgif($UPLOAD_DIR.$file);
  135.             break;
  136.     }
  137.  
  138.     header ("Content-type: {$imageinfo['mime']}");
  139.  
  140.     if ($img){
  141.         foreach($actions as $action) {
  142.             if(is_array($action)){
  143.                 // Crop
  144.                 $crop_img = imagecreatetruecolor($action[3], $action[4]);
  145.                 imagecopy( $crop_img , $img , 0, 0, $action[1] , $action[2] , $action[3] ,$action[4] );
  146.                 $img = $crop_img;
  147.             }else {
  148.                 // Other actions
  149.                 switch ($action) {
  150.                     case "gray":
  151.                         imagefilter($img, IMG_FILTER_GRAYSCALE);
  152.                         break;
  153.                     case "turn_right":
  154.                         $img = imagerotate($img, 270, 0);
  155.                         break;
  156.                     case "turn_left":
  157.                         $img = imagerotate($img, 90, 0);
  158.                         break;
  159.                     case "mirror":
  160.                         imageflip($img, IMG_FLIP_HORIZONTAL);
  161.                         break;
  162.                 }
  163.             }
  164.         }
  165.  
  166.         if(array_values(array_slice($actions, -1))[0] == "save"){
  167.             switch ($imageinfo[2]) {
  168.                 case IMAGETYPE_PNG:
  169.                     imagepng($img,$UPLOAD_DIR.$file);
  170.                     break;
  171.                 case IMAGETYPE_JPEG:
  172.                     imagejpeg($img,$UPLOAD_DIR.$file);
  173.                     break;
  174.                 case IMAGETYPE_GIF:
  175.                     imagegif($img,$UPLOAD_DIR.$file);
  176.                     break;
  177.             }
  178.             save_thumbnail($file, $UPLOAD_DIR, $THUMB_WIDTH);
  179.         }
  180.         switch ($imageinfo[2]) {
  181.             case IMAGETYPE_PNG:
  182.                 imagepng($img);
  183.                 break;
  184.             case IMAGETYPE_JPEG:
  185.                 imagejpeg($img);
  186.                 break;
  187.             case IMAGETYPE_GIF:
  188.                 imagegif($img);
  189.                 break;
  190.         }
  191.  
  192.  
  193.         imagedestroy($img);
  194.     }
  195.  
  196. ?>
  197.  
  198. //editimage_changeaction.js (in index.php einbinden!)
  199. function changeAction(action){
  200.     var formData = new FormData();
  201.     formData.append("action", action);
  202.     $.ajax({
  203.         type: "POST",
  204.         data: formData,
  205.         url: window.location.href,
  206.         cache: false,
  207.         contentType: false,
  208.         processData: false,
  209.         success: function(response, error){
  210.             editActiontransferComplete(response, error);
  211.             if(action == 'save'){
  212.                 toIndexPage();
  213.             }
  214.         }
  215.     });
  216. }
  217.  
  218. function editActiontransferComplete(response, error) {
  219.     //console.log(response);
  220.     $("#editimage_show img").attr('src', $("#editimage_show img",response).attr('src'));
  221. }
  222.  
  223. function toIndexPage(){
  224.     window.location.href = location.protocol + '//' + location.host + location.pathname;
  225. }
  226.  
  227. function showCrop(){
  228.     $(".crop_active_button").prop("hidden", false);
  229.     $("#editimage_crop").prop("hidden", true);
  230.  
  231.     var img_width = $('#editimage_show img').width();
  232.     var img_height = $('#editimage_show img').height();
  233.     var min_width = 50;
  234.     var min_height = 50;
  235.     $("#editimage_show").width(img_width).height(img_height);
  236.     $("#editimage_croparea").prop("hidden", false)
  237.         .resizable({ helper: '.ui-resizable-helper',
  238.                     minHeight: min_height,
  239.                     minWidth: min_width,
  240.                     aspectRatio: true,
  241.                     containment:  "parent"  })
  242.         .draggable({ containment: "parent" });
  243. }
  244.  
  245. function hideCrop(){
  246.     $(".crop_active_button").prop("hidden", true);
  247.     $("#editimage_crop").prop("hidden", false);
  248.  
  249.     $("#editimage_croparea").prop("hidden", true)
  250.         .resizable("destroy")
  251.         .draggable("destroy");
  252. }
  253.  
  254. function sendCropInfo(){
  255.     var position = $('#editimage_croparea').position();
  256.     var width = $('#editimage_croparea').width();
  257.     var height = $('#editimage_croparea').height();
  258.     var x = position.left - 15;
  259.     var y = position.top;
  260.  
  261.     hideCrop();
  262.     var formData = new FormData();
  263.     formData.append("action", "crop");
  264.     formData.append("x", x);
  265.     formData.append("y", y);
  266.     formData.append("width", width);
  267.     formData.append("height", height);
  268.     $.ajax({
  269.         type: "POST",
  270.         data: formData,
  271.         url: window.location.href,
  272.         cache: false,
  273.         contentType: false,
  274.         processData: false,
  275.         success: function(response, error){
  276.             editActiontransferComplete(response, error);
  277.         }
  278.     });
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement