附加base64 img时获取黑色图片

ch32k0

我正在尝试发送带有图片附件的电子邮件我可以发送电子邮件,但是显示图片(仅黑色)没有成功。所述php正在接收一个base64字符串,它是一个图像。另外我想这样做没有任何存储或写入服务器上的任何东西。有没有办法做到这一点?
这是个什么php样子。

   <?php 
    $base64 = $_POST['dataURL'];
    //define the receiver of the email 
    $to = '[email protected]'; 
    //define the subject of the email 
    $subject = 'Test email with attachment'; 
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From:Francisco Granado\r\nReply-To: [email protected]"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = base64_decode($base64); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    This email have attached a image file 
    For SED_WE analysis purposes. 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    <h1>SED_WE</h1>
    <p>This email have attached a image file </p> 
    <p>For SED_WE analysis purposes.</p> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: image/jpeg; name="attachment.jpeg"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  

    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
   ?>

这也是我的js:

$(document).ready(function (e) {
    var imgBase64;
    function imgToBase64(url, callback, outputFormat) {
        var canvas = document.createElement('CANVAS'); //Creates variable containing canvas in the DOM
        var canvas_context = canvas.getContext('2d'); //Creates canvas environment (context) in 2 dimensions 
        var img = new Image; // new Image instance
        img.crossOrigin = 'Anonymous'; //Cross origin textures from other URL's are permitted (see http://blog.chromium.org/2011/07/using-cross-domain-images-in-webgl-and.html) for more information
        console.log('ON LOAD IMG');
        //Image load trigger, encode image into base64
        img.onload = function () {
            //Determine canvas height and width 
            canvas.height = img.height;
            canvas.width = img.width;
            console.log('Canvas info:\n Height: ' + canvas.height + '\n Width: ' + canvas.width);
            //Draws the image raw format 
            canvas_context.drawImage(img, 0, 0);
            var dataURL = canvas.toDataURL(outputFormat || 'image/png'); //THIS IS WHERE THE MAGIC HAPPENS (img encode to base64)
            //imgBase64 = dataURL;
            $('#show-picture').attr('src', dataURL); //Change source of the #show-picture image tag to the base64 string to show preview to user 
            console.log(dataURL);
            callback.call(this, dataURL); //Callback function @param {dataURL} 
            // Clean up
            canvas = null;
        };
        img.src = url;
    }

    //Reads the path to the picture for a preview 
    function readURL(input) {
        if (input.files && input.files[0]) { //if file exist
            var reader = new FileReader(); //new instance of the file reader

            reader.onload = function (e) { //when the reader loads the chosen file...
                imgToBase64(e.target.result, function (dataURL) { //..then the is converted 
                    setDataURL(dataURL);
                });
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    function setDataURL(dataURL){
        imgBase64 = dataURL;
        console.log('setDataUrl=>'+imgBase64);
    }



    $('#take-picture').change(function () {
        readURL(this);
        console.log('ONLOAD'+imgBase64);
    });

    $('#uploadImgBtn').on('click',function(){
        var base64String= imgBase64.replace(/data:image[^;]+;base64,/,'');
        console.log('base64string'+base64String);
        $.post('php/mailer.php',base64String,function(data){alert('Success!'); console.log('response: '+data)});
    });

});

PS:我尝试了与其他文章不同的方法,但是没有成功,或者它们写在服务器上,这也是我第一次使用php,因此欢迎任何好的建议。

ch32k0

在将数据发送到的javascript文件中,php没有键名dataURL,我需要data:image/*;base64,从字符串中删除掉以发送纯base64。所以它应该是这样的:

$('#uploadImgBtn').on('click',function(){
        var base64String= imgBase64.replace(/data:image[^;]+;base64,/,'');
        console.log('base64string'+base64String);
        $.post('php/mailer.php',{dataURL:base64String},function(data){
            alert('Success!');
            console.log('response: '+data)});
    });

然后,php将收到,dataURL而我要做的就是chunk_split该base64字符串。像这样:

  $base64 = $_POST['dataURL'];
   ...
  $attachment = chunk_split($base64);

  ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

图片到base64

来自分类Dev

抓取图片网址src时,获取数据:image / jpeg; base64

来自分类Dev

Base64上传图片问题

来自分类Dev

从URL获取图像字节/ base64

来自分类Dev

从Salesforce获取文件的base64数据

来自分类Dev

Angular:从Base64图像获取尺寸

来自分类Dev

从 URI 中获取文件 base64

来自分类Dev

使用javascript获取base64图像

来自分类Dev

将位图转换为base64时获得黑色位图

来自分类Dev

将图片编码为base64,获取无效的base64字符串(使用base64EncodedStringWithOptions的iOS)

来自分类Dev

尝试对base64进行解码时出现“非法base64数据”

来自分类Dev

从图像进行Base64转换时未获取base64字符串

来自分类Dev

Safari中的Base64图片标签未显示

来自分类Dev

Summernote-图片网址,而不是Base64

来自分类Dev

上传图片字节,而不是base64表示形式

来自分类Dev

JSON图片网址,而不是base64

来自分类Dev

data:image / gif; base64图片没有动画

来自分类Dev

Python解码base64到图片不起作用

来自分类Dev

在客户端从base64制作图片

来自分类Dev

JSON图片网址,而不是base64

来自分类Dev

如何在Google图表中的svg中获取带有图片的png(base64)?

来自分类Dev

上载图片asp.net MVC时出现Base64错误

来自分类Dev

使用reader.readAsArrayBuffer(file)获取图像base64

来自分类Dev

Javascript-从base64图像获取扩展名

来自分类Dev

如何获取WCF请求的base64签名编码

来自分类Dev

Base64解码编码,获取不同的数据

来自分类Dev

Highcharts-React获取图表的Base64 PNG

来自分类Dev

如何获取当前的加密URL Base64

来自分类Dev

我正在尝试从base64获取图像的高度