裁剪图像后无法正确显示

Posted

技术标签:

【中文标题】裁剪图像后无法正确显示【英文标题】: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;
  

【问题讨论】:

【参考方案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);

【讨论】:

以上是关于裁剪图像后无法正确显示的主要内容,如果未能解决你的问题,请参考以下文章

无法使用正确的位置或原始 x,y 裁剪图像。

Jcrop 图像在预览中无法正确显示

JCrop 无法正确裁剪图像

裁剪取自 AVCaptureStillImageOutput 的图像

裁剪图像无法正常工作,存储了错误的部分

arcgis裁剪后的图像在ENVI中打开显示是白块是怎么回事啊?