4:3 纵横比裁剪在 PHP 与 jQuery 插件


4:3 aspect ratio crop in PHP with jQuery Plugin

我想用PHP将图像裁剪为4:3的纵横比。我在设置中使用 http://odyniec.net/projects/imgareaselect/jQuery 插件,比例为 4:3。

这是php代码,我用于裁剪,但最终裁剪是黑色背景或比例。

<?php
/*
    Author = Sedat Göç
    Description : Crop and Filter Image use PHP and JQuery
    Author Mail : sedatgoc34@gmail.com
    Version : 1.0

*/
require("config.php");

$x = $_POST["x"];
$y = $_POST["y"];
$width = $_POST["width"]; 
$height = $_POST["height"];
$sWidth = $_POST["sWidth"]; 
$sHeight = $_POST["sHeight"]; 
$img = $_POST["img"];
$imageHash = md5($img+rand(0,999999));
$d = explode('/',$img);
$d = $d[count($d)-1];
list($wv, $hv) = getimagesize($imagePath.$d);
$ratio = $wv / $sWidth;

$newH = $width * $ratio;
$newW = $height * $ratio;
$newX = $x * $ratio;
$newY = $y * $ratio;

$image = imagecreatefrompng($imagePath.$d);
$dest = imagecreatetruecolor($newW, $newH);

imagecopy($dest, $image, 0, 0, $newX, $newY ,$newW,$newH);
imagepng($dest,$imageHashPath.$imageHash.'.png');
imagedestroy($image);
$c = explode($dir,($imageHashPath.$imageHash.'.png'));
echo $c[1];
?>

我需要什么才能在jQuery所选区域中获得最佳作物?

     $('#imageS').imgAreaSelect({
      aspectRatio: '4:3', 
      handles: true ,
      onSelectChange: function (img, selection) {
        x = selection.x1;
        y = selection.y1;
        width = selection.width;
        height = selection.height;      
    }
  });
 $("#crop").click(function(){
        if(width>=200){
            var sWidth = $("#imageS").width();
            var sHeight = $("#imageS").height();
            $.ajax({
              type: "POST",
              url: "tool/crop.php",
              data:{x:x,y:y,width:width,height:height,img:img,sWidth:sWidth,sHeight:sHeight},
              success: function(gelencevap){ 
                    $('#imageS').imgAreaSelect({
                            remove:true
                    });
                    $(".panel-title").html("Select Filter");
                    $("#imageS").hide();// hide imageS div
                    $("#crop").hide();// hide crop button
                    $('#imageT').show(); // show imageT div
                    $("#filters").show(); // show filter list
                    $("#imageT").attr("src",gelencevap);
                }
              });
        }
        else
        {
            alert("Please select.(min width 200px)");
        }
    });

我找到了这个问题的解决方案。

<?php
/*
    Author = Sedat Göç
    Description : Crop and Filter Image use PHP and JQuery
    Author Mail : sedatgoc34@gmail.com
    Version : 1.0

*/
require("config.php");
function endimage($IMAGE,$itype='png',$ifile='') {  
switch(strtolower($itype)){  
   case 'gif':  
       imagegif($IMAGE, $ifile);  
    break;  
   case 'jpg':  
   case 'jpeg':  
       imagejpeg($IMAGE, $ifile, '75');  
    break;  
   default:  
       imagepng($IMAGE, $ifile);  
   break;  
  } 
 }    
function endcreate($IMAGE,$itype='png') {  
switch(strtolower($itype)){  
   case 'gif':  
      return imagecreatefromgif($IMAGE);  
    break;  
   case 'jpg':  
   case 'jpeg':  
      return imagecreatefromjpeg($IMAGE);  
    break;  
   default:  
      return imagecreatefrompng($IMAGE);  
   break;  
 } 
}         

$width = $_POST["width"]; 
$height = $_POST["height"];
$sWidth = $_POST["sWidth"]; 
$sHeight = $_POST["sHeight"];  
$x = $_POST['x'];
$y = $_POST['y'];
$img = $_POST["img"]; 
$imageExtension = end(explode('.', $img));
$imageHash = md5($img+rand(0,999999));
$d = explode('/',$img);
$d = $d[count($d)-1];            
list($wv, $hv) = getimagesize($imagePath.$d);

if($width > $height)
{
$ratio = $wv / $sWidth;
$newW = $width * $ratio;
$newH = $height * $ratio;  
$newX = $x1 * $ratio;
$newY = $y1 * $ratio; 
}
else
{
$ratio = $wv / $sWidth;
$newW = $height * $ratio;
$newH = $width * $ratio;  
$newX = $x1 * $ratio;
$newY = $y1 * $ratio; 
} 
$image = endcreate($imagePath.$d,$imageExtension); 
$dest = imagecreatetruecolor($newW, $newH);
imagecopy($dest, $image, 0, 0, $newX, $newY ,$newW,$newH);                    
endimage($dest, $imageExtension, $imageHashPath.$img); 
imagedestroy($image); 
$c = $relativePath.$img.'?timestamp='.time();
echo $c;
?>