裁剪中心的缩略图图像,宽度和高度不同

时间:2017-07-14 10:04:34

标签: php image position crop

我遇到这个问题,我的裁剪图像没有在缩放图像的中心被裁剪。我现在已经尝试了几个小时没有成功。 任何有关计算和位置的帮助将不胜感激。

这是返回我的值的代码的一部分

    // image dimensions
    $obj->sw = $p_aImageInfo[0];
    $obj->sh = $p_aImageInfo[1];

    //thumbnail sizes
    $obj->tsw = 100;
    $obj->tsh = 200;


    $obj->yOff = 0;
    $obj->xOff = 0;
    if($obj->sw < $obj->sh) {
      $obj->scale = $obj->tsw / $obj->sw;
      $obj->yOff = $obj->sh/2 - $obj->tsw/$obj->scale/2; 
    } else {
      $obj->scale = $obj->tsh / $obj->sh;
      $obj->xOff = $obj->sw/2 - $obj->tsh/$obj->scale/2; 
    }

这是制作新图像的代码

          // Create the resized image destination
      $croppedImage = imagecreatetruecolor($l_oCropInfo->tsw, $l_oCropInfo->tsh);
      imagealphablending($croppedImage, false);
      imagesavealpha($croppedImage,true);
      $transparent = imagecolorallocatealpha($croppedImage, 255, 255, 255, 127);
      imagefilledrectangle($croppedImage, 0, 0, $l_oCropInfo->tsw, $l_oCropInfo->tsh, $transparent);

      // Copy from image source, resize it, and paste to image destination
      imagecopyresampled($croppedImage, $im, 0, 0, $l_oCropInfo->xOff,
                                                   $l_oCropInfo->yOff,
                                                   $l_oCropInfo->tsw, 
                                                   $l_oCropInfo->tsh,
                                                   $l_oCropInfo->tsw / $l_oCropInfo->scale,
                                                   $l_oCropInfo->tsh / $l_oCropInfo->scale);

创建图像的结果和需要创建的图像。

enter image description here

1 个答案:

答案 0 :(得分:0)

好的小错误现在可行了

更改

$obj->yOff = $obj->sh/2 - $obj->tsw/$obj->scale/2; 
$obj->xOff = $obj->sw/2 - $obj->tsh/$obj->scale/2; 

$obj->yOff = $obj->sh/2 - $obj->tsh/$obj->scale/2;
$obj->xOff = ($obj->sw/2 - $obj->tsw/$obj->scale/2);