我正在使用 ckeditor 处理 HTML 文本。当我在其中粘贴图像时,ckeditor 会上传图像。我使用了 TCPDF 和 MPDF 之类的 pdf 库,我遇到了两个明显的错误,每个库一个。
mPDF error: IMAGE Error (SOURCE-IMAGE): Error parsing temporary file image object created with GD library to parse PNG image
TCPDF ERROR: [Image] Unable to get the size of the image: (SOURCE-IMAGE)
我在ckeditor中粘贴时上传图片的代码如下:
<?php
session_start();
class image{
private $save_path = 'uploads/';
private $image_string = '';
private $image_name = '';
private $image;
private $response = array();
public $loaded = false;
public function __construct(){
$this->response = array(
'error' => 1,
'message' => 'unknown error.'
);
$this->image_string = filter_input(INPUT_POST, 'image');
$ext = substr($this->image_string,11,3);
$randomLetters = $rand = substr(md5(microtime()),rand(0,26),6);
$imgnumber = count(scandir($this->save_path));
$this->image_name = "$imgnumber$randomLetters.$ext";
if(!empty($this->image_name) && !empty($this->image_string)){
$this->loaded = true;
}
}
public function save(){
if(!empty($this->image_name) && !empty($this->image_string)){
return $this->progress();
}
else{
$this->response['message'] = 'Error. Not all required infor is given.';
$this->response['error'] = 1;
return $this->response;
}
}
private function progress(){
$imgarr = explode(',', $this->image_string);
if(!isset($imgarr[1])){
$this->response['message'] = 'Error on post data image. String is not the expected string.';
$this->response['error'] = 1;
return $this->response;
}
$this->image = base64_decode($imgarr[1]);
if(!is_null($this->image)){
$file = $this->save_path . $this->image_name;
if(file_exists($file)){
$this->response['message'] = 'Image already exists on server.';
$this->response['error'] = 1;
return $this->response;
}
if(file_put_contents($file, $this->image) !== false){
$this->response['message'] = 'Image saved to server';
$this->response['error'] = 0;
$this->response['source'] = '../plugins/imageuploader/'.$file;
return $this->response;
}
else{
$this->response['error'] = 1;
$this->response['message'] = 'Error writing file to disk';
return $this->response;
}
}
else{
$this->response['message'] = 'Error decoding base64 string.';
return $this->response;
}
}
}
$img = new image();
if($img->loaded){
$result = $img->save();
echo json_encode($result);
}
else{
$result = array(
'error' => 1,
'message' => 'Not all post data given'
);
echo json_encode($result);
}
?>
什么会导致这个错误?
编辑: ajax 代码是 ckeditor 代码的一部分,部分在这里,图像来自 base64 代码:
function h(a, d) {
if (a && "function" === typeof a.getAsFile) {
var b = a.getAsFile(), c = new FileReader;
c.onload = function (a) {
var fd = new FormData();
fd.append('image', a.target.result); //the base64 of image with format equals in src of tag img in html
$.ajax({
type: 'POST',
url: '../plugins/imageuploader/ajaxupload.php',
data: fd,
processData: false,
contentType: false,
dataType: 'json'
}).done(function(data) {
if((data.error == 0) && (typeof data.source != 'undefined')){
//alert(data.source);
var b = d.document.createElement("img", {attributes: {src: data.source}});
setTimeout(function () {
d.insertElement(b)
}, 10)
}else{
alert('Não foi possível carregar a imagem:\nErro - '+data.message); // show the message error if it can't be uploaded.
}
});
};
c.readAsDataURL(b);
}
}
最佳答案
在发送数据之前,您需要对一些 base64 字符进行编码。例如 + 和 = 将导致代码表现不同。 或者使用十六进制值而不是 base64 编码。
编辑: 视形式而定。但是你可以设置一个按钮来使用这个函数转换文本区域:
function escapeChars(){
var value = document.getElementById("myTextarea").value;
value = value.replace("+", "%2b");
value = value.replace("/", "%2f");
value = value.replace("=", "%3d");
document.getElementById("myTextarea").value = value;
}
或者如果您使用 ajax,只需在发送前使用该函数。
在 php 中,还原更改:
$this->image_string = str_replace(array("%2b", "%2f", "%3d"), array("+", "/", "="), $_POST['image']);
代替 $this->image_string = filter_input(INPUT_POST, 'image');
编辑2 在 PHP 中:
<?php
session_start();
class image{
private $save_path = 'uploads/';
private $image_string = '';
private $image_name = '';
private $image;
private $response = array();
public $loaded = false;
public function __construct(){
$this->response = array(
'error' => 1,
'message' => 'unknown error.'
);
$this->image_string = str_replace(array("%2b", "%2f", "%3d"), array("+", "/", "="), $_POST['image']);
$imgarr = explode(',', $this->image_string);
if(!isset($imgarr[1])){
return;
}
$this->image_string = $imgarr[1];
$namearray = explode('.', $imgarr[0]);
$ext = end($namearray);
if(!in_array($ext, array('jpg','png')))
return false;
$randomLetters = $rand = substr(md5(microtime()),rand(0,26),6);
$imgnumber = count(scandir($this->save_path));
$this->image_name = "$imgnumber$randomLetters.$ext";
if(!empty($this->image_name) && !empty($this->image_string)){
$this->loaded = true;
}
}
public function save(){
if(!empty($this->image_name) && !empty($this->image_string)){
return $this->progress();
}
else{
$this->response['message'] = 'Error. Not all required infor is given.';
$this->response['error'] = 1;
return $this->response;
}
}
private function progress(){
$this->image = base64_decode($this->image);
if(!is_null($this->image)){
$file = $this->save_path . $this->image_name;
if(file_exists($file)){
$this->response['message'] = 'Image already exists on server.';
$this->response['error'] = 1;
return $this->response;
}
if(file_put_contents($file, $this->image) !== false){
$this->response['message'] = 'Image saved to server';
$this->response['error'] = 0;
$this->response['source'] = '../plugins/imageuploader/'.$file;
return $this->response;
}
else{
$this->response['error'] = 1;
$this->response['message'] = 'Error writing file to disk';
return $this->response;
}
}
else{
$this->response['message'] = 'Error decoding base64 string.';
return $this->response;
}
}
}
$img = new image();
if($img->loaded){
$result = $img->save();
echo json_encode($result);
}
else{
$result = array(
'error' => 1,
'message' => 'Not all post data given'
);
echo json_encode($result);
}
?>
在发送 ajax 之前在 Javascript 中:
function escapeChars(value){
value = value.replace("+", "%2b");
value = value.replace("/", "%2f");
value = value.replace("=", "%3d");
return value;
}
然后你可以在值中使用escapeChars。
关于php - 图片上传导致PDF库出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41916940/
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数
我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin
文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame
我正在尝试获得良好的Ruby编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用self.。但是现在我偶然发现了这个:classMyClass上面的代码导致错误privatemethodsanitize_namecalled但是当删除self.并仅使用sanitize_name时,它会起作用。这是为什么? 最佳答案 发生这种情况是因为无法使用显式接收器调用私有(private)方法,并且说self.sanitize_name是显式指定应该接收sanitize_name的对象(self),而不是依赖于隐式接收器(也是