将水印添加到图像(Base64)并将其转换 [英] Add watermark to image (Base64) and convert it

查看:208
本文介绍了将水印添加到图像(Base64)并将其转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个PHP脚本(如下): 我使用POST获得base64字符串,而不是使用imagecreatefromstring函数,然后尝试添加水印并保存它.但是上传后服务器上的文件为空.我在做什么错了?

I have this PHP script (below): I getting base64 string using POST, than use imagecreatefromstring function and then trying to add watermark and save it. But the file is empty on the server after upload. What i'm doing wrong?

    <?php
    if($_SERVER['REQUEST_METHOD'] == "POST") {
    define('UPLOAD_DIR', 'img/');

    $base64 = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $_POST['string']));

    $stamp = imagecreatefrompng('img/wm.png');
    $im = imagecreatefromstring($base64);


    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);


    $file = UPLOAD_DIR . uniqid() . '.png';

    imagepng($im, $file, 100);


    echo 'ok';
    } else {
    echo "error";
    }


?>

有人可以帮我解决这个问题吗?

Can anyone help me to solve this issue?

推荐答案

请在之前添加此代码,以检查是否创建了图像

add this code before in order to check if your image was created

$im = imagecreatefromstring($base64);
if ($im !== false) {
    header('Content-Type: image/png');
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);


    $file = UPLOAD_DIR . uniqid() . '.png';

    imagepng($im, $file, 100);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

//

这篇关于将水印添加到图像(Base64)并将其转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆