1

Hi there I'm using the following script, and it works perfectly.

My problem is, how do I replace the original image with the watermarked one leaving the same filename and extension?

$stamp = imagecreatefrompng(base_static_url().$this->marker_url);
$im = imagecreatefromjpeg($img_path);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

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

I tryed:

file_put_contents($img_path, imagecreatefromjpeg($im));

But got:

Error: failed to open stream: HTTP wrapper does not support writeable connections

And I also tried:

file_put_contents($img_path, $im);

And then I got a new error:

Error: file_put_contents(): supplied resource is not a valid stream resource
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
itsme
  • 48,972
  • 96
  • 224
  • 345

2 Answers2

2

You could try:

imagejpeg($im, $img_path); 

imagejpeg() takes a filename argument which is described as:

The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

However, as another user has mentioned - if you're trying to save a file to a remote server then you'll have to do it a different way. One method might be using PHP's FTP functions: http://www.php.net/manual/en/ftp.examples-basic.php

Joe
  • 15,205
  • 8
  • 49
  • 56
  • ok itryed imagejpeg() since the original one it's a jpg to be replaced. problem is http not allows unlinking and seems the path passed to imagejpeg() is not ok while it is ok for watermarking :/ i received : **imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/site/uploads/img/iphone.jpg' for writing: No such file or directory** – itsme Jun 18 '13 at 10:45
  • Are you sure that's the correct URL? The error message is saying that the path doesn't exist. Are you getting any other errors or warnings in your php error log? – Joe Jun 18 '13 at 10:47
  • yah the path is absolutely correct :( and i granted 777 access just to test :( – itsme Jun 18 '13 at 10:50
  • Ok, if you navigate to that url in your browser does the correct image show up? Is the watermarking working correctly? – Joe Jun 18 '13 at 10:59
  • replace imagepng() with imagejpeg() in your answer and i'm gonna accept it. Thanks so much you were right! – itsme Jun 18 '13 at 11:07
  • also unlink() it seems not necessary, cause using same filename will replace the image without having to delete it first ;)+ – itsme Jun 18 '13 at 11:13
  • @badbetonbreakbutbedbackbone - glad it worked for you :) I just updated my answer with your suggestions! – Joe Jun 18 '13 at 11:38
2

Well the error explains it all:

Error: failed to open stream: HTTP wrapper does not support writeable connections

The HTTP wrapper (a part of PHP that lets you use http:// protocols in URIs) cannot write to a web address. This makes sense, though, because imagine if someone could just run this this:

file_put_contents('http://google.com', '<!--malicious script-->');

And takeover google!

To save a file to a remote webserver you need to access its filesystem with FTP, SFTP, or the like. PHP has builtins for interfacing with FTP.

However, I suspect that the file, which you are trying to modify, is on the server from which this PHP script is executing. In that case, you need to use the path to the file on the server (it might be something like /var/www/images/image.jpg) and not the web address (http://www.yoursite.com/images/image.jpg) in file_put_contents().

Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
  • i tryed : **file_put_contents('./uploads/img/iphone.jpg',imagejpeg($im) ); ** it replaces the original one with same name and ext but the image now is empty :P – itsme Jun 18 '13 at 10:58
  • @badbetonbreakbutbedbackbone That's because `imagejpeg($im)` echos the image instead of returns it (with one argument, it reutrns a boolean to indicate success or failure). To write to a file, pass the filename as the second parameter: `imagejpeg($im, './uploads/img/iphone.jpg')` – Bailey Parker Jun 18 '13 at 11:00
  • +1 ok you saved my life, sorry but i have to accept the other dude's answer, since he told first to use imagepng() :P – itsme Jun 18 '13 at 11:07