我正在尝试实现一个在其他图像上绘制图像的功能。 我总是得到第二张图片,上面是黑色背景,这里是我正在使用的代码
function writeImageOnImage($canvasImage, $x, $y, $drawnImage, $multiplyer = 1, $dynamicProperotions = false){
if(is_resource($canvasImage)){
$canvasImage = $canvasImage;
}else{
$canvasImage = imagecreatefrompng($canvasImage);
}
$drawnImageResource = imagecreatefrompng($drawnImage, true);
$drawnImageResource = $drawnImageResource['image'];
//imagecolortransparent($drawnImageResource, imagecolorallocate($drawnImageResource, 255, 255, 255));
if($dynamicProperotions){
$width = imagesx($drawnImageResource);
$height = imagesy($drawnImageResource);
if($multiplyer > 1){
$width /= $multiplyer;
$height /= $multiplyer;
}
}else{
$width = 20;
$height = 20;
}
imagealphablending($canvasImage, false);
imagesavealpha($canvasImage, true);
//imagecolorallocatealpha($canvasImage, 0, 0, 0, 127);
imagecopy($canvasImage, $drawnImageResource, $x*$multiplyer, $y*$multiplyer, 0, 0, $width*$multiplyer, $height*$multiplyer);
imagedestroy($drawnImageResource);
return $canvasImage;
}
这是我如何调用此函数
writeImageOnImage("test.png", 100, 100, "checkSign.png", 1, true);
" checkSign.png"是从用户html画布的输出创建的(它提供了svg输出)。 我使用Imagick将svg图像转换为png并且代码为:
function createSvgImage($data, $maxWidth, $maxHeight, $fileName){
global $configuration;
$result = array();
$result["file"] = $fileName;
$data = str_replace("data:image/svg+xml;base64", "", $data);
$data = str_replace(" ", "+", $data);
$data = base64_decode($data);
$data = str_replace('stroke="#000000"', 'stroke="#004fca"', $data);
$xml = simplexml_load_string($data);
$attr = $xml->attributes();
$size = [$maxWidth, $maxHeight];
$widthRatio = intval($attr->width) / $size[0];
$heightRatio = intval($attr->height) / $size[1];
$image = new \Imagick();
$image->setBackgroundColor(new \ImagickPixel('transparent'));
if($widthRatio > $heightRatio){
$image->setResolution(72 * ($size[0] / intval($attr->width)),72*((intval($attr->height)*$widthRatio) / intval($attr->height)));
$image->readimageblob($data);
$image->scaleImage($maxWidth,floatval($attr->height)/$widthRatio);
$result['width'] = $maxWidth;
$result['height'] = floatval($attr->height)/$widthRatio;
}else{
$image->setResolution(72*((intval($attr->width)*$heightRatio) / intval($attr->width)),72 * ($size[1] / intval($attr->height)));
$image->readimageblob($data);
$image->scaleImage(floatval($attr->width)/$heightRatio, $maxHeight);
$result['width'] = floatval($attr->width)/$heightRatio;
$result['height'] = $maxHeight;
}
$image->setimagetype(\Imagick::IMGTYPE_TRUECOLOR);
$image->setimageformat("png");
/*$image->setOption('png:color-type', 6);
$image->setOption('png:bit-depth', 8);*/
//$image->writeimage("png16:".$fileName);
$image->writeimage($fileName);
return $result;
}
并使用以下代码启动此功能:
createCanvasImage($_POST['canvas'], 500, 500, "checkSign.png");
提前致谢!