2016-06-03 10 views
0

Ich verwende den folgenden Code PHP, um ein zugeschnittenes Bild von jcrop zu speichern.mein zugeschnittenes Bild ist nur eine schwarz gefärbte Box von 2KB?

$targ_w = $targ_h = 150; 
$jpeg_quality = 90; 

$src = "../profiles/"; 
$src = $target . basename($_FILES['userfile']['name']); 
$img_r = imagecreatefromjpeg($src); 
$dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], 
$targ_w,$targ_h,$_POST['w'],$_POST['h']); 


imagejpeg($dst_r, "simple2.jpg", $jpeg_quality); 

A simple2.jpg nicht im Verzeichnis erhalten gespeichert, aber es ist nur ein schwarz gefärbtes Quadrat der Größe 2 KB. Ich möchte, dass das zugeschnittene Teil gespeichert wird. Wie behebe ich das?

+0

Bitte zeigen Sie Ihre POST variables's Werte . – Jehy

Antwort

0

verwenden

index.php

<html lang="en" > 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="author" content="Script Tutorials" /> 
     <title>HTML5 Image uploader with Jcrop | Script Tutorials</title> 

     <!-- add styles --> 
     <link href="css/main.css" rel="stylesheet" type="text/css" /> 
     <link href="css/jquery.Jcrop.min.css" rel="stylesheet" type="text/css" /> 

     <!-- add scripts --> 
     <script src="js/jquery.min.js"></script> 
     <script src="js/jquery.Jcrop.min.js"></script> 
     <script src="js/script.js"></script> 
    </head> 

    <body> 
     <header> 
      <h2>HTML5 Image uploader with Jcrop </h2> 
     </header> 

     <div class="demo"> 
      <div class="bheader"><h2>-- Image upload form --</h2></div> 

      <div class="bbody"> 
       <!-- upload form --> 
       <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()"> 

        <!-- hidden crop params --> 
        <input type="hidden" id="x1" name="x1" /> 
        <input type="hidden" id="y1" name="y1" /> 
        <input type="hidden" id="x2" name="x2" /> 
        <input type="hidden" id="y2" name="y2" /> 


        <h2>Step1: Please select image file</h2> 
        <div> 
        <input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" /> 
        </div> 

        <div class="error"></div> 



        <div class="step2"> 
         <h2>Step2: Please select a crop region</h2> 
         <img id="preview" /> 

         <div class="info"> 
          <label>File size</label> 
          <input type="text" id="filesize" name="filesize" /> 

          <label>Type</label> 
          <input type="text" id="filetype" name="filetype" /> 

          <label>Image dimension</label> 
          <input type="text" id="filedim" name="filedim" /> 
          <label>W</label> <input type="text" id="w" name="w" /> 
          <label>H</label> <input type="text" id="h" name="h" /> 
         </div> 

         <input type="submit" value="Upload" /> 
        </div> 





       </form> 
      </div> 
     </div> 
    </body> 
</html> 

upload.php

<?php 
function uploadImageFile() { // Note: GD library is required for this function 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     // $iWidth = $iHeight = 200; // desired image result dimensions 

     $iWidth=abs($_POST['x2']-$_POST['x1']); 
     $iHeight=abs($_POST['y2']-$_POST['y1']); 



     $iJpgQuality = 90; 

     if ($_FILES) { 

      // if no errors and size less than 250kb 
      if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024)    { 
       if (is_uploaded_file($_FILES['image_file']['tmp_name'])) { 

        // new unique filename 
        $sTempFileName = 'cache/' . md5(time().rand()); 

       // mkdir("cache",0664,true); 
        chmod('cache', 0664); 

        //echo $url = $_SERVER['SERVER_NAME'] . dirname(__FILE__); 
        //echo '<br>'; 
        $root=$_SERVER['DOCUMENT_ROOT']; 


        $newsTempFileName = $root.'/beta/nikhil/demo/jquery-images-direct-crop/'.$sTempFileName; 





        // move uploaded file into cache folder 
        move_uploaded_file($_FILES['image_file']['tmp_name'], $newsTempFileName); 



        // change file permission to 644 
        @chmod($sTempFileName, 0644); 



        if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) { 
         $aSize = getimagesize($sTempFileName); // try to obtain image info 
         if (!$aSize) { 
          @unlink($sTempFileName); 
          return; 
         } 



         // check for image type 
         switch($aSize[2]) { 
          case IMAGETYPE_JPEG: 
           $sExt = '.jpg'; 

           // create a new image from file 
           $vImg = @imagecreatefromjpeg($sTempFileName); 
           break; 
          case IMAGETYPE_PNG: 
           $sExt = '.png'; 

           // create a new image from file 
           $vImg = @imagecreatefrompng($sTempFileName); 
           break; 
          default: 
           @unlink($sTempFileName); 
           return; 
         } 

         // create a new true color image 
         $vDstImg = @imagecreatetruecolor($iWidth, $iHeight); 

         // copy and resize part of an image with resampling 
         imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']); 

         // define a result image filename 
         $sResultFileName = $sTempFileName . $sExt; 

         // output image to file 
         imagejpeg($vDstImg, $sResultFileName, $iJpgQuality); 
         @unlink($sTempFileName); 

         return $sResultFileName; 
        } 
       } 
      } 
     } 
    } 
} 

$sImage = uploadImageFile(); 
echo '<img src="'.$sImage.'" />'; 
?> 

Live Demo: https://www.script-tutorials.com/demos/316/index.html Add jquery und CSS

+0

verwenden Sie auch diese https://www.script-tutorials.com/html5-image-uploader-with-jcrop/ –

+0

Ich bekomme den gleichen Fehler. Ich fühle mich wie ich einen dummen Fehler mache :( –

+0

Ok, ich löste es. In meinem HTML, wo das Bild lädt, hatte ich die Abmessungen als 200 * 200 angegeben. Daher das Problem. Dieser Code funktioniert nur, wenn das gesamte Bild ist Ich denke, ich muss es zuerst Größe ändern. –