我一直在尝试使用 PHP 裁剪一些图像并将这些图像转换为 base 64 字符串...我找到了一种可行的方法,但它用黑色填充了我的透明背景!
有人知道更好的方法来实现这一点吗?或者也许有某种方法可以防止我的透明背景pngs
在此过程中被填充。提前致谢!
$image = imagecreatefrompng('/path to my image');
$cropped_image = imagecrop($image, [
'x' => $temp->asset->offset->x,
'y' => $temp->asset->offset->y,
'width' => $temp->asset->width,
'height' => $temp->asset->height
]);
$stream = fopen('php://memory','r+');
imagepng($cropped_image, $stream);
rewind($stream);
$temp->asset->src = 'data:image/png;base64,'.base64_encode(stream_get_contents($stream));
imagedestroy($image);