尝试使用输入类型文本传递超过 524288 字节的 ToDataURL

2022-12-30php开发问题
0

本文介绍了尝试使用输入类型文本传递超过 524288 字节的 ToDataURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 Canvas 的 DataURL(使用 JavaScript)创建图像.当用户点击提交时,该值会被发送到一个输入类型的文本标签(例如,),然而,显然在 Chrome 上,当它的长度为 524,288 个字符.

我将它发送到输入标签,因为我需要在 PHP 中获取值(作为 $_POST['dataurltext'];),以便我可以创建一个图像并将其上传到我的网络服务器.

关于如何绕过这个长度的任何想法?

我应该改用评论框吗?

感谢您的帮助,不胜感激.

解决方案

Try sent canvas as Blob at javascript;使用 fopen()php://input 作为参数来读取 Blobstream_copy_to_streamfile_get_contents() , file_put_contents()php

处处理文件

参见 HTMLCanvasElement.toBlob()

if (!HTMLCanvasElement.prototype.toBlob) {Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {价值:功能(回调,类型,质量){var binStr = atob( this.toDataURL(type, quality).split(',')[1] ),len = binStr.length,arr = new Uint8Array(len);for (var i=0; i

除了 $_POST, $_GET和 $_FILE:在 JavaScript 和 PHP 中使用 Blob

I am trying to create an image using DataURL of a Canvas (using JavaScript). When a user hits submit, the value gets sent to an Input type text tag (e.g., <input type='text'>), however, apparently on Chrome, the text gets cut off when it is at length 524,288 characters.

I am sending it to an input tag because I need to obtain the value in PHP (as a $_POST['dataurltext'];), so that I can create an image and upload it to my web server.

Any ideas on how to bypass this length?

Should I use a comment box instead, maybe?

Thank you for any help, it will be greatly appreciated.

解决方案

Try sending canvas as Blob at javascript; use fopen() with php://input as parameter to read Blob, stream_copy_to_stream or file_get_contents() , file_put_contents() to process file at php

See HTMLCanvasElement.toBlob()

if (!HTMLCanvasElement.prototype.toBlob) {
 Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
  value: function (callback, type, quality) {

    var binStr = atob( this.toDataURL(type, quality).split(',')[1] ),
        len = binStr.length,
        arr = new Uint8Array(len);

    for (var i=0; i<len; i++ ) {
     arr[i] = binStr.charCodeAt(i);
    }

    callback( new Blob( [arr], {type: type || 'image/png'} ) );
  }
 });
}

Beyond $_POST, $_GET and $_FILE: Working with Blob in JavaScript and PHP

<?php

// choose a filename
$filename = "hello.json";

// the Blob will be in the input stream, so we use php://input
$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb'); 

// Note: we don't need open and stream to stream, we could've used file_get_contents and file_put_contents
stream_copy_to_stream($input, $file);
fclose($input);
fclose($file);

?>

这篇关于尝试使用输入类型文本传递超过 524288 字节的 ToDataURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17