【问题标题】:After crop image not show properly裁剪图像后无法正确显示
【发布时间】:2015-02-10 22:14:40
【问题描述】:

我正在尝试使用 jcrop 裁剪图像。我的裁剪过程有效,但裁剪后图像的高度和宽度无法正常工作。图像始终显示空白阴影。

after crop image look like

original image look like

我的代码是

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
 $targ_w =  $_POST['w'];$targ_h = $_POST['h'];
 $jpeg_quality = 90;
 $src = 'amazonaws/'.$_POST['rq'];

  $type = strtolower(substr(strrchr($src,"."),1));
  if($type == 'jpeg') $type = 'jpg';
  switch($type){
    case 'bmp': $img_r = imagecreatefromwbmp($src); break;
    case 'gif': $img_r = imagecreatefromgif($src); break;
    case 'jpg': $img_r = imagecreatefromjpeg($src); break;
    case 'png': $img_r = imagecreatefrompng($src); break;
    default : return "Unsupported picture type!";
  } 

    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );


  if($type == "gif" or $type == "png"){
    imagecolortransparent($dst_r, imagecolorallocatealpha($dst_r, 0, 0, 0, 127));
    imagealphablending($dst_r, false);
    imagesavealpha($dst_r, true);
  } 
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);


 switch($type){
    case 'bmp': imagewbmp($dst_r,$src); break;
    case 'gif': imagegif($dst_r,$src); break;
    case 'jpg': imagejpeg($dst_r,$src,$jpeg_quality); break;
    case 'png': imagepng($dst_r,$src); break;
  }
}

【问题讨论】:

    标签: php image jcrop


    【解决方案1】:

    这是 imagecopyresampled() 原型:

    bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
    

    在你的脚本中,你指定

    $targ_w =  $_POST['w']; $targ_h = $_POST['h'];
    

    当你打电话时

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

    您是在告诉复制函数源图像与目标图像的大小相同

    您需要使用

    获取原始图像尺寸
    list($src_width, $src_height, $src_type, $src_attr) = getimagesize($src) ;
    

    并在复制功能中使用这些尺寸

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

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 1970-01-01
      • 2012-01-22
      • 2011-08-03
      • 1970-01-01
      • 2016-04-15
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多