Php 将水印添加到已发布的图像上

Php 将水印添加到已发布的图像上,php,watermark,Php,Watermark,所以在这个网站上,管理员发布了一个图像,在它通过sql发送到数据库之前,我想添加一个水印。这是我这部分的代码 if(!$_POST['name'] | !$_POST['cat'] | !$_POST['frame'] | !$_POST['molding'] | !$_POST['price'] | $_FILES["photo"]["tmp_name"]) { die('You did not fill in a required field.'); } $imag

所以在这个网站上,管理员发布了一个图像,在它通过sql发送到数据库之前,我想添加一个水印。这是我这部分的代码

  if(!$_POST['name'] | !$_POST['cat'] | !$_POST['frame'] | !$_POST['molding'] | !$_POST['price'] | $_FILES["photo"]["tmp_name"]) {

    die('You did not fill in a required field.');

    }

$image = file_get_contents($_FILES['photo']['tmp_name']);
$_POST['name'] = addslashes($_POST['name']);
$_POST['price'] = addslashes($_POST['price']);
$_POST['molding'] = addslashes($_POST['molding']);
$_POST['frame'] = addslashes($_POST['frame']);
$_POST['cat'] = addslashes($_POST['cat']);



// Load the stamp and the photo to apply the watermark to

$stamp = imagecreatefrompng('watermark2.PNG');

$save_watermark_photo_address = 'watermark_photo2.jpg';

// Set the margins for the stamp and get the height/width of the stamp image

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


$imgx = imagesx($image);
$imgy = imagesy($image);
$centerX=round($imgx/2) - ($sx/2);
$centerY=round($imgy/2) - ($sy/2);
// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 

imagecopy($image, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));


// Output and free memory
//header('Content-type: image/png');

imagejpeg($image, $save_watermark_photo_address, 80);
最后保存的部分是检查它

任何使用$image的操作都会出现错误 参数1应为资源,字符串在中给定

我认为这意味着图像格式是错误的,但我不知道如何修复它

在我添加水印代码之前,请大家注意,一切都很好。

更多说明, 你可能想用这个

老科尔德:

$image = file_get_contents($_FILES['photo']['tmp_name']);
新代码:

$imgData= file_get_contents($_FILES['photo']['tmp_name']);
$image = imagecreatefromstring( $imgData);

这是对我上述评论的澄清。

我认为问题出在这里$image=addslashesfile\u get\u contents$\u FILES['photo']['tmp\u name'];为什么要在图像文件内容中添加斜杠?这使它成为字符串值而不是图像。。。我想我添加它是没有真正原因的。我知道斜杠是为了什么,sql注入只是我不知道有什么极端的人可以用sql注入攻击我。它没有解决任何问题,仍然是同一个问题。好的,我认为你需要使用imagecreatefromstring,检查这个ohh,很抱歉听到,如果这篇文章修复了原始问题创建水印请标记为AnswerWell我现在有一个新问题。我已经添加了水印,但现在我需要它的原始格式,当我把文件内容作为一个BLOB放到我的数据库中时。你知道怎么做吗?我尝试过几种方法。使用$imgData插入db?!但是上面没有水印。我使用$image将水印放在上面。我不确定是否有其他方法,只需尝试将带水印的图像保存到一个文件中,然后使用file\u get\u content读取文件并插入数据库,然后删除该文件。