我正在尝试裁剪图像,使用图像的某些部分,但也允许在其周围添加“额外”空间。但是,当裁剪的图像在“额外”空间中生成黑色空间时,我希望它是透明的。
使用cropper JavaScript 获取裁剪坐标:https ://fengyuanchen.github.io/cropperjs/
然后使用 PHP imagecopyresample
d 将图像裁剪为大小。
图像的裁剪很好,但是如果我将图像裁剪为大于原始尺寸,它会在图像周围添加黑色空间,我想将其更改为透明。
研究了在裁剪后的图像中搜索黑色像素并将它们转换为透明的,但是当图像中有黑色时,这个想法就会中断
Current php code: (asuming file type is PNG)
//$cropData
//is an array of data passed through from the cropper containing the original width and height, new width and height and the cropping x and y coordinates.
//passed in image to be cropped
$current_image = "/folder/example.jpg";
//image file location of cropped image
$image_name = "/folder/cropped_example.jpg";
//create blank image of desired crop size
$width = $cropData["width"];
$height = $cropData["height"];
$background = imagecreatetruecolor($width, $height);
//crop coordinates
$crop_x = $cropData["x"];
$crop_y = $cropData["y"];
//create resouce image of current image to be cropped
$image = imagecreatefrompng($current_image);
//crop image
imagecopyresampled($background, $image, 0, 0, $crop_x, $crop_y, $width, $height, $width, $height)){
imagepng($background, $image_name);
//File Uploaded... return to page