PHP 水印不会保存到目标文件夹,但原始图像可以
PHP watermark does not save to the destination folder, but original image do
我正在尝试编写一个带有文件上传功能的简单 php 图库。每张照片我想要 3 个文件 - 原始文件、较小尺寸的文件和一个带水印的文件。
保存原图没问题,但上传后文件夹中没有带水印的图片。我有权在此文件夹中写入,即使我尝试使用 chmod
强制它,它也无法正常工作。
我还需要为服务器生成一个 200x125px 的图像,但无法生成,因为水印无法正常工作。
我怎样才能做到这一点?
<?php
require '../vendor/autoload.php';
if (isset($_POST['submit'])){
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$fileAuthor = $_POST['author'];
$fileTitle = $_POST['title'];
$conn = new MongoDB\Client('mongodb://localhost:27017');
$db = $conn->images;
$collection = $db->uimages;
$allowed = array('jpg', 'png', 'jpeg');
if (in_array($fileActualExt, $allowed)){
if ($fileError === 0){
if ($fileSize < 1024000){
$fileNameID = uniqid('', true).".".$fileActualExt;
$fileDestFolder = '../images/'.$fileNameID;
$image = $_FILES['file'];;
$fontSize = 4;
$text = $_POST['watermark'];
$xPosition = 10;
$yPosition = 10;
$newImg = imagecreatefromjpeg($image);
$fontColor = imagecolorallocate($newImg, 255, 0, 0);
imagestring($newImg, $fontSize, $xPosition, $yPosition, $text, $fontColor);
$save = "../images/". "water".$fileNameID .".png";
chmod($save,0755);
imagepng($newImg, $save, 0, NULL);
imagedestroy($newImg);
// move_uploaded_file($newImg, $fileDestFolder);
move_uploaded_file($fileTmpName, $fileDestFolder);
$imageu = [
'_id' => $fileNameID,
'author' => $_POST['author'],
'title' => $_POST['title'],
];
$collection->insertOne($imageu);
header("Location: ../wheels.php?uploadsuccess");
} else {
echo "The image size is too big";
}
} else {
echo "There was an error with the image";
}
} else {
echo "You can not use this file extension";
}
}
?>
您的行 $image = $_FILES['file'];;
(除了有两个 semi-colons - 删除其中一个!)将 $image
定义为 $_FILES['file']
给出的数组。当您在页面顶部访问它的属性时,您已经知道这是一个数组。
然后您将此数组传递到 imagecreatefromjpeg()
,它需要一个文件路径。您应该将其传递给 $fileTmpName
。
编辑: 添加了略微压缩的 100% 完整可验证代码(没有 MongoDB 内容和 header 重定向)完整的表单,以便您可以测试:
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$fileAuthor = $_POST['author'];
$fileTitle = $_POST['title'];
$allowed = array('jpg', 'png', 'jpeg');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1024000) {
$fileNameID = uniqid('', true) . "." . $fileActualExt;
$fileDestination = '../images/' . $fileNameID;
$fontSize = 4;
$text = $_POST['watermark'];
$xPosition = 10;
$yPosition = 10;
$newImg = imagecreatefromjpeg($fileTmpName);
$fontColor2 = imagecolorallocate($newImg, 255, 0, 0);
imagestring($newImg, $fontSize, $xPosition, $yPosition, $text, $fontColor2);
$save = "../images/" . "water" . $fileNameID . ".png";
imagepng($newImg, $save, 0, null);
imagedestroy($newImg);
move_uploaded_file($fileTmpName, $fileDestination);
} else {
echo "The image size is too big";
}
} else {
echo "There was an error with the image";
}
} else {
echo "You can not use this file extension";
}
}
?>
<form enctype="multipart/form-data" method="POST">
<label>Watermark</label><input type="text" name="watermark"><br>
<input type="file" name="file">
<input type="submit" name="submit">
</form>
该代码还有一些其他问题(例如,您应该按 MIME 类型而不是文件扩展名检查文件类型,并且上传的 jpeg 文件将以文件名 water{uuid} 结尾。jpg.png), 但你的问题至少应该得到回答。
我正在尝试编写一个带有文件上传功能的简单 php 图库。每张照片我想要 3 个文件 - 原始文件、较小尺寸的文件和一个带水印的文件。
保存原图没问题,但上传后文件夹中没有带水印的图片。我有权在此文件夹中写入,即使我尝试使用 chmod
强制它,它也无法正常工作。
我还需要为服务器生成一个 200x125px 的图像,但无法生成,因为水印无法正常工作。
我怎样才能做到这一点?
<?php
require '../vendor/autoload.php';
if (isset($_POST['submit'])){
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$fileAuthor = $_POST['author'];
$fileTitle = $_POST['title'];
$conn = new MongoDB\Client('mongodb://localhost:27017');
$db = $conn->images;
$collection = $db->uimages;
$allowed = array('jpg', 'png', 'jpeg');
if (in_array($fileActualExt, $allowed)){
if ($fileError === 0){
if ($fileSize < 1024000){
$fileNameID = uniqid('', true).".".$fileActualExt;
$fileDestFolder = '../images/'.$fileNameID;
$image = $_FILES['file'];;
$fontSize = 4;
$text = $_POST['watermark'];
$xPosition = 10;
$yPosition = 10;
$newImg = imagecreatefromjpeg($image);
$fontColor = imagecolorallocate($newImg, 255, 0, 0);
imagestring($newImg, $fontSize, $xPosition, $yPosition, $text, $fontColor);
$save = "../images/". "water".$fileNameID .".png";
chmod($save,0755);
imagepng($newImg, $save, 0, NULL);
imagedestroy($newImg);
// move_uploaded_file($newImg, $fileDestFolder);
move_uploaded_file($fileTmpName, $fileDestFolder);
$imageu = [
'_id' => $fileNameID,
'author' => $_POST['author'],
'title' => $_POST['title'],
];
$collection->insertOne($imageu);
header("Location: ../wheels.php?uploadsuccess");
} else {
echo "The image size is too big";
}
} else {
echo "There was an error with the image";
}
} else {
echo "You can not use this file extension";
}
}
?>
您的行 $image = $_FILES['file'];;
(除了有两个 semi-colons - 删除其中一个!)将 $image
定义为 $_FILES['file']
给出的数组。当您在页面顶部访问它的属性时,您已经知道这是一个数组。
然后您将此数组传递到 imagecreatefromjpeg()
,它需要一个文件路径。您应该将其传递给 $fileTmpName
。
编辑: 添加了略微压缩的 100% 完整可验证代码(没有 MongoDB 内容和 header 重定向)完整的表单,以便您可以测试:
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$fileAuthor = $_POST['author'];
$fileTitle = $_POST['title'];
$allowed = array('jpg', 'png', 'jpeg');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1024000) {
$fileNameID = uniqid('', true) . "." . $fileActualExt;
$fileDestination = '../images/' . $fileNameID;
$fontSize = 4;
$text = $_POST['watermark'];
$xPosition = 10;
$yPosition = 10;
$newImg = imagecreatefromjpeg($fileTmpName);
$fontColor2 = imagecolorallocate($newImg, 255, 0, 0);
imagestring($newImg, $fontSize, $xPosition, $yPosition, $text, $fontColor2);
$save = "../images/" . "water" . $fileNameID . ".png";
imagepng($newImg, $save, 0, null);
imagedestroy($newImg);
move_uploaded_file($fileTmpName, $fileDestination);
} else {
echo "The image size is too big";
}
} else {
echo "There was an error with the image";
}
} else {
echo "You can not use this file extension";
}
}
?>
<form enctype="multipart/form-data" method="POST">
<label>Watermark</label><input type="text" name="watermark"><br>
<input type="file" name="file">
<input type="submit" name="submit">
</form>
该代码还有一些其他问题(例如,您应该按 MIME 类型而不是文件扩展名检查文件类型,并且上传的 jpeg 文件将以文件名 water{uuid} 结尾。jpg.png), 但你的问题至少应该得到回答。