使用64base解码字符串从PHP创建映像

时间:2022-11-13 13:32:50

I am able to create the image but the png it generates has a dark background. actually it should be transparent but i have no idea why its becoming black color in background. what am i doing wrong ?

我能够创建图像,但它生成的png具有深色背景。实际上它应该是透明的,但我不知道为什么它在背景中变成黑色。我究竟做错了什么 ?

$img = imagecreatefromstring(base64_decode($profilePicture));
    if ($img != false) {
        header('Content-Type: image/png');
        $rnd = sprintf('%04d', rand(0, 9999));
        $fileName = $rnd.'_'.time().'.png'; // random number + timestamp

        if (!imagepng($img, $upload_path.'images\\user\\profile\1\\'.$fileName))
            $this->sendResponse(false, 1133);

This is how it should be, http://www.image-share.com/upload/2510/41.png

这应该是这样的,http://www.image-share.com/upload/2510/41.png

But this is the image i am receiving... http://www.image-share.com/upload/2510/42.png

但这是我收到的图片...... http://www.image-share.com/upload/2510/42.png

3 个解决方案

#1


0  

Basically the image should have its background marked as transparent, nothing to do with the code

基本上图像应该将其背景标记为透明,与代码无关

#2


0  

This way of saving helped to get the original image quality.

这种节省方式有助于获得原始图像质量。

file_put_contents($upload_path.'images\\user\\profile\1\\'.$fileName, base64_decode($profilePicture));

#3


0  

in controller file

在控制器文件中

public function actionCreate()
{
    $model = new Datamykad();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {


        $folder = Yii::getAlias('@app').'\images';

        $filename = date("Y-m-d_H-i-s").'.png';
        $file_path = $folder.'\.'.$filename;
        $rawImage = $model->stringtoimage;

        $removeHeaders = explode(',', $rawImage);
        //need to decode before saving (data base64)
        $decode= base64_decode($removeHeaders[1]);
        file_put_contents($file_path, $decode);

    //}
        $model->img =$filename;
        $model->save();
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

view file.

at javascript;

function saveIt(){
    //var canvasData = new Image();
    var myDrawing = document.getElementById("canvas");
    var canvasData = myDrawing.toDataURL("image/png");
    //var postData = "canvasData="+canvasData;
    var postData = canvasData;

    imgLog(postData);
    }
function imgLog(data){
    document.getElementById("datamykad-stringtoimage").textContent += data;  
}

#1


0  

Basically the image should have its background marked as transparent, nothing to do with the code

基本上图像应该将其背景标记为透明,与代码无关

#2


0  

This way of saving helped to get the original image quality.

这种节省方式有助于获得原始图像质量。

file_put_contents($upload_path.'images\\user\\profile\1\\'.$fileName, base64_decode($profilePicture));

#3


0  

in controller file

在控制器文件中

public function actionCreate()
{
    $model = new Datamykad();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {


        $folder = Yii::getAlias('@app').'\images';

        $filename = date("Y-m-d_H-i-s").'.png';
        $file_path = $folder.'\.'.$filename;
        $rawImage = $model->stringtoimage;

        $removeHeaders = explode(',', $rawImage);
        //need to decode before saving (data base64)
        $decode= base64_decode($removeHeaders[1]);
        file_put_contents($file_path, $decode);

    //}
        $model->img =$filename;
        $model->save();
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

view file.

at javascript;

function saveIt(){
    //var canvasData = new Image();
    var myDrawing = document.getElementById("canvas");
    var canvasData = myDrawing.toDataURL("image/png");
    //var postData = "canvasData="+canvasData;
    var postData = canvasData;

    imgLog(postData);
    }
function imgLog(data){
    document.getElementById("datamykad-stringtoimage").textContent += data;  
}