裁剪的png图像周围的干预黑色边框

Intervention black borders around cropped png image

我在 laravel 应用程序中使用 Intervention(就像每个人一样)并且遇到了一个问题,即在裁剪图像时在图像周围应用黑色边框,如下所示:

看起来很奇怪,它保留图像文件的透明度,然后用黑色填充图像的其余部分。

我对这些图片进行裁剪保存的方法如下:

/**
 * Accept parameter array (
    '_token' => 'E1seBDvsEBj1aNpLmenIKkAoNSKct878tqnIwOQO',
    'x' => 55,
    'y' => '30',
    'width' => '200',
    'height' => '200',
    'filename' => '1_somerandomhash.jpg',
    )
 *
 * Move the given filename from TMP_ORG_LOGOS to ORGANIZATION_LOGOS_DIRECTORY, apply
 * cropped dimensions, rename to organization slug name plus small random hash, return
 * true or false
 *
 * @param array $params
 * @return bool
 */
public function saveLogo(int $userId, array $params) : bool {
    try{
        $org = $this->userService->getUserDefaultOrg($userId);
        $newImageName = $org->slug . '-' . uniqid() . '.png';
        Image::make(getenv('TMP_ORG_LOGOS') . $params['filename'])
            ->crop(round($params['width']), round($params['height']),
                round($params['x']), round($params['y']))
            ->save(getenv('ORGANIZATION_LOGOS_DIRECTORY') . $newImageName, 100);
        $org->logo = $newImageName;
        $org->save();
        return true;
    } catch (\Exception $e){
        return false;
    }
}

有没有人运行以前遇到过这个问题?有没有办法在干预中让这些黑边变得透明?

编辑

我还应该提到,干预是使用 php 的默认 GD 库进行图像处理。

通过在裁剪后的图像上使用干预 canvas() method. I then used trim() 来创建单独的图像以移除周围的边框,并将该图像插入到新的 canvas 图像的中心,从而解决了这个问题。有点复杂,而且很慢(在服务器上 3-4 秒),但它解决了问题。仍然对更好的解决方案持开放态度(如果存在的话)。

public function saveLogo(int $userId, array $params) : bool {
    try{
        $org = $this->userService->getUserDefaultOrg($userId);
        $newImageName = $org->slug . '-' . uniqid() . '.png';

        $cropped = Image::make(getenv('TMP_ORG_LOGOS') . $params['filename'])
            ->crop(round($params['width']), round($params['height']),
                round($params['x']), round($params['y']))
            ->encode('png', 100)->trim();

        $canvas = \Image::canvas(round($params['width']), round($params['height']));
        $canvas->insert($cropped, 'center');
        $canvas->save(getenv('ORGANIZATION_LOGOS_DIRECTORY') . $newImageName, 100);

        $org->logo = $newImageName;
        $org->save();

        return true;
    } catch (\Exception $e){
        return false;
    }
}
#Importing modules opencv + numpy
import cv2
import numpy as np

#Reading an image (you can use PNG or JPG)
img = cv2.imread('Robo.jpg')
## 0.5 means 50% of image to be scaling
img = cv2.resize(img,None,fx=0.5,fy=0.5)

# black
img1 = np.zeros((600,600,3))
#Getting the bigger side of the image
s = max(img1.shape[0:2])

#Creating a dark square with NUMPY  
f = np.zeros((s,s,3),np.uint8)

#Getting the centering position
ax,ay = (s - img.shape[1])//2,(s - img.shape[0])//2

#Pasting the 'image' in a centering position
f[ay:img.shape[0]+ay,ax:ax+img.shape[1]] = img

#Showing results (just in case) 
cv2.imshow("IMG",f)
cv2.imshow("IMG2",img)
#A pause, waiting for any press in keyboard
cv2.waitKey(0)

#Saving the image
# cv2.imwrite("img2square.png",f)
cv2.destroyAllWindows()