从中心裁剪并在PHP中调整大小


Crop from the center and resize in PHP

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

这就是我目前所得到的:

    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;
}       

该函数运行良好,但从左上角开始裁剪。我想让它从中心裁剪。

您必须计算imagecopyrasampled()的裁剪图像偏移:

在计算新尺寸之前保持原始尺寸:

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

并将您的图像副本重新采样替换为:

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

您可以在这里查看手册。

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

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);