0

So on this website an admin posts an image, before it is sent to the database via sql I want to add a watermark. This is my code for this part.

  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);

The save part at the end was to check it.

Anything that uses $image comes up with the error expects parameter 1 to be resource, string given in.

I think it means the images format is wrong but I have no idea how to fix it.

For anyone's information before I added the watermark code everything works perfectly.

  • i think the problem is here `$image = addslashes(file_get_contents ($_FILES['photo']['tmp_name']));` why do you add slashes to the image file content? that makes it string value instead of image... i think – Yazan Nov 08 '14 at 10:34
  • I added it for no real reason. I know what the slashes are for, sql injection its just I do not know to what extreme people can attack me using sql injection. It did not fix anything anyway, still same problem. – Matthew Hammond Nov 08 '14 at 10:37
  • ok, i think you need to use `imagecreatefromstring()`, check this http://stackoverflow.com/questions/5770795/php-temporary-file-upload-not-valid-image-resource – Yazan Nov 08 '14 at 11:08

1 Answers1

0

for more clarification, you may want to use this

old colde:

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

new code:

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

this is a clarification of my comment above.

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • ohh, sorry to hear that, if this post fixed the original issue (creating watermark) please mark as Answer – Yazan Nov 08 '14 at 11:28
  • Well I now have a new problem. I have added the watermark but now I need it in the original format when I did file_get_contents() to put it into my db as a BLOB. Any idea how to do that? I have tried several methods. – Matthew Hammond Nov 08 '14 at 11:28
  • use the $imgData to insert in db?! – Yazan Nov 08 '14 at 11:29
  • But that does not have the watermark on it. I use $image to put the watermark on it. – Matthew Hammond Nov 08 '14 at 11:30
  • i mm not sure if there is another way, just try to save the watermarked image to a file, then use `file_get_content()` to read the file and insert into db, then delete that file. – Yazan Nov 08 '14 at 11:45
  • or you can get the new image as base64Encoded, check this http://stackoverflow.com/questions/8502610/how-to-create-a-base64encoded-string-from-image-resource – Yazan Nov 08 '14 at 11:46
  • Yea that works great but when I get the image back from the database the image does not appear. – Matthew Hammond Nov 08 '14 at 11:57
  • Gives me the errors mysql_query(): MySQL server has gone away and mysql_query(): Error reading result set's header in – Matthew Hammond Nov 08 '14 at 12:02
  • you have to do some work yourself, this question originally was about watermark image, and it was answered, – Yazan Nov 09 '14 at 07:54