Weblog of Neill Horsman

24 Jun, 2009

Photo Tagging Project with jQuery/PHP/MySQL/CSS and an unordered list

Posted by: Neill Horsman In: Technology| business| design

A few months back I worked on a project that required photo tagging. The project got scraped but I am still developing most of the functionality for future work.

The requirement for this particular project was for a photo gallery grouped into albums, with photos consisting mainly of people out on the town. I wont go into full details but here is how you tag a static image.

View the demo here
Grab the source code here

We will start with creating a table to store all the image information in
[code lang="sql"]
CREATE TABLE phototags (
id int(255) NOT NULL AUTO_INCREMENT,
photoid int(11) NOT NULL,
title varchar(255) NOT NULL,
x1 int(11) DEFAULT NULL,
y1 int(11) DEFAULT NULL,
x2 int(11) DEFAULT NULL,
y2 int(11) DEFAULT NULL,
width int(11) DEFAULT NULL,
height int(11) DEFAULT NULL,
PRIMARY KEY (id)
);
[/code]
*photoid can be used later to implement into a photo gallery

1. First include the required .js
[code lang="html4strict"]



[/code]

2. add an image to your document.
[code lang="html4strict"]
Our image
[/code]
*note we need to give it an ID which will correspond with out jQuery selector.

3. Now the jQuery
[code lang="javascript"]
//Create some vars to hold the x/y info when selecting areas of the image
var $x1, $y1, $x2, $y2, $w, $h;
function selectChange(img, selection) {
$x1.text(selection.x1);
$y1.text(selection.y1);
$x2.text(selection.x2);
$y2.text(selection.y2);
$w.text(selection.width);
$h.text(selection.height);
//load the x/y/width/height into hidden text boxes
$('input#x1').val(selection.x1);
$('input#y1').val(selection.y1);
$('input#x2').val(selection.x2);
$('input#y2').val(selection.y2);
$('input#w').val(selection.width);
$('input#h').val(selection.height);
}

$(document).ready(function () {
$x1 = $('#x1');
$y1 = $('#y1');
$x2 = $('#x2');
$y2 = $('#y2');
$w = $('#w');
$h = $('#h');
});

//here is where we tell jQuery what image the area select will appear on.
$(window).load(function () {
$('img#imageid').imgAreaSelect({ selectionOpacity: 0, onSelectChange: selectChange,
outerOpacity: 0.4, handles: true });
$('.imgareaselect-handle').css('opacity', 0.7);
});
[/code]

In the above code, we inclued jQuery files, made an image with an ID, told jQuery to use that image for the image area select, and set up some other bits to later store this information in a database.

4. Now we need to store the x/y/width/height into some hidden text boxes, lets create a form
[code lang="php"]

[/code]

5. Then we can connect to out mysql database and insert all this information
[code lang="php"]
// mysql('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'user' ,'pass', 'db_name');
// If user has submitted a tag, insert into database
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
if(isset($_REQUEST['form'])) {
$query = $db->query(" INSERT INTO phototags (id, title, x1, y1, x2, y2, width, height) " .
" VALUES('', '".$_POST['title']."', '".$_POST['x1']."', '".$_POST['y1']."', '".$_POST['x2']."', '".$_POST['y2']."', '".$_POST['w']."', '".$_POST['h']."') ");
}
}
?>
[/code]

6. Now we need to get the coordinates from the database to make up our imagemap using an ul.
[code lang="php"]
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
//database select
$query = $db->query(" SELECT id, title, x1, y1, x2, y2, width, height FROM phototags ");
while ($result = $query ->fetch_object()) {
//take the title for the tag and replace all spaces with a - to name the css class
$name = str_replace(' ', '-', $result->title);
?>

}
}
?>
[/code]

7. Now for some CSS to show the title for the tag
[code lang="css"]
#map { margin:0; padding:0; width:400px; font-family:arial, helvetica, sans-serif; font-size:8pt; }
#map li { margin:0; padding:0; list-style:none; }
#map li a { position:absolute; display:block; background:url(images/blank.gif); text-decoration:none; }
#map li a span { display:none; text-decoration:none; }
#map li a:hover span { position:relative; display:block; left:10px; top:120px; padding:5px; border:1px solid #000; background:#fff; text-decoration:none; color:#000; filter:alpha(opacity=80); opacity:0.8; }
[/code]

View the demo here
Grab the source code here

This is my first tutorial so if its a bit hard to follow I am sorry, have a look at the source, its rather straight forward.

Feel free to use this for any personal/commercial project, modify or redistribute.
Send me an email if you find a use for it, or a better way to do everything.

12 Responses to "Photo Tagging Project with jQuery/PHP/MySQL/CSS and an unordered list"

1 | Devin

June 26th, 2009 at 11:21 am

Avatar

Hey,

The script looks very good and professional, but I was wondering if it was possible to change some tagging settings. For example, I want two people to tag the image with the same name (it would be really easy to make sure they spell it the same with the name suggest feature you have) so that there is a bigger chance of the tag being correct. Also, I have not really looked at the code too heavily but is it possible to delete a single tag.

And I assume that by clicking the tagged name under the photo will take you to a page with all the photos with this person tagged in it, am I correct?

2 | mSafdel

July 5th, 2009 at 4:48 pm

Avatar

Hey
Why it’s not working in IE8?

3 | Photo tagging project update | Weblog of Neill Horsman

July 9th, 2009 at 12:20 am

Avatar

[...] you are familiar with the photo tagging project I recently released you will know the Image Selection plug-in didn’t work in IE web browsers. [...]

4 | Neill Horsman

July 9th, 2009 at 12:32 am

Avatar

- mSafdel
Am working on this, should be good in IE by weeks end.

5 | Peter

July 19th, 2009 at 6:47 pm

Avatar

Hey Neill,

first of all, thank you. I have been looking for something like that for quite some time now. Actually it’s so easy when you see the done work :) . It works in safari, google chrome, firefox just unluckily not in IE. Why do microsoft always have to be different? :)

6 | james

July 27th, 2009 at 11:45 am

Avatar

can i edit and delete tags similar
http://phototagging.qiztech.com/editor.php ?

beacuse more adjust need not finish at first time . thanks

7 | Tony

July 31st, 2009 at 6:32 pm

Avatar

To James,

http://phototagging.qiztech.com/editor.php looks a little bit buggy

If I have to pay for software I would purchase http://taghim.com/
This http://phototagging.qiztech.com/ looks like Chinese tacky thing *)

take a look on this dumps:
http://badwebmasters.com/images/1.jpg
http://badwebmasters.com/images/2.jpg
you can tag outside the picture :) this is a really big matter ))

8 | Audry

August 3rd, 2009 at 1:25 am

Avatar

I agree with last post, thanks for screenshots Tony (( qiztech.com not professional company, I asked couple of question regarding integration, supprt etc.. from tech support more than one week ago and they still did not reply anything I think they did not speak English :( , what the f*ck!
I was thinking to buy this script but now i see it was not a good idea to invest money to something which doesn’t have any support, any users feedback.

9 | Audry

August 4th, 2009 at 5:39 am

Avatar

Your script is good but my site conflict with jquery,
if it could help anyone I stop up on taghim script.

10 | Parte

August 18th, 2009 at 5:48 am

Avatar

I bought http://phototagging.qiztech.com/ one mounth ago and faced so many dificulties the problem was with tagging out photo and tag sometimes did not saved correctly and alwayse fucked up interface, when I contact with company I purchase it many times but I newer get any reply back, just want to wam you.

I try your script but I cant make it work with joomla

anyone can sujest me good tool with good support
I really do not want to get in to the same trap again

11 | Sergey

August 18th, 2009 at 5:20 pm

Avatar

we use taghim.com over than 2 years in our party people community we purchase standard version and this guys wrote an extension for reading users from our database.
Parte if you looking for support contact this guys.

12 | Alex

October 20th, 2009 at 9:02 pm

Avatar

I recommend TagHim
The most professional guys all over the market

Comment Form


  • Alex: I recommend TagHim The most professional guys all over the market
  • Sergey: we use taghim.com over than 2 years in our party people community we purchase standard version and this guys wrote an extension for reading users from
  • Parte: I bought http://phototagging.qiztech.com/ one mounth ago and faced so many dificulties the problem was with tagging out photo and tag sometimes did n

Flickr PhotoStream

    Air_DSC4644WheelTagged BuildingDSC03433DSC03432DSC03420DSC03404DSC03422

About

Born in Ellisras, South Africa, I have since moved to Sydney, Australia. Stopping along the way for a few years in England and New Zealand. For the past five years I have been designing and developing web sites, web applications, marketing solutions and corporate branding / identity

www.neillh.com.au Freelance Design




Stream

There are no events to show at this time.

Powered by Lifestream from iBegin.