PHP 从图像变量上传文件

PHP Upload file from Image variable

我的想法是上传一个带有水印的图像文件。此代码需要像这样工作。在投资组合网站上,所有者通过我制作的 CMS 上传了一张他想出售的图片。但是因为他想出售图片,所以他希望图片带有水印出现在网站上。

所以我的想法是,我让他上传原始图像,然后我通过$_Files() 获取图像,然后将其与水印结合起来,重新制作一张图像并上传该图像。

现在是我的问题,我如何上传在 PHP 中创建的图像我试图将其转换为 Base64 但没有成功。这是我的代码。

抱歉英语不好!

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);


$filetype =  substr($target_file, -4);

if($filetype == ".gif") $image = @imagecreatefromgif($_FILES["fileToUpload"]["tmp_name"]);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);
if($filetype == ".png") $image = @imagecreatefrompng($_FILES["fileToUpload"]["tmp_name"]);
if (empty($image)) die();
$watermark = @imagecreatefrompng('watermark.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

file_put_contents($target_file, $image)

试试这个: imagejpeg($image, $target_file);imagepng($image, $target_file);imagegif($image, $target_file);