Weblog of Neill Horsman

24 Jun, 2009

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

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

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

We will start with creating a table to store all the image information in

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

*photoid can be used later to implement into a photo gallery

1. First include the required .js

 <!-- Include jQuery and the image area select js -->
 <script src="js/jquery-1.3.2.min.js" type="text/javascript"><!--mce:2--></script>
<script src="js/jquery.imgareaselect-0.7.min.js" type="text/javascript"><!--mce:3--></script>
<strong>2.</strong> add an image to your document.
<pre lang="html"><img id="imageid" src="images/image.jpg" alt="Our image" />

*note we need to give it an ID which will correspond with out jQuery selector.

3. Now the jQuery

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

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

<form action="index.php" method="post"> <!-- Grab the X/Y/Width/Height -->
<input id="x1" name="x1" type="hidden" value="&lt;?php echo $x1; ?&gt;" />
<input id="y1" name="y1" type="hidden" value="&lt;?php echo $y1; ?&gt;" />
<input id="x2" name="x2" type="hidden" value="&lt;?php echo $x2; ?&gt;" />
<input id="y2" name="y2" type="hidden" value="&lt;?php echo $y2; ?&gt;" />
<input id="w" name="w" type="hidden" value="&lt;?php echo $w; ?&gt;" />
<input id="h" name="h" type="hidden" value="&lt;?php echo $h; ?&gt;" />
<input name="title" size="30" type="text" />
<input name="form" type="hidden" value="submit" />
<input class="send" type="submit" value="Submit" /> </form>
 
[/code]
 
<strong>5.</strong> 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-&gt;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']."') ");
}
}
?&gt;

6. Now we need to get the coordinates from the database to make up our imagemap using an ul.

if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
//database select
$query = $db-&gt;query(" SELECT id, title, x1, y1, x2, y2, width, height FROM phototags ");
while ($result = $query -&gt;fetch_object()) {
//take the title for the tag and replace all spaces with a - to name the css class
$name = str_replace(' ', '-', $result-&gt;title);
?&gt;
<!-- create a CSS class with the title name from our tag -->
 
<!-- 	#map a. { top:px; left:px; width:px; height:px; } --><!-- create a UL with the CSS class -->

<ul id="map">
 
	<li><a class="&lt;?php echo $name; ?&gt;" href="#"><span><strong>title; ?&gt;</strong></span></a></li>
 
</ul>
 
 
}
}
?&gt;

7. Now for some CSS to show the title for the tag

#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; }

View the demo 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 project.
Send me an email if you find a use for it, or a better way to do everything.

22 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

13 | akash

November 10th, 2009 at 9:00 pm

Avatar

Hey Neill,
first of all, thank you. I have been looking for something like that for quite some time now.

Why it is not working in IE 8 and at the bottom of there are all tags and when i click on that it is not working so can you give me the solution. So is there any solution for that like when we mouse over on that name the name shows on the photo like when we normally mouse over the photo and tags are displayed with white box.

14 | akash

November 13th, 2009 at 7:07 pm

Avatar

Hey neillh, this is good script. For me there are few questions:

1. why it is not working in ie-8
2. At the bottom side there are many tags but when we click on it gives a error page. so what is solution for that ? can we do like when we mouse hover the tag it(tag) shows on the photo like when we mouse hover the photo normally tag displayed with white box.

15 | Jens

December 23rd, 2009 at 10:20 am

Avatar

Hello Neill!

very nice script…thanx for sharing this!

I tried this script on my server, but i have a little problem.

Maybe you can help me?!

I upload the files and give them access to database, also i create the phototags-table.

The autocomplete-Data i get from my community-profiles table …this works fine, also i can create the “select-cropper”

If i click now on the submit-button, the page reloads an but nothing happens!!

No Errors but also no data in DB.

Hope you can help me, sorry for my english

THX in advance

Jens

16 | Jens

December 23rd, 2009 at 10:23 am

Avatar

…if i take a look in the source-code bevor clicking on submit the inputs are filled with the correct data

17 | Jenny

March 5th, 2010 at 6:43 pm

Avatar

Is there an update that works on IE??

18 | Neill Horsman

March 25th, 2010 at 7:55 pm

Avatar

Major update coming very shortly. sorry for the delay.

Now working IN IE and with error reporting and deleting tags.

stay tuned.

19 | Rajesh

April 23rd, 2010 at 4:42 pm

Avatar

Dude need help I am stuck with this code, its not working in IE :( Awesome code but I need it fixed for IE also Please can you have this done soon .

20 | Evan Foster

April 29th, 2010 at 3:49 am

Avatar

Internet Explorer 8 is very good because it is as stable as Opera. I hate the previous versions of IE like IE6 because it hangs frequently. “

21 | Swapnil Borse

May 11th, 2010 at 3:34 pm

Avatar

Hey Neill!!!

I guess we can also add up an edit tag functionality to it.

22 | Rob Sawyer

July 21st, 2011 at 3:48 pm

Avatar

I’m working on adding this to a cakephp project and I was wondering if you could provide the function.php file for download? I have the tagging system working, but I’m curious as to how you’re php file is setup.

Thanks for taking the time to document your process.

Comment Form

Categories

 

June 2009
M T W T F S S
« Jan   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  

Flickr PhotoStream

    AirWheelTagged BuildingDSC03433DSC03432DSC03404DSC03591DSC03584DSC03580

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.