Crop Image From Center PHP

后端 未结 4 1047
梦如初夏
梦如初夏 2020-12-05 05:40

I want to crop an image from the center in the size 200 * 130 the image to be cropped may vary in size, if the image is smaller we wont crop it i know how to this part where

4条回答
  •  执念已碎
    2020-12-05 06:31

    This might help you.

    function cropCentered($img, $w, $h)
    {
      $cx = $img->getWidth() / 2;
      $cy = $img->getHeight() / 2;
      $x = $cx - $w / 2;
      $y = $cy - $h / 2;
      if ($x < 0) $x = 0;
      if ($y < 0) $y = 0;
      return $img->crop($x, $y, $w, $h);
    }
    

    I'm assuming you're using the GD library. $img is a GD image, $w and $h are width and height, respectively, you want your new image to have. In your case, $w = 200, $h = 130.

提交回复
热议问题