watermark images

Ask about a PHP problem here.
Post Reply
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

watermark images

Post by Robbedoesie »

Hello,
I did the watermark images tutorial and it is working good. I implement the watermark function in an upload image tutorial, chanced a little bit, checked if it is still working and no, it isn't working any more and I can't get it let working again.

Can anybody tell me why it is not working?

This is the upload image page;
[syntax=php]<?php
include 'init.php';

if (!logged_in()) {
header('Location: administratie.php');
exit();
}
include 'template/header.php';
?>
<h3>Upload foto</h3>
<?php
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image'] ['name'];
$image_size = $_FILES['image'] ['size'];
$image_temp = $_FILES['image'] ['tmp_name'];

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.',$image_name)));

$album_id = $_POST['album_id'];

$errors = array();

if (empty($image_name) || empty($album_id)) {
$errors[] = 'Niet alles is ingevuld';
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = 'Bestandsnaam is niet toegestaan.';
}
if ($image_size > 2097152) {
$errors[] = 'Je kan maximaal 2mb uploaden.';
}
if (album_check($album_id) === false) {
$errors[] = 'Er kan niet naar dit album worden geupload.';
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error, '<br />';
}
} else {
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
//This is the watermark_image part
$image_output = 'uploads/' . md5(microtime(true)).'.png';
watermark_image($image_temp, $image_output);
}

$albums = get_albums();

if (empty($albums)) {
echo '<p>Je hebt nog geen albums. <a href="create_album.php">Maak een album.</a></p>';
} else {
?>
<form action="" method="post" enctype="multipart/form-data">
<p>Kies een foto:<br /><input type="file" name="image" /></p>
<p>
Kies een album:<br />
<select name="album_id">
<?php
foreach ($albums as $album) {
echo '<option value="', $album['id'], '">', $album['name'], '</option>';
}
?>
</select>
</p>
<p><input type="submit" value="Upload" /></p>
</form>
<?php
}

include 'template/footer.php';
?>[/syntax]

The function is;
[syntax=php]<?php
function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;

mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);

create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
}

function get_images($album_id) {
$album_id = (int)$album_id;

$images = array();

$image_query = mysql_query("SELECT `image_id`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
while ($images_row = mysql_fetch_assoc($image_query)) {
$images[] = array(
'id' => $images_row['image_id'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}

function image_check($image_id) {
$image_id = (int)$image_id;
$query = mysql_query("SELECT COUNT(`image_id`) FROM `images` WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
return (mysql_result($query, 0) == 0) ? false : true;
}
//The watermark_image part
function watermark_image($image, $output) {
$info = getimagesize($image);

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('watermark.png');

imagecopy($main, $overlay, 5, 5, 0, 0, imagesx($overlay), imagesy($overlay));
imagepng($main, $output);
}

function delete_image($image_id) {
$image_id = (int)$image_id;

$image_query = mysql_query("SELECT `album_id`, `ext` FROM `images` WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
$image_result = mysql_fetch_assoc($image_query);

$album_id = $image_result['album_id'];
$image_ext = $image_result['ext'];

unlink('uploads/'.$album_id.'/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/'.$album_id.'/'.$image_id.'.'.$image_ext);

mysql_query("DELETE FROM `images` WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
}
?>[/syntax]

Thanks in advance.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

I had put the watermark part on the wrong spot. It is working now. What I am now trying to accomplish is that the watermarked image is going into the right image album. The albums are made with php and al the albums got there own album id, but I don't know how to get the watermarked image in an album.
My image upload file is;
[syntax=php]<?php
include 'init.php';

if (!logged_in()) {
header('Location: administratie.php');
exit();
}
include 'template/header.php';
?>
<h3>Upload foto</h3>
<?php
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image'] ['name'];
$image_size = $_FILES['image'] ['size'];
$image_temp = $_FILES['image'] ['tmp_name'];

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.',$image_name)));

$album_id = $_POST['album_id'];

$image_output = 'uploads/' .$album_id.'/'.$image_ext.'.png';
watermark_image($image_temp, $image_output);

$errors = array();

if (empty($image_name) || empty($album_id)) {
$errors[] = 'Niet alles is ingevuld';
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = 'Bestandsnaam is niet toegestaan.';
}
if ($image_size > 2097152) {
$errors[] = 'Je kan maximaal 2mb uploaden.';
}
if (album_check($album_id) === false) {
$errors[] = 'Er kan niet naar dit album worden geupload.';
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error, '<br />';
}
} else {
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
}[/syntax]
I thought that I only needed the $image_id and $image_ext but or I use it wrong or I just don't know how to reach the image albums.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: watermark images

Post by Helx »

Were the un-watermarked images being put into the album?
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

For me it is not that simple because the image upload function is a bit more complicated then a usual image upload function.
image_upload.php;
[syntax=php]function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;

mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);
}
[/syntax]

And I think that I need the image_id but I don't know how.

My watermark_image now looks like this;
[syntax=php]if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image'] ['name'];
$image_size = $_FILES['image'] ['size'];
$image_temp = $_FILES['image'] ['tmp_name'];

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.',$image_name)));

$album_id = $_POST['album_id'];
$image_id = (int)$image_id;
$image_file = $image_id.'.'.$image_ext;

$image_output = 'uploads/' .$album_id.'/'.$image_file.'.png';
watermark_image($image_temp, $image_output, $album_id);[/syntax]

And that is not good enough, no watermarked images. How can I do the same as the image_upload?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: watermark images

Post by Helx »

So its SQL your having trouble with?

If so, I have already made a topic here: viewtopic.php?f=13&t=1907

I'm having the same problem.

#100th post :D
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

With the image_upload it stores data in the database, with the watermark_image it doesn't, that makes it different and for me more difficult(and maybe to difficult) to get the right php code and bring the watermarked images into the right album. At the moment there is no watermarked images to be found anywhere. It's not that I having trouble with mysql at the moment but I think that I have more trouble with how to use the right variables to get the right path for the watermarked images to the right album.

By the way, did you solved your sql query problem?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: watermark images

Post by Helx »

If your uploading to a directory, I think you may have to CHMOD. That's where I always mess up.

And no, still having issues with SQL :/
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

Well, I get the watermarked images in the albums but the files doesn't replace the target image and the file names looks different. Instead of for example 23.jpg it is 0.jpg.png, and a newer uploaded watermarked file got the same file name, so it replace an older watermarked image. Could mysql bring me the solution here?

With your mysql query problem, doesn't the mysql_error() tell you something? In your case maybe [syntax=php]mysql_query("INSERT INTO reg_id (for, registration_number, registration_password, CRT, registrar) VALUES ('$for', '$reg_id', '$reg_pass', '$crt', '$usr')", $db1or die(mysql_error());[/syntax] can tell you what is wrong.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: watermark images

Post by jacek »

You probably want to insert he $image_id rther than the image_ext don't you ?
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

I only inserted the $image_id instead of the $image_ext. The name of the png file changed from 0.jpg.png to 0.png but nothing else changed. If I use the $image_name instead of the $image_id the watermarked image is also uploaded. So if I upload two images there will be four images in an album, one image with the image id, for example 43.jpg, and the watermarked image with the complete filename, like imagename.JPG.png.

I think that using the image_id is the better way but the result is not good yet. If I upload two images there will be three images in an album, one watermarked image and two not watermarked images.

How to get at least the same result as with the $image_name and if it is possible that the watermarked image overwrites the original image? Do I then have to loop trough the images?

A part of the image_upload.php file is now;
[syntax=php]if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image'] ['name'];
$image_size = $_FILES['image'] ['size'];
$image_temp = $_FILES['image'] ['tmp_name'];

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.',$image_name)));


$album_id = $_POST['album_id'];

$image_output = 'uploads/' .$album_id.'/'.$image_name.'.png';
watermark_image($image_temp, $image_output);[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: watermark images

Post by jacek »

To fix that you just need to use the correct name in watermark_image($image_temp, $image_output); it will save the image with the name you give it.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

Jacek, are the parameters not correct?

I unlinked the $image_temp, so it is looking like this;
[syntax=php] $image_output = 'uploads/' .$album_id.'/'.$image_name.'.jpg';
watermark_image($image_temp, $image_output);
unlink($image_temp);[/syntax]
The watermark image appears in the folder but not in the thumbs folder. I tried to insert the image for in the thumbs folder like for example this;
[syntax=php]function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;
$image_name = $_FILES['image'] ['name'];
$image_temp = $_FILES['image'] ['tmp_name'];


mysql_query("INSERT INTO `images` VALUES ('', '$image_name', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);

create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/.$image_temp');
}[/syntax]
But I can't get it working.
Maybe I make it more difficult for me then it should be. To just use the correct name in the watermark_image sounds more easy than what I am doing now. I make now all kinds of combinations but nothing works.
Where to begin to make this work?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: watermark images

Post by jacek »

You probably don't want to watermark the thumbnail too, normally it's only the full size image that has it. The gallery page starts to look pretty ugly once you have lots of watermarked images.

One thing I noticed

[syntax=php]create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/.$image_temp');[/syntax]
variables don't translate to their value in ' so this should be

[syntax=php]create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/'.$image_temp);[/syntax]
There is also no need to create a temporary file you can create the thumbnail directly from $_FILES['something']['tmp_name']

If that does not get it working, can you post the full code of all files and I will take a closer look :)
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

jacek wrote:You probably don't want to watermark the thumbnail too, normally it's only the full size image that has it. The gallery page starts to look pretty ugly once you have lots of watermarked images.

One thing I noticed

[syntax=php]create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/.$image_temp');[/syntax]
variables don't translate to their value in ' so this should be

[syntax=php]create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/'.$image_temp);[/syntax]
There is also no need to create a temporary file you can create the thumbnail directly from $_FILES['something']['tmp_name']


That was one of my attempts to get the watermark_image right without success. I now know that I better not use the $image_temp for creating thumbnails.

I can't get this working. Several attempts further(I lost count) the watermark_image function is two parameters richer, so it looks now like this;
[syntax=php]function watermark_image($image, $image_ext, $image_id, $output) {
$info = getimagesize($image);

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('watermark.png');

imagecopy($main, $overlay, 5, 5, 0, 0, imagesx($overlay), imagesy($overlay));
imagepng($main, $output);
} [/syntax]
and the image_uploadis now;
[syntax=php]<?php
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image'] ['name'];
$image_size = $_FILES['image'] ['size'];
$image_temp = $_FILES['image'] ['tmp_name'];

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.',$image_name)));


$album_id = $_POST['album_id'];
$image_file = $image_id.'.'.$image_ext;

$image_output = 'uploads/' .$album_id.'/'.$image_file.'.jpg';
watermark_image($image_temp, $image_ext, $image_id, $image_output);
[/syntax]
I try to get the same result as the upload_image function;
[syntax=php]function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;
$image_name = $_FILES['image'] ['name'];

mysql_query("INSERT INTO `images` VALUES ('', '$image_name', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);

create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
}
[/syntax]
but without success as you probably already guessed.

I send you all the files. It is from a tutorial from phpacademy.
I hope you can help me with this.
Thanks.
Attachments
phpacademy.7z
(122.08 KiB) Downloaded 663 times
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: watermark images

Post by jacek »

Well you are not using the middle two parameters of the watermark function so you can remove those.

Everything looks right to me, the only thing you need to do to get the watermarking working is replace the move_uploaded_file line in upload_image(), so that function would become

[syntax=php]function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;
$image_name = $_FILES['image'] ['name'];

mysql_query("INSERT INTO `images` VALUES ('', '$image_name', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;

watermark_image($image_temp, 'uploads/'.$album_id.'/'.$image_file);

create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
}[/syntax]

I was hoping to be able to test it out myself but since this is part of such a large tutorial that is not really possible :(
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: watermark images

Post by Robbedoesie »

That is what I would say a clever, inventive solution. I would never came up whit that possibility.
Thanks :D
Post Reply