Join over 55,891 Subscribers Today! FREE UPDATES!
Get The Only Freelancer crash course you will ever need to read!
There is a popular saying that a picture is worth a thousand words. Images are a great way of catching our attention instantly. But sometimes its necessary to tag or link specific parts of images to provide information. Popular social media giants like Facebook and Google Plus use image tagging to identify the people and places in images.
In this tutorial I am going to show you how to create a simple image tagging system using jQuery and CSS. Lets get started.
In this step we are going to setup the initial layout with the image to be tagged. We need to use the jQuery UI library for dragging and dropping and resizing. Let’s take a look at our initial code.
<html>
<head>
<title>Image Tagging with jQuery and PHP</title>
<link href="jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
</head>
<body>
<div id='main_panel'>
<div style='margin: auto;
text-align: center;
width: 600px;'>
<div id='image_panel' >
<img src='tagging.jpg' id='imageMap' />
<div id='mapper' ><img src='save.png' onclick='openDialog()' /></div>
<div id="planetmap">
</div>
<div id='form_panel'>
<div class='row'>
<div class='label'>Title</div><div class='field'><input type='text' id='title' /></div>
</div>
<div class='row'>
<div class='label'></div>
<div class='field'>
<input type='button' value='Add Tag' onclick='addTag()' />
</div>
</div>
</div>
</div>
</div>
<div style="background: none repeat scroll 0 0 #C7C7C8;
border: 1px solid #AEAEAE;
clear: both;
margin: 20px auto;
padding: 20px 0;
text-align: center;
width: 600px;">
<input type="button" value="Show Tags" onclick="showTags()" />
<input type="button" value="Hide Tags" onclick="hideTags()" />
</div>
</div>
</html>
Now lets get an idea about how our layout is structured and important components for this tutorial.
<div id='image_panel' >
<img src='tagging.jpg' id='imageMap' />
<div id='mapper' ><img src='save.png' onclick='openDialog()' /></div>
<div id="planetmap">
</div>
<div id='form_panel'>
<div class='row'>
<div class='label'>Title</div><div class='field'><input type='text' id='title' /></div>
</div>
<div class='row'>
<div class='label'></div>
<div class='field'>
<input type='button' value='Add Tag' onclick='addTag()' />
</div>
</div>
</div>
</div>
Now we have a basic idea about our layout. Styles used in the demo are not included in the above code. So make sure to use the project files while you read the tutorial to get a better understanding.
We have to load the tag window when the user clicks somewhere on the image. If you haven’t checked it already, load the demo and click somewhere. Lets see how we can create the tag window using the following code. Look at the $(“#imageMap”).click in the project files and I’ll be explaining it step by step.

var image_left = $(this).offset().left; var click_left = e.pageX; var left_distance = click_left - image_left; var image_top = $(this).offset().top; var click_top = e.pageY; var top_distance = click_top - image_top;
var mapper_width = $('#mapper').width();
var imagemap_width = $('#imageMap').width();
var mapper_height = $('#mapper').height();
var imagemap_height = $('#imageMap').height();
Then we calculate the width and heights of mapper (window for tagging) and the actual image. Now we have all the required parameters to start loading the tag window. We can use the top_distance and left_distance to load the tag window. The problem is it should not go outside the image container, we have to do the following validations while loading tag window.

if((top_distance + mapper_height > imagemap_height) && (left_distance + mapper_width > imagemap_width)){
$('#mapper').css("left", (click_left - mapper_width - image_left ))
.css("top",(click_top - mapper_height - image_top ))
.css("width","100px")
.css("height","100px")
.show();
}

else if(left_distance + mapper_width > imagemap_width){
$('#mapper').css("left", (click_left - mapper_width - image_left ))
.css("top",top_distance)
.css("width","100px")
.css("height","100px")
.show();
}

else if(top_distance + mapper_height > imagemap_height){
$('#mapper').css("left", left_distance)
.css("top",(click_top - mapper_height - image_top ))
.css("width","100px")
.css("height","100px")
.show();
}

else{
$('#mapper').css("left",left_distance)
.css("top",top_distance)
.css("width","100px")
.css("height","100px")
.show();
}
Finally we want our tagging window to be moved across the image and resize. Following code will enable dragging and resizing on tagging window.
$("#mapper").resizable({ containment: "parent" });
$("#mapper").draggable({ containment: "parent" });
You can see a save icon inside the tagging window opened in the previous section. Move and resize the tagging window and position it where you want to create the tag. Then click the save button which will call the openDialog function as shown below.
var openDialog = function(){
$("#form_panel").fadeIn("slow");
};
Form used to save the tag details will be displayed as shown below.

You can type a title for the tag and click the add button. The tag will be saved and hidden immediately. The section below shows the code for add tag functionality.
var addTag = function(){
var position = $('#mapper').position();
var pos_x = position.left;
var pos_y = position.top;
var pos_width = $('#mapper').width();
var pos_height = $('#mapper').height();
$('#planetmap').append('<div class="tagged" style="width:'+pos_width+';height:'+
pos_height+';left:'+pos_x+';top:'+pos_y+';" ><div class="tagged_box" style="width:'+pos_width+';height:'+
pos_height+';display:none;" ></div><div class="tagged_title" style="top:'+(pos_height+5)+';display:none;" >'+
$("#title").val()+'</div></div>');
$("#mapper").hide();
$("#title").val('');
$("#form_panel").hide();
};
Create as many tags as you want using the same procedure. Now we have to show the hidden tags when the mouse is moved over the tag area. Consider the following code.
$(".tagged").live("mouseover",function(){
if($(this).find(".openDialog").length == 0){
$(this).find(".tagged_box").css("display","block");
$(this).css("border","5px solid #EEE");
$(this).find(".tagged_title").css("display","block");
}
});
$(".tagged").live("mouseout",function(){
if($(this).find(".openDialog").length == 0){
$(this).find(".tagged_box").css("display","none");
$(this).css("border","none");
$(this).find(".tagged_title").css("display","none");
}
});
Now we have completed the tag creation process. Sometimes we may want to edit current tags by repositioning them. Let’s move onto editing tags.
Once you mouseover a tag, it will be displayed. Then click on the tag and you will see a tag window open with save and delete buttons. Lets look at the code first.

$(".tagged").live("click",function(){
$(this).find(".tagged_box").html("<img src='del.png' class='openDialog' value='Delete' onclick='deleteTag(this)' />\n\
<img src='save.png' onclick='editTag(this);' value='Save' />");
var img_scope_top = $("#imageMap").offset().top + $("#imageMap").height() - $(this).find(".tagged_box").height();
var img_scope_left = $("#imageMap").offset().left + $("#imageMap").width() - $(this).find(".tagged_box").width();
$(this).draggable({ containment:[$("#imageMap").offset().left,$("#imageMap").offset().top,img_scope_left,img_scope_top] });
});
var editTag = function(obj){
$(obj).parent().parent().draggable( 'disable' );
$(obj).parent().parent().removeAttr( 'class' );
$(obj).parent().parent().addClass( 'tagged' );
$(obj).parent().parent().css("border","none");
$(obj).parent().css("display","none");
$(obj).parent().parent().find(".tagged_title").css("display","none");
$(obj).parent().html('');
}
var deleteTag = function(obj){
$(obj).parent().parent().remove();
};
Now we have completed the image tagging process. No tags are displayed in default mode. So you can use the following code if you want to show and hide all the tags specified in the image.
Show All Tags
var showTags = function(){
$(".tagged_box").css("display","block");
$(".tagged").css("border","5px solid #EEE");
$(".tagged_title").css("display","block");
};
Hide All Tags
var hideTags = function(){
$(".tagged_box").css("display","none");
$(".tagged").css("border","none");
$(".tagged_title").css("display","none");
};
In this tutorial I have explained the process and necessary code for creating a simple image tagging system. Now it’s your responsibility to add new features and use it in your projects. Hope you enjoyed it and feel free to add your suggestions and questions in the comments section.
Get The Only Freelancer crash course you will ever need to read!
Rakhitha Nimesh is a software engineer and writer from Sri Lanka. He likes to develop applications and write on latest technologies. He is available for freelance writing and WordPress development. You can read his latest book on Building Impressive Presentations with Impress.js. He is a regular contributor to 1stWebDesigner, Tuts+ network and Sitepoint network. Make sure to contact him at www.innovativephp.com or follow him on Twitter
Tuesday, September 4th, 2012 17:36
Excellent example!! It seems that the tag is saved on the client side. Is it possible to save the tagged image to the server side such as in file system or database?
Saturday, September 15th, 2012 06:13
Definitely you can save it on server side by getting the four corner points of the tagged section and using an AJAX request in tag creation.
Monday, August 13th, 2012 22:09
One comment, after many hours of trying to figure it out, I found that this line need “px” after all of the top, left, width height definitions so the tagged box would show up in the proper location.
$(‘#planetmap’).append(”+ $(“#title”).val()+”);
Great Tutorial, very easy to follow! Works well with the latest jquery library.
Wednesday, September 5th, 2012 04:12
the tagged position is not shoeing correctly.i tagged in different places but all the tags are showing on the left bottom corner of the image..any solution…please replay…thanx…
Saturday, September 15th, 2012 06:12
Can you provide me an image of your screen ? Also did you change any part of the code ?
Friday, July 13th, 2012 13:43
Interesting, but I couldn’t get the demo to work in either IE or Chrome…
Tuesday, July 10th, 2012 01:27
Good tutorial…thanks!
If not, then it's time to learn how to:
You can trust 1stWebDesigner to help you become a better web designer!
- Jacob Cass | Just Creative
Just enter your name and email below and click Get Updates!
Jaap
Tuesday, July 10th, 2012 01:27
Good tutorial…thanks!
Chris Sund
Monday, August 13th, 2012 22:09
One comment, after many hours of trying to figure it out, I found that this line need “px” after all of the top, left, width height definitions so the tagged box would show up in the proper location.
$(‘#planetmap’).append(”+ $(“#title”).val()+”);
Great Tutorial, very easy to follow! Works well with the latest jquery library.
nephy
Wednesday, September 5th, 2012 04:12
the tagged position is not shoeing correctly.i tagged in different places but all the tags are showing on the left bottom corner of the image..any solution…please replay…thanx…
Rakhitha Nimesh
Saturday, September 15th, 2012 06:12
Can you provide me an image of your screen ? Also did you change any part of the code ?
Dainis Graveris
Tuesday, August 14th, 2012 03:42
Hey Chris, thanks for appreciation and little fix! :)
Debbie Schmidt
Friday, July 13th, 2012 13:43
Interesting, but I couldn’t get the demo to work in either IE or Chrome…
Dainis Graveris
Wednesday, August 1st, 2012 07:37
Works fine to me on Chrome! You just need to add tags at first(click on map and add title and tag), it starts from zero, it should be mentioned in demo..I was confused first time too!
سعر الذهب في مصر
Sunday, August 26th, 2012 07:52
Thanks a lot for this tutorial.
K. Cheung
Tuesday, September 4th, 2012 17:36
Excellent example!! It seems that the tag is saved on the client side. Is it possible to save the tagged image to the server side such as in file system or database?
Rakhitha Nimesh
Saturday, September 15th, 2012 06:13
Definitely you can save it on server side by getting the four corner points of the tagged section and using an AJAX request in tag creation.