【问题标题】:Crop from the center and resize in PHP从中心裁剪并在 PHP 中调整大小
【发布时间】:2012-08-16 14:58:28
【问题描述】:

我想要一个功能,可以在不丢失纵横比的情况下调整图像的特定高度和重量。所以首先我想裁剪它然后调整它的大小。

这是我目前得到的:

    function image_resize($src, $dst, $width, $height, $crop=1){

  if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type!";

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

  // resize
  if($crop){
    if($w < $width or $h < $height) return "Picture is too small!";
    $ratio = max($width/$w, $height/$h);
    $h = $height / $ratio;
    $x = ($w - $width / $ratio) / 2;
    $w = $width / $ratio;
  }
  else{
    if($w < $width and $h < $height) return "Picture is too small!";
    $ratio = min($width/$w, $height/$h);
    $width = $w * $ratio;
    $height = $h * $ratio;
    $x = 0;
  }

  $new = imagecreatetruecolor($width, $height);

  // preserve transparency
  if($type == "gif" or $type == "png"){
    imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
    imagealphablending($new, false);
    imagesavealpha($new, true);
  }

  imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

  switch($type){
    case 'bmp': imagewbmp($new, $dst); break;
    case 'gif': imagegif($new, $dst); break;
    case 'jpg': imagejpeg($new, $dst); break;
    case 'png': imagepng($new, $dst); break;
  }
  return true;
}       

该功能运行良好,但从左上角裁剪。 我希望它从中心裁剪。

【问题讨论】:

    标签: php image resize crop


    【解决方案1】:

    您必须为 imagecopyresampled() 计算裁剪后的图像偏移:

    在计算新之前保持原始大小:

    // resize
    $originalW = $w;
    $originalH = $h;
    if ($crop) {
    ...
    

    并将您的 imagecopyresampled 替换为:

    imagecopyresampled($new, $img, 0, 0, ($originalW - $width)/2, ($originalH - $height)/2, $width, $height, $w, $h);
    

    您可以查看手册here

    【讨论】:

    • 我更新了 imagecopyresampled 调用,参数顺序错误,抱歉
    • 谢谢,它现在可以工作了。但我还是有问题。例如:当我将(300 × 450)的肖像图像调整为(260 x 140)时,我会得到一个我不想要的黑色侧边栏。这是两张图片:postimage.org/image/6vvq5l5snpostimage.org/image/5vywa020h
    【解决方案2】:

    我解决了图像中的黑色区域。现在这个脚本调整大小、裁剪和居中。 最终代码在这里:

    if(!list($w, $h) = getimagesize($src)) return array(false,"Unsupported picture type!");
    
    $type = strtolower(substr(strrchr($src,"."),1));
    if($type == 'jpeg') $type = 'jpg';
    
    switch($type){
        case 'bmp': $img = imagecreatefromwbmp($src); break;
        case 'gif': $img = imagecreatefromgif($src); break;
        case 'jpg': $img = imagecreatefromjpeg($src); break;
        case 'png': $img = imagecreatefrompng($src); break;
        default : return array(false,"Unsupported picture type!");
    }
    
    // resize
    $originalW = $w;
    $originalH = $h;
    if($crop){
        if ($w < $width or $h < $height) return array(false,"Picture is too small! Min size $width px X $height px");
        $ratio = max($width/$w, $height/$h);
        $h = $height / $ratio;
        $x = ($w - $width / $ratio) / 2;
        $w = $width / $ratio;
    } else {
        if($w < $width and $h < $height) return array(false,"Picture is too small! Min size $width px X $height px");
        $ratio = min($width/$w, $height/$h);
        $width = $w * $ratio;
        $height = $h * $ratio;
        $x = 0;
    }
    
    $new = imagecreatetruecolor($width, $height);
    
    // preserve transparency
    if($type == "gif" or $type == "png"){
        imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
        imagealphablending($new, false);
        imagesavealpha($new, true);
    }
    imagecopyresampled($new, $img, 0, 0, ($w - $width)/2, ($h - $height)/2,  $width,  $height, $w - (($w - $width)/2), $h - (($h - $height)/2) );
    
    switch($type){
        case 'bmp': imagewbmp($new, $dst); break;
        case 'gif': imagegif($new, $dst); break;
        case 'jpg': imagejpeg($new, $dst); break;
        case 'png': imagepng($new, $dst); break;
    }
    return array(true,$dst);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 2011-11-11
      • 1970-01-01
      • 2011-09-23
      • 2015-03-05
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多