1

縦横比を失うことなく、画像の特定の高さと重さにサイズを変更する機能が必要です。最初にトリミングしてからサイズを変更します。

これは私がこれまでに得たものです:

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

関数は正常に動作していますが、左上隅からトリミングされています。中心から切り抜きたい。

4

2 に答える 2

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

ここでマニュアルを確認できます。

于 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 に答える