1

My Idea is to upload a image file that is combined with a watermark. This code needs to work like this. On a portfolio website, the owner uploads an image he wants to sell throught the CMS i made. But because he wants to sell the image he wants it to come on the site with a watermark.

So my idea, i let him upload the original image, then i get the image through the $_Files() en combine it with a watermark, make it one image again and upload that image.

Now is my question, how do i upload a image created in PHP i tried to convert it to Base64 but with no luck. Here is my code.

Sorry for the bad english!

$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)
3
  • 2
    imagejpeg should do the trick -> php.net/manual/en/function.imagejpeg.php (instead of file_put_contents) Commented Jan 12, 2015 at 14:51
  • I think you use use move_uploaded_file first to "upload" the file to a temp folder, then manipulate it however you need, then save it to its final location. Commented Jan 12, 2015 at 14:52
  • @RocketHazmat I think you don't have to use move_uploaded_file, because the file is already in a temporary directory. but if the OP want's to save the original file he should use it to save the image on the server in addition to the watermarked one. Commented Jan 12, 2015 at 14:55

1 Answer 1

1

Try this: imagejpeg($image, $target_file); or imagepng($image, $target_file); or imagegif($image, $target_file);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.