我有PHP脚本从canvas的base 64字符串创建PNG图像,我需要弄清楚如何使用它,这个新的PHP脚本不会创建调整大小的PNG文件。
参考:
使用PHP脚本但没有调整大小。
<?php
if (!file_exists('userCanvas/'))
{
mkdir('userCanvas', 0755, true);
}
else
{
$name = $_POST['name'];
$img = $_POST['CanvasPic'];
define('UPLOAD_DIR', 'userCanvas/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Could not save the file!';
}
?>
新脚本但不会创建调整大小的PNG图像:
<?php
if (!file_exists('userCanvas/'))
{
mkdir('userCanvas', 0755, true);
}
else
{
$name = $_POST['name'];
$img = $_POST['CanvasPic'];
define('UPLOAD_DIR', 'userCanvas/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$width = 350;
$height = 250;
header('Content-Type: image/png');
list($width_orig, $height_orig) = getimagesize($file);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig)
{
$width = $height*$ratio_orig;
}
else
{
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$success = file_put_contents($image_p, $data);
print $success ? $image_p : 'Could not save the file!';
}
?>
编辑:我发现错误(跳过行号和网址),我不知道如何解决这些错误。
我发现错误,(跳过的网址和代码行):
PHP Warning: getimagesize(userCanvas/53bc7c926a606.png): failed to open stream: No such file or directory
PHP Warning: Division by zero in
PHP Warning: imagecreatetruecolor(): Invalid image dimensions
PHP Warning: imagecreatefrompng(userCanvas/53bc7c926a606.png): failed to open stream: No such file or directory
PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given
PHP Warning: file_put_contents(): Filename cannot be empty