PHP Tutorial: Watermarking an Image [problems]

Post here is you are having problems with any of the tutorials.
Post Reply
phpnewby
Posts: 2
Joined: Fri Feb 22, 2013 10:42 pm

PHP Tutorial: Watermarking an Image [problems]

Post by phpnewby »

Hello all,

I followed this tutorial: https://www.youtube.com/watch?v=o6PMaKmCnHg But the code doesn't work very well. I get next issue:

When I upload a image, it will be uploaded as a file and not as a .png. He doesn't upload the images in the 'images/' folder. He upload them in de root folder with the name: $image_name.

I have no idea what I do wrong. I followed your tutorial a few times but I can' t find the solution. Can anybody help me with this (little) error?

My code(S):

image.php
<?php
include('core/init.inc.php');
if (isset($_FILES['image'])){
	$image_name = 'images/' . md5(microtime(true)) . '.png';
	watermark_image($_FILES['image']['tmp_name'], '$image_name');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
	<div>
    <?php
	
	
	?>
    </div>
    <div>
    	<form action="" method="post" enctype="multipart/form-data" />
        
        <input type="file" name="image" />
        <input type="submit" value="Upload!" />

        </form>
	</div>            
</body>
</html>

img.inc.php
<?php

function watermark_image($image, $output){
	$info = @getimagesize($image);
	
	print_r($info);
	
	switch ($info['mime']){
		case 'image/jpeg':
			$main = imagecreatefromjpeg($image);
		break;
		
		case 'image/png':
			$main = imagecreatefrompng($image);
		break;
		
		case 'image/gif':
			$main = imagecreatefromgif($image);
		break;
	default: 
		return false;
	
	}
	
	imagealphablending($main, true);
	
	$overlay = imagecreatefrompng("{$GLOBALS['path']}/watermark.png");
	
	imagecopy($main, $overlay, 5, 5, 0, 0, imagesx($overlay), imagesy($overlay));
	
	imagepng($main, $output);
}
?>

init.inc.php
<?php
$path = dirname(__FILE__);
include("{$path}/inc/img.inc.php");
?>
Result:
Image

So, my map directory:
Image

I hope anybody can help me.



Sorry for my bad English..

Regards,
phpnewby
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP Tutorial: Watermarking an Image [problems]

Post by Helx »

In line 5 of image.php, loose the apostrophe's on $image_name (').
I don't think you need them.
phpnewby
Posts: 2
Joined: Fri Feb 22, 2013 10:42 pm

Re: PHP Tutorial: Watermarking an Image [problems]

Post by phpnewby »

Hello Helx,

You saved my day! This is working great!

Regards,
php newby
Post Reply