Crop Image From Center PHP

后端 未结 4 1046
梦如初夏
梦如初夏 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:34

    Jeez, why are you doing it the hard way? Just simply set the x and y positions as the amount to crop / 2

       $imageSize = getimagesize('thumbnail.png');
    
    $croppedImage = imagecrop(imagecreatefrompng('thumbnail.png'), ['x' => 0, 'y' => ($imageSize[1]-$imageSize[0]*(9/16))/2, 'width' => $imageSize[0], 'height' =>  $imageSize[0]*(9/16)]);
    

    notice how I used my $imageSize[0]*(9/16), which is the amount i am cropping by in the y direction, and i subtracted that from the original image height to find crop amount, then divided by 2. If you want to do the same for width, simply follow the same steps.

提交回复
热议问题