Overview

Packages

  • Ansel
  • None

Classes

  • Ansel
  • Ansel_Ajax_Application
  • Ansel_Ajax_Imple_EditCaption
  • Ansel_Ajax_Imple_EditFaces
  • Ansel_Ajax_Imple_EditGalleryFaces
  • Ansel_Ajax_Imple_Embed
  • Ansel_Ajax_Imple_GallerySlugCheck
  • Ansel_Ajax_Imple_ImageSaveGeotag
  • Ansel_Ajax_Imple_LocationAutoCompleter
  • Ansel_Ajax_Imple_MapLayerSelect
  • Ansel_Ajax_Imple_TagActions
  • Ansel_Ajax_Imple_ToggleGalleryActions
  • Ansel_Ajax_Imple_ToggleOtherGalleries
  • Ansel_Ajax_Imple_UploadNotification
  • Ansel_Api
  • Ansel_Exception
  • Ansel_Faces
  • Ansel_Faces_Base
  • Ansel_Faces_Facedetect
  • Ansel_Faces_User
  • Ansel_Factory_Faces
  • Ansel_Factory_Storage
  • Ansel_Factory_Styles
  • Ansel_Form_Ecard
  • Ansel_Form_Image
  • Ansel_Form_ImageDate
  • Ansel_Form_Upload
  • Ansel_Gallery
  • Ansel_Gallery_Decorator_Date
  • Ansel_GalleryMode_Base
  • Ansel_GalleryMode_Date
  • Ansel_GalleryMode_Normal
  • Ansel_Image
  • Ansel_ImageGenerator
  • Ansel_ImageGenerator_Mini
  • Ansel_ImageGenerator_PolaroidThumb
  • Ansel_ImageGenerator_PolaroidThumbStack
  • Ansel_ImageGenerator_RoundedThumb
  • Ansel_ImageGenerator_RoundedThumbStack
  • Ansel_ImageGenerator_Screen
  • Ansel_ImageGenerator_ShadowThumb
  • Ansel_ImageGenerator_ShadowThumbStack
  • Ansel_ImageGenerator_SquareThumb
  • Ansel_ImageGenerator_Thumb
  • Ansel_LoginTasks_SystemTask_Upgrade
  • Ansel_Report
  • Ansel_Report_letter
  • Ansel_Report_mail
  • Ansel_Report_tickets
  • Ansel_Search
  • Ansel_Search_exif
  • Ansel_Search_Tag
  • Ansel_Storage
  • Ansel_Style
  • Ansel_Tagger
  • Ansel_Test
  • Ansel_Tile_DateGallery
  • Ansel_Tile_Gallery
  • Ansel_Tile_Image
  • Ansel_View_Ansel
  • Ansel_View_Base
  • Ansel_View_EmbeddedRenderer_GalleryLink
  • Ansel_View_EmbeddedRenderer_Mini
  • Ansel_View_EmbeddedRenderer_Slideshow
  • Ansel_View_Gallery
  • Ansel_View_GalleryProperties
  • Ansel_View_GalleryRenderer_Base
  • Ansel_View_GalleryRenderer_Gallery
  • Ansel_View_GalleryRenderer_GalleryLightbox
  • Ansel_View_Image
  • Ansel_View_List
  • Ansel_View_Results
  • Ansel_View_Slideshow
  • Ansel_View_Upload
  • Ansel_Widget
  • Ansel_Widget_Actions
  • Ansel_Widget_Base
  • Ansel_Widget_GalleryFaces
  • Ansel_Widget_Geotag
  • Ansel_Widget_ImageFaces
  • Ansel_Widget_Links
  • Ansel_Widget_OtherGalleries
  • Ansel_Widget_OwnerFaces
  • Ansel_Widget_SimilarPhotos
  • Ansel_Widget_Tags
  • Ansel_XPPublisher
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Face recognition class
  4:  *
  5:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  * @author  Duck <duck@obala.net>
 10:  * @package Ansel
 11:  */
 12: class Ansel_Faces
 13: {
 14:     /**
 15:      * Delete faces from VFS and DB storage.
 16:      *
 17:      * @TODO: Move SQL queries to Ansel_Storage::
 18:      *
 19:      * @param Ansel_Image $image Image object to delete faces for
 20:      * @param integer $face      Face id. If empty, all faces for $image are
 21:      *                           removed
 22:      *
 23:      * @throws Ansel_Exception
 24:      */
 25:     static public function delete(Ansel_Image $image, $face = null)
 26:     {
 27:         if ($image->facesCount == 0) {
 28:             return true;
 29:         }
 30: 
 31:         $path = self::getVFSPath($image->id) . '/faces';
 32:         $ext = self::getExtension();
 33: 
 34:         if ($face === null) {
 35:             $sql = 'SELECT face_id FROM ansel_faces WHERE image_id = ' . $image->id;
 36:             try {
 37:                 $faces = $GLOBALS['ansel_db']->selectValues($sql);
 38:             } catch (Horde_Db_Exception $e) {
 39:                 throw new Ansel_Exception($e);
 40:             }
 41:             try {
 42:                 foreach ($faces as $id) {
 43:                     $GLOBALS['injector']
 44:                         ->getInstance('Horde_Core_Factory_Vfs')
 45:                         ->create('images')
 46:                         ->deleteFile($path, $id . $ext);
 47:                 }
 48:             } catch (Horde_Vfs_Exception $e) {}
 49:             try {
 50:                 $GLOBALS['ansel_db']->delete('DELETE FROM ansel_faces WHERE '
 51:                     . 'image_id = ' . $image->id);
 52:                 $GLOBALS['ansel_db']->update('UPDATE ansel_images SET '
 53:                     . 'image_faces = 0 WHERE image_id = ' . $image->id
 54:                     . ' AND image_faces > 0 ');
 55:             } catch (Horde_Db_Exception $e) {
 56:                 throw new Ansel_Exception($e);
 57:             }
 58:             $gallery = $GLOBALS['injector']
 59:                 ->getInstance('Ansel_Storage')
 60:                 ->getGallery($image->gallery);
 61:             $gallery->set('faces', $gallery->get('faces') - count($faces), true);
 62:         } else {
 63:             try {
 64:                 $GLOBALS['injector']
 65:                     ->getInstance('Horde_Core_Factory_Vfs')
 66:                     ->create('images')
 67:                     ->deleteFile($path, (int)$face . $ext);
 68:             } catch (Horde_Vfs_Exception $e) {}
 69:             try {
 70:                 $GLOBALS['ansel_db']->delete('DELETE FROM ansel_faces WHERE'
 71:                     . ' face_id = ' . (int)$face);
 72:                 $GLOBALS['ansel_db']->update('UPDATE ansel_images SET '
 73:                     . 'image_faces = image_faces - 1 WHERE image_id = '
 74:                     . $image->id . ' AND image_faces > 0 ');
 75:             } catch (Horde_Db_Exception $e) {
 76:                 throw new Ansel_Exception($e);
 77:             }
 78:             $gallery = $GLOBALS['injector']
 79:                 ->getInstance('Ansel_Storage')
 80:                 ->getGallery($image->gallery);
 81:             $gallery->set('faces', $gallery->get('faces') - 1, true);
 82:         }
 83:     }
 84: 
 85:     /**
 86:      * Get image path
 87:      *
 88:      * @param integer $image Image ID to get
 89:      */
 90:     static public function getVFSPath($image)
 91:     {
 92:         return '.horde/ansel/' . substr(str_pad($image, 2, 0, STR_PAD_LEFT), -2) . '/';
 93:     }
 94: 
 95:     /**
 96:      * Get filename extension
 97:      *
 98:      */
 99:     static public function getExtension()
100:     {
101:         if ($GLOBALS['conf']['image']['type'] == 'jpeg') {
102:             return '.jpg';
103:         } else {
104:             return '.png';
105:         }
106:     }
107: 
108:     /**
109:      * Get face link. Points to the image that this face is from.
110:      *
111:      * @param array $face  Face data
112:      *
113:      * @return string  The url for the image this face belongs to.
114:      */
115:     static public function getLink(array $face)
116:     {
117:         return Ansel::getUrlFor(
118:             'view',
119:             array('view' => 'Image',
120:                   'gallery' => $face['gallery_id'],
121:                   'image' => $face['image_id']));
122:     }
123: 
124:     /**
125:      * Generate HTML for a face's tile
126:      *
127:      * @param integer $face  The face id.
128:      *
129:      * @return string  The generated HTML
130:      */
131:     static public function getFaceTile($face)
132:     {
133:         $faces = $GLOBALS['injector']->getInstance('Ansel_Faces');
134:         if (!is_array($face)) {
135:             $face = $faces->getFaceById($face, true);
136:         }
137:         $face_id = $face['face_id'];
138: 
139:         // The HTML to display the face image.
140:         $imghtml = sprintf("<img src=\"%s\" class=\"bordered-facethumb\" id=\"%s\" alt=\"%s\" />",
141:             $faces->getFaceUrl($face['image_id'], $face_id),
142:             'facethumb' . $face_id,
143:             htmlspecialchars($face['face_name']));
144: 
145:         $img_view_url = Ansel::getUrlFor('view',
146:             array('gallery' => $face['gallery_id'],
147:                   'view' => 'Image',
148:                   'image'=> $face['image_id'],
149:                   'havesearch' => false));
150: 
151:         // Build the actual html
152:         $html = '<div id="face' . $face_id . '"><table><tr><td>' . $img_view_url->link() . $imghtml . '</a></td><td>';
153:         if (!empty($face['face_name'])) {
154:             $html .= Horde::url('faces/face.php')->add('face', $face['face_id'])->link() . $face['face_name'] . '</a><br />';
155:         }
156: 
157:         // Display the face name or a link to claim the face.
158:         if (empty($face['face_name']) && $GLOBALS['conf']['report_content']['driver']) {
159:             $html .= Horde::url('faces/claim.php')->add('face', $face_id)->link(array('title' => _("Do you know someone in this photo?"))) . _("Claim") . '</a>';
160:         }
161: 
162:         // Link for searching for similar faces.
163:         if (Horde_Util::loadExtension('libpuzzle') !== false) {
164:             $html .= Horde::url('faces/search/image_search.php')->add('face_id', $face_id)->link() . _("Find similar") . '</a>';
165:         }
166:         $html .= '</div></td></tr></table>';
167: 
168:         return $html;
169:     }
170: 
171: }
172: 
API documentation generated by ApiGen