Also thumbnails are watermarked

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

Also thumbnails are watermarked

Post by Robbedoesie »

Hello,
I have a resize and watermark function what will resize and watermark the main images while uploading and not the thumbnails, well I thought that this would be the case but it isn't. The upload image function looks like this;
[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;

$source_image = $image_temp;
$destination = 'uploads/'.$album_id.'/'.$image_file;
$tn_w = 900;
$tn_h = 600;
$quality = 100;
$wmsource = 'watermark.png';
image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);

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

The destination is in the folder uploads and not in the folder uploads/thumbs, but still also the thumbnails are watermarked and that is not looking nice at all.
Can somebody tell how this can be possible?

Thanks
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Also thumbnails are watermarked

Post by Temor »

Well, your creating the image before you create the thumbnail. Switch these lines around:
[syntax=php]image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);

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

so that create_thumb comes before image_handler.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Also thumbnails are watermarked

Post by Robbedoesie »

I did put the create_thumb function before the image_handler function like this;
[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;



$source_image = $image_temp;
$destination = 'uploads/'.$album_id.'/'.$image_file;
$tn_w = 900;
$tn_h = 600;
$quality = 100;
$wmsource = 'watermark.png';
create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);


}[/syntax]
and this;
[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;

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

$source_image = $image_temp;
$destination = 'uploads/'.$album_id.'/'.$image_file;
$tn_w = 900;
$tn_h = 600;
$quality = 100;
$wmsource = 'watermark.png';
image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);


}[/syntax]
But this created broken images. The image_handler function is an upload image function. I think that like this it uploads the image without the thumbnail because it is placed before the image upload function.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Also thumbnails are watermarked

Post by Temor »

Well, that probably has something to do with how the create_thumbs and image_handler functions work. I can't find either on php.net so I guess it's custom. If you could post the code and or the docs for it that would help.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Also thumbnails are watermarked

Post by Robbedoesie »

Sorry for the late reaction, I had to work today.

The upload and resize/watermark part are in the image.func.php;
[syntax=php]<?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;

$source_image = $image_temp;
$destination = 'uploads/'.$album_id.'/'.$image_file;
$tn_w = 900;
$tn_h = 600;
$quality = 100;
$wmsource = 'watermark.png';
image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);

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



function image_handler($source_image,$destination,$tn_w,$tn_h,$quality = 80,$wmsource) {

#find out what type of image this is
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);

#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}

#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
$src_ratio = $src_w/$src_h;
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
if ($tn_w/$tn_h > $src_ratio) {
$tn_w = $tn_h * $src_ratio;
} else {
$tn_h = $tn_w / $src_ratio;
}

$newpic = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $tn_w, $tn_h, $src_w, $src_h);

#if we need to add a watermark
if($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);

#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
return false;
}

#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
imagealphablending($newpic, true);
$img_w = imagesx($newpic);
$img_h = imagesy($newpic);
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wm_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wm_h / 2); // For centering the watermark on any image

imagecopy($newpic, $watermark, $dst_x, $dst_y, 0, 0, $wm_w, $wm_h);
}
if(Imagejpeg($newpic,$destination,$quality)) {
return true;
}
return false;
}

?>[/syntax]

And the file where these functions are used in is;
[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');
$chunks = explode('.', $image_name);
$image_ext = end($chunks);
$image_ext = strtolower($image_ext);

$album_id = $_POST['album_id'];

$errors = array();

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

include 'template/header.php';

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 > 52428800) {
$errors[] = 'Je kan maximaal 5mb uploaden.';
}
if (album_check($album_id) === false) {
$errors[] = 'Er kan niet naar dit album worden geupload.';
}
}
}

$albums = get_albums();

if (empty($albums)) {
echo '<p>Je hebt nog geen albums. <a href="create_album.php">Maak een album.</a></p>';
} else {
?>
<h3>Upload foto</h3>
<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>
[/syntax]

I hope you can see what the problem is, I don't...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Also thumbnails are watermarked

Post by jacek »

It looks like your image_handler() function creates the watermarking ? If so you need to not use that on the thumbnail :P
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Also thumbnails are watermarked

Post by Robbedoesie »

Ah, the thumbnails are created from the main image and the main image are watermarked by using the image_handler function while uploading. I had to upload the image first, then make a thumbnail from it and then use the image_handler function. Maybe very logical for the most of you, for me again a learning
experience :)
Post Reply