我知道可以获取画布客户端的图像dataURL,甚至可以使用Node.js将画布渲染到图像文件。
我正在处理分辨率为~9000x9000及以上的图像,并且上述方法对于此尺寸的图像来说速度慢/不可靠。
是否可以读取我可以从fabric.js获取到PHP的JSON数据,并使用像ImageMagick这样的图形库渲染服务器端?
干杯
答案 0 :(得分:1)
将您的JSON数据导出到文件或直接读取 然后使用Imagick
创建一个更大的图像 $print = new Imagick();
$print->setResolution(300, 300);
$background = #ff00ff;
$print->newImage(9000, 9000, new ImagickPixel($background));
$print->setImageFormat('png32');
$print->setImageUnits(imagick::RESOLUTION_PIXELSPERCENTIMETER);
// Re-Scaling each Image/Text for Larger Canvas/Image
foreach ($printData->json_data->objects as $i => $object) {
addImage($object, $print, $printData);
}
function addImage($object, $print, $printData) {
$widthScale = ($printData->width / 300); fabric canvas width
$heightScale = ($printData->height / 500); fabric canvas height
$fileDir = dirname(__FILE__) . DS . 'media' . DS . 'original' . DS;
$src = new Imagick($fileDir . basename($object->src));
$size = $src->getImageGeometry();
$resizeWidth = ($object->width * $object->scaleX) * $widthScale;
$resizeHeight = ($object->height * $object->scaleY) * $heightScale;
$src->resizeImage($resizeWidth, $resizeHeight, Imagick::FILTER_LANCZOS, 1);
$sizeAfterResize = $src->getImageGeometry();
$src->rotateImage(new ImagickPixel('none'), $object->angle);
$sizeAfterRotate = $src->getImageGeometry();
$left = $object->left * $widthScale;
$top = $object->top * $heightScale;
$print->compositeImage($src, Imagick::COMPOSITE_DEFAULT, $left, $top);
}
try {
// Saving High Quality Image in (300 dpi)
$fileDir = dirname(__FILE__) . DS . 'media' . DS . 'prints';
if (!file_exists($fileDir) || !is_dir($fileDir)) {
if (!mkdir($fileDir))
die("Could not create directory: {$fileDir}\n");
}
$saved = $print->writeimage($fileDir . DS . $id . '.png');
header('Content-type: image/png');
echo $print;
} catch (Exception $e) {
echo $e->getMessage();
}