1

I want to have a function that resizes to a specific height en weight of an image without losing the aspect ratio. So first i want to crop it and then resizing it.

This is what i got so far:

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

The function is working fine, but its crops from the upper leftcorner. I want it to crop from the center.

4

2 回答 2

1

You have to calculate cropped image shift for imagecopyresampled():

Keep original size before you calculate new:

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

And replace Your imagecopyresampled by this:

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

You can check manual here.

于 2012-08-21T09:43:34.820 回答
0

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

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);
于 2014-11-13T14:48:59.050 回答