我有这个PHP脚本(如下): 我使用POST获取base64字符串,而不是使用imagecreatefromstring函数,然后尝试添加水印并保存它。但上传后该文件在服务器上为空。我做错了什么?
<?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";
}
?>
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
之前添加此代码,以检查您的图片是否已创建
$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.';
}
//