dtr84664 2012-08-21 16:40 采纳率: 100%
浏览 51
已采纳

从中心裁剪并在PHP中调整大小(功能)

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
    $originalW = $w;
    $originalH = $h;

  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, ($originalW - $width)/2, ($originalH - $height)/2, $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 i still have a problem. For example: When i resize a portrait image thats (300 × 450) to (260 x 140) i get a black side bar which i dont want.

Here are the 2 images:

postimage.org/image/6vvq5l5snenter image description here

  • 写回答

2条回答 默认 最新

  • dongping8572 2014-04-16 06:00
    关注

    It worked for me. You can try:

    $x=288; $y=202; // my final thumb
    $ratio_thumb=$x/$y; // ratio thumb
    
    list($xx, $yy) = getimagesize($image); // original size
    $ratio_original=$xx/$yy; // ratio original
    
    if ($ratio_original>=$ratio_thumb) {
        $yo=$yy; 
        $xo=ceil(($yo*$x)/$y);
        $xo_ini=ceil(($xx-$xo)/2);
        $xy_ini=0;
    } else {
        $xo=$xx; 
        $yo=ceil(($xo*$y)/$x);
        $xy_ini=ceil(($yy-$yo)/2);
        $xo_ini=0;
    }
    
    imagecopyresampled($thumb, $source, 0, 0, $xo_ini, $xy_ini, $x, $y, $xo, $yo);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据