【www.gdgbn.com--jquery】

php+jquery在线切图代码[防dedecms]


 
  
  Cropper
  
  
  
  
  
  <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&2.5.2/build/dragdrop/dragdrop-min.js&2.5.2/build/element/element-beta-min.js&2.5.2/build/resize/resize-beta-min.js&2.5.2/build/imagecropper/imagecropper-beta-min.js&2.5.2/build/connection/connection-min.js&2.5.2/build/json/json-min.js"></script>
  
  
  
  <script type="text/javascript">
   uploader = {
    carry: function(){
     // set form
     YAHOO.util.Connect.setForm("uploadForm", true);
     // upload image
     YAHOO.util.Connect.asyncRequest("POST", "upload.php", {
      upload: function(o){
       // parse our json data
       var jsonData = YAHOO.lang.JSON.parse(o.responseText);
       
       // put image in our image container
       YAHOO.util.Dom.get("imageContainer").innerHTML = "";
       
       // init our photoshop
       photoshop.init(jsonData.image);
       
       // get first cropped image
       photoshop.getCroppedImage();
      }
     });
    }
   };
      
   photoshop = {
    image: null,
    crop: null,
    //url: null,
    
    init: function(image){
     // set our image
     photoshop.image = image;
     
     // our image cropper from the uploaded image
     photoshop.crop = new YAHOO.widget.ImageCropper("yuiImg");
     photoshop.crop.on("moveEvent", function() {
      // get updated coordinates
      photoshop.getCroppedImage();
     });
    },
    
    getCroppedImage: function(){
     var coordinates = photoshop.getCoordinates();
     var url = "crop.php?image=" + photoshop.image + "&cropStartX=" + coordinates.left +"&cropStartY=" + coordinates.top +"&cropWidth=" + coordinates.width +"&cropHeight=" + coordinates.height;
     YAHOO.util.Dom.get("downloadLink").innerHTML = "download cropped image";
    },
    
    getCoordinates: function(){
     return photoshop.crop.getCropCoords();
    }
   };
   
   // add listeners
   YAHOO.util.Event.on("uploadButton", "click", uploader.carry);
  </script>
 
 
  
   
    AJAX image cropper - YUI-based
   
   
   
    


     Image :
     
    

    
    
    
    
   
   
   
    .net">HTML Blog
   
  
 

上面为index.php文件
根据x,y,来用php重新绘图

 // get variables
 $imgfile = $_GET["image"];
 $cropStartX = $_GET["cropStartX"];
 $cropStartY = $_GET["cropStartY"];
 $cropW = $_GET["cropWidth"];
 $cropH = $_GET["cropHeight"];

 // Create two images
 $origimg = imagecreatefromjpeg($imgfile);
 $cropimg = imagecreatetruecolor($cropW,$cropH);

 // Get the original size
 list($width, $height) = getimagesize($imgfile);

 // Crop
 imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

 // force download nes image
 header("Content-type: image/jpeg");
 header("Content-Disposition: attachment; filename="".$imgfile.""");
 imagejpeg($cropimg);

 // destroy the images
 imagedestroy($cropimg);
 imagedestroy($origimg);
?>

这里是图片上的,

 
 if(!empty($_FILES["uploadImage"])) {
    // get file name
  $filename = basename($_FILES["uploadImage"]["name"]);
  
  // get extension
    $ext = substr($filename, strrpos($filename, ".") + 1);
    
    // check for jpg only
    if ($ext == "jpg") {
        // generate unique file name
     $newName = "images/".time().".".$ext;
     
     // upload files
         if ((move_uploaded_file($_FILES["uploadImage"]["tmp_name"], $newName))) {
   
          // get height and width for image uploaded
          list($width, $height) = getimagesize($newName);
          
          // return json data
             echo "{"image" : "".$newName."", "height" : "".$height."", "width" : "".$width."" }";
         }
         else {
             echo "{"error" : "An error occurred while moving the files"}";
         }
    }
    else {
       echo "{"error" : "Invalid image format"}";
    }
 }
?>

本文来源:http://www.gdgbn.com/wangyezhizuo/22966/