Skip to content

Commit cacebab

Browse files
committed
add components for cropping tags
1 parent 48fbf15 commit cacebab

26 files changed

+126
-2
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

admin.php

100644100755
File mode changed.

css/admin_style.css

100644100755
File mode changed.

css/fonts/mugshot.eot

100644100755
File mode changed.

css/fonts/mugshot.svg

100644100755
File mode changed.

css/fonts/mugshot.ttf

100644100755
File mode changed.

css/fonts/mugshot.woff

100644100755
File mode changed.

css/selection.json

100644100755
File mode changed.

css/style.css

100644100755
File mode changed.

include/capture.php

100644100755
+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
defined('MUGSHOT_PATH') or die('Hacking attempt!');
44

55
include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
6+
include_once(MUGSHOT_PATH . 'include/nn.training.thumbnails.php');
67

78
function add_mugshot_methods($arr) {
89
$service = &$arr[0];
@@ -41,6 +42,22 @@ function book_mugshots($data, &$service) {
4142
$rm = pwg_db_real_escape_string($value['removeThis']);
4243
$tagName = ($tag == -1 && $name != '') ? tag_id_from_tag_name($name) : $tag;
4344

45+
// Create or remove the training thumbnails, depending on webmaster settings.
46+
if ($plugin_config['autotag']) {
47+
$sql = "SELECT * FROM `". IMAGES_TABLE . "` WHERE `id`=".$imageId.";";
48+
49+
$imgData = pwg_db_fetch_assoc(pwg_query($sql));
50+
51+
// Remove or add cropped faces in the images to a directory.
52+
if($rm == 0 && $width >= 40 && $height >= 40 && extension_loaded('imagick') === true) {
53+
crop_image_faces($imgData['path'], $imgData['md5sum'], $name, $imgW, $imgH, $width, $height, $left, $top);
54+
}
55+
56+
if($rm == 1) {
57+
delete_image_faces($name, $imgData['md5sum']);
58+
}
59+
}
60+
4461
// Remove a mugshot
4562
if ($rm == 1) {
4663
$dString .= ($tag != '') ? $tag . ',' : '';

include/nn.training.thumbnails.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
defined('MUGSHOT_PATH') or die('Hacking attempt!');
4+
5+
// Check if the directory is empty
6+
function dir_is_empty($dir) {
7+
if (!file_exists($dir)) {
8+
return;
9+
}
10+
$handle = opendir($dir);
11+
while (false !== ($entry = readdir($handle))) {
12+
if ($entry != "." && $entry != "..") {
13+
return FALSE;
14+
}
15+
}
16+
return TRUE;
17+
}
18+
19+
// Automatically rotate the image before cropping
20+
function auto_rotate_image($image) {
21+
$o = $image->getImageOrientation();
22+
23+
switch ($o) {
24+
case 3:
25+
$image->rotateImage('#000', 180);
26+
break;
27+
28+
case 6:
29+
$image->rotateImage('#000', 90);
30+
break;
31+
32+
case 8:
33+
$image->rotateImage('#000', -90);
34+
break;
35+
}
36+
37+
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
38+
}
39+
40+
// Crop faces in the image
41+
function crop_image_faces($p, $imgFileName, $name, $iw, $ih, $width, $height, $left, $top) {
42+
try {
43+
$image = new Imagick($p);
44+
auto_rotate_image($image);
45+
$image -> resizeImage($iw, $ih, Imagick::FILTER_LANCZOS,1);
46+
$image -> cropImage($width, $height, $left, $top);
47+
$struc = str_replace(' ', '_', strtolower($name));
48+
$structure = getcwd()."/plugins/MugShot/training/".$struc;
49+
50+
if (!file_exists($structure)) {
51+
mkdir($structure, 0775, true);
52+
}
53+
54+
// If the file exists that means you have,
55+
// 1) tagged someone twice in the same photo,
56+
// 2) tagged someone in the same photo that is asociated with another album.
57+
// In Either case, we don't want duplicate photos that are almost exactly the same cluttering up our training data.
58+
if (!file_exists($structure.'/'.$imgFileName)) {
59+
$image -> writeImage($structure.'/'.$imgFileName.'.jpg');
60+
chmod($structure, 0760);
61+
}
62+
63+
} catch (Exception $e) {
64+
error_log($e->getMessage(), 0);
65+
}
66+
}
67+
68+
// Delete image face if the tag is deleted
69+
function delete_image_faces($name, $imgFileName) {
70+
try {
71+
$struc = str_replace(' ', '_', strtolower($name));
72+
$structure = getcwd()."/plugins/MugShot/training/".$struc;
73+
$remTest = false;
74+
$dirTest = false;
75+
76+
if (file_exists($structure.'/'.$imgFileName)) {
77+
$remTest = unlink($structure.'/'.$imgFileName.'.jpg');
78+
}
79+
80+
if (dir_is_empty($structure)) {
81+
$dirTest = rmdir($structure);
82+
}
83+
84+
} catch (Exception $e) {
85+
error_log($e->getMessage(), 0);
86+
}
87+
}
88+
89+
?>

index.php

100644100755
File mode changed.

js/admin_mug.js

100644100755
File mode changed.

js/mug.js

100644100755
File mode changed.

language/en_UK/index.php

100644100755
File mode changed.

language/en_UK/plugin.lang.php

100644100755
File mode changed.

language/en_US/index.php

100644100755
File mode changed.

language/en_US/plugin.lang.php

100644100755
File mode changed.

language/index.php

100644100755
File mode changed.

language/nb_NO/index.php

100644100755
File mode changed.

language/nb_NO/plugin.lang.php

100644100755
File mode changed.

main.inc.php

100644100755
File mode changed.

maintain.inc.php

100644100755
File mode changed.

template/admin.tpl

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
.labels {width: 200px;float: left;}
1313
.inputs {width: 200px;margin-left: 200px;}
1414
.ta {border-radius:5px 5px 5px 5px;}
15+
input[type="radio"] {
16+
margin-right: 10px;
17+
}
1518
{/literal}{/html_style}
1619

1720
{combine_script id='LocalStorageCache' load='footer' path='admin/themes/default/js/LocalStorageCache.js'}
@@ -45,7 +48,7 @@ jQuery(document).ready(function() {
4548
<p>
4649
{if count($groups) > 0}
4750
<strong>{'The following groups can use MugShot to tag people'|@translate}</strong>
48-
<br></br>
51+
<br><br>
4952
<select data-selectize="groups" data-value="{$groups_selected|@json_encode|escape:html}"
5053
placeholder="{'Type in a search term'|translate}"
5154
name="groups[]" multiple style="width:600px;"></select>
@@ -54,10 +57,25 @@ jQuery(document).ready(function() {
5457
{/if}
5558
</p>
5659

60+
<!-- Enable automatic cropping of tagged faces -->
61+
<p><br>
62+
<label for="autotag">
63+
<strong>{'Enable cropping of faces for automated tagging'|@translate}</strong><br>
64+
<i>This requires ImageMagick to be installed, including the PHP plugin</i><br><br>
65+
{if $autotag }
66+
<input type="radio" id="autotag1" name="autotag" value="1" checked /><b><i>ALLOW</i></b> MugShot to crop tagged faces from photos<br>
67+
<input type="radio" id="autotag2" name="autotag" value="0"/><b><i>DO NOT ALLOW</i></b> MugShot to crop tagged faces from photos<br>
68+
{else}
69+
<input type="radio" id="autotag1" name="autotag" value="1"/><b><i>ALLOW</i></b> MugShot to crop tagged faces from photos<br>
70+
<input type="radio" id="autotag2" name="autotag" value="0" checked /><b><i>DO NOT ALLOW</i></b> MugShot to crop tagged faces from photos<br>
71+
{/if}
72+
</label>
73+
</p><br>
74+
</div>
75+
5776
{if count($groups) > 0}
5877
<p class="formButtons">
5978
<input type="submit" value="{'Save'|@translate}" name="save" />
6079
</p>
6180
{/if}
62-
</div>
6381
</form>

0 commit comments

Comments
 (0)