简体   繁体   English

如何使用PHP将画布另存为PNG图像?

[英]How to save canvas as PNG image using PHP?

I'm passing user's score to make-image.php file using GET. 我正在使用GET将用户的分数传递给make-image.php文件。 I want the score passed on top of myCanvas background image. 我想将分数传递到myCanvas背景图像之上。 How to save this 2 items (background image) and the canvas (score) as 1 PNG image in a folder using PHP? 如何使用PHP将这2个项目(背景图像)和画布(分数)另存为1个PNG图像在文件夹中?

I'm using Canvas2Image scripts but I don't know how to do it. 我正在使用Canvas2Image脚本,但我不知道该怎么做。

Please help. 请帮忙。 I'm stucked. 我被困住了。

This the make-image.php file: 这个make-image.php文件:

<?php $score = $_GET['score']; ?>
<html>
    <head>
    <script src="base64.js"></script>
    <script src="canvas2image.js"></script>
    <style type="text/css">

    body { margin: 0; }

    </style>
    </head>
    <body>
        <canvas id="myCanvas" width="300" height="300" style="border:1px solid #d3d3d3;background:url(http://i.imgur.com/PWSOy.jpg);"></canvas>
        <script type="text/javascript">

            var score = <?php echo $score; ?>;
            var canvas = document.getElementById('myCanvas');
            var context = canvas.getContext('2d');
            var x = canvas.width / 2;
            var y = canvas.height / 2;

            context.font = '30pt Calibri';
            context.textAlign = 'center';
            context.fillStyle = 'black';
            context.fillText(score, x, y);

      </script>
    </body>
</html>

Recently I programmed something for that no need for external libraries or something it's just a few lines of code 最近,我编写了一些不需要外部库的东西,或者只是几行代码

The $_POST['signature'] below would contain your base64 encoded image 下面的$_POST['signature']将包含您的base64 encoded图像

define("UPLOAD_DIR", "images/signatures/");
$signature = $_POST['signature'];
$signature = str_replace('data:image/png;base64,', '', $signature);
$signature = str_replace(' ', '+', $signature);
$data = base64_decode($signature);
$file = UPLOAD_DIR . md5(microtime().date('Y-m-d')).'.png';
$success = file_put_contents($file, $data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM