Example #1
1
function processImage($uploadDir, $filename)
{
    $uploadedFile = combine($uploadDir, $filename);
    //save exif data
    $message = "File uploaded.";
    $exif = getImageMetadata($uploadedFile);
    $dateTaken = getExifDateTaken($uploadedFile, $exif);
    if (!$dateTaken) {
        $dateTaken = getDateFromFilename($filename);
    }
    if (!$dateTaken) {
        $dateTaken = getFileDate($uploadedFile);
    }
    $exif["dateTaken"] = $dateTaken;
    $description = arrayGetCoalesce($exif, "ImageDescription", "IPTC.Caption");
    $description = trim($description);
    writeCsvFile("{$uploadedFile}.txt", $exif);
    writeTextFile("{$uploadedFile}.js", jsValue($exif));
    //resize images and keep hd version
    $sizes = getConfig("thumbnails.sizes");
    $resized = resizeMultiple($uploadDir, $filename, $sizes);
    $keep = getConfig("thumbnails.keep");
    if ($keep) {
        moveFile("{$uploadDir}/.{$keep}", $filename, $uploadDir);
        deleteDir("{$uploadDir}/.{$keep}");
        unset($resized[$keep]);
    }
    if ($dateTaken) {
        setFileDate($uploadedFile, $dateTaken);
    }
    $vars = get_defined_vars();
    $result = array();
    $exif["meal"] = selectMeal($dateTaken);
    $result["_exif"] = $exif;
    $result["success"] = true;
    return addVarsToArray($result, "filename filesize mimeType dateTaken description", $vars);
}
Example #2
0
" title="<?php 
        echo gettext("Previous Image");
        ?>
">&laquo; <?php 
        echo gettext('Previous Image') . ' (' . $previmg->getTitle() . ')';
        ?>
</a>
				<?php 
    }
    ?>
				</div>
				<?php 
}
?>
			<?php 
if (getImageMetadata()) {
    ?>
				<div class="img-nav noshow-mobile">
				<?php 
    printImageMetadata('', false);
    ?>
				</div>
			<?php 
}
?>
			<?php 
if (function_exists('printAddToFavorites')) {
    printAddToFavorites($_zp_current_album);
}
if ($zpskel_download) {
    ?>
 /** For every album in the gallery, look for its file. Delete from the database
  * if the file does not exist. Do the same for images. Clean up comments that have
  * been left orphaned.
  *
  * Returns true if the operation was interrupted because it was taking too long
  *
  * @param bool $cascade garbage collect every image and album in the gallery.
  * @param bool $complete garbage collect every image and album in the *database* - completely cleans the database.
  * @param  int $restart Image ID to restart scan from
  * @return bool
  */
 function garbageCollect($cascade = true, $complete = false, $restart = '')
 {
     if (empty($restart)) {
         // Check for the existence of top-level albums (subalbums handled recursively).
         $result = query("SELECT * FROM " . prefix('albums'));
         $dead = array();
         $live = array('');
         // purge the root album if it exists
         $deadalbumthemes = array();
         // Load the albums from disk
         $albumfolder = getAlbumFolder();
         while ($row = mysql_fetch_assoc($result)) {
             if (!file_exists($albumfolder . UTF8ToFilesystem($row['folder'])) || in_array($row['folder'], $live)) {
                 $dead[] = $row['id'];
                 if ($row['album_theme'] !== '') {
                     // orphaned album theme options table
                     $deadalbumthemes[$row['id']] = $row['folder'];
                 }
             } else {
                 $live[] = $row['folder'];
             }
         }
         if (count($dead) > 0) {
             /* delete the dead albums from the DB */
             $first = array_pop($dead);
             $sql1 = "DELETE FROM " . prefix('albums') . " WHERE `id`='{$first}'";
             $sql2 = "DELETE FROM " . prefix('images') . " WHERE `albumid`='{$first}'";
             $sql3 = "DELETE FROM " . prefix('comments') . " WHERE `type`='albums' AND `ownerid`='{$first}'";
             $sql4 = "DELETE FROM " . prefix('obj_to_tag') . " WHERE `type`='albums' AND `objectid`='{$first}'";
             foreach ($dead as $albumid) {
                 $sql1 .= " OR `id` = '{$albumid}'";
                 $sql2 .= " OR `albumid` = '{$albumid}'";
                 $sql3 .= " OR `ownerid` = '{$albumid}'";
                 $sql4 .= " OR `objectid` = '{$albumid}'";
             }
             $n = query($sql1);
             if (!$complete && $n > 0 && $cascade) {
                 query($sql2);
                 query($sql3);
                 query($sql4);
             }
         }
         if (count($deadalbumthemes) > 0) {
             // delete the album theme options tables for dead albums
             foreach ($deadalbumthemes as $id => $deadtable) {
                 $sql = 'DELETE FROM ' . prefix('options') . ' WHERE `ownerid`=' . $id;
                 query($sql, true);
             }
         }
     }
     if ($complete) {
         if (empty($restart)) {
             /* refresh 'metadata' of dynamic albums */
             $albumfolder = getAlbumFolder();
             $albumids = query_full_array("SELECT `id`, `mtime`, `folder` FROM " . prefix('albums') . " WHERE `dynamic`='1'");
             foreach ($albumids as $album) {
                 if (($mtime = filemtime($albumfolder . UTF8ToFilesystem($album['folder']))) > $album['mtime']) {
                     // refresh
                     $data = file_get_contents($albumfolder . UTF8ToFilesystem($album['folder']));
                     while (!empty($data)) {
                         $data1 = trim(substr($data, 0, $i = strpos($data, "\n")));
                         if ($i === false) {
                             $data1 = $data;
                             $data = '';
                         } else {
                             $data = substr($data, $i + 1);
                         }
                         if (strpos($data1, 'WORDS=') !== false) {
                             $words = "words=" . urlencode(substr($data1, 6));
                         }
                         if (strpos($data1, 'THUMB=') !== false) {
                             $thumb = trim(substr($data1, 6));
                         }
                         if (strpos($data1, 'FIELDS=') !== false) {
                             $fields = "&searchfields=" . trim(substr($data1, 7));
                         }
                     }
                     if (!empty($words)) {
                         if (empty($fields)) {
                             $fields = '&searchfields=4';
                         }
                     }
                     $sql = "UPDATE " . prefix('albums') . "SET `search_params`=\"{$words}.{$fields}\", `thumb`=\"{$thumb}\", `mtime`=\"{$mtime}\" WHERE `id`=\"" . $album['id'] . "\"";
                     query($sql);
                 }
             }
             /* Delete all image entries that don't belong to an album at all. */
             $albumids = query_full_array("SELECT `id` FROM " . prefix('albums'));
             /* all the album IDs */
             $idsofalbums = array();
             foreach ($albumids as $row) {
                 $idsofalbums[] = $row['id'];
             }
             $imageAlbums = query_full_array("SELECT DISTINCT `albumid` FROM " . prefix('images'));
             /* albumids of all the images */
             $albumidsofimages = array();
             foreach ($imageAlbums as $row) {
                 $albumidsofimages[] = $row['albumid'];
             }
             $orphans = array_diff($albumidsofimages, $idsofalbums);
             /* albumids of images with no album */
             if (count($orphans) > 0) {
                 /* delete dead images from the DB */
                 $firstrow = array_pop($orphans);
                 $sql = "DELETE FROM " . prefix('images') . " WHERE `albumid`='" . $firstrow . "'";
                 foreach ($orphans as $id) {
                     $sql .= " OR `albumid`='" . $id . "'";
                 }
                 query($sql);
                 // Then go into existing albums recursively to clean them... very invasive.
                 foreach ($this->getAlbums(0) as $folder) {
                     $album = new Album($this, $folder);
                     if (!$album->isDynamic()) {
                         if (is_null($album->getDateTime())) {
                             // see if we can get one from an image
                             $image = $album->getImage(0);
                             if (is_object($image)) {
                                 $album->setDateTime($image->getDateTime());
                             }
                         }
                         $album->garbageCollect(true);
                         $album->preLoad();
                     }
                 }
             }
         }
         /* Look for image records where the file no longer exists. While at it, check for images with IPTC data to update the DB */
         $start = array_sum(explode(" ", microtime()));
         // protect against too much processing.
         if (!empty($restart)) {
             $restartwhere = ' WHERE `id`>' . $restart;
         } else {
             $restartwhere = '';
         }
         $sql = 'SELECT `id`, `albumid`, `filename`, `desc`, `title`, `date`, `mtime` FROM ' . prefix('images') . $restartwhere . ' ORDER BY `id`';
         $images = query_full_array($sql);
         foreach ($images as $image) {
             $sql = 'SELECT `folder` FROM ' . prefix('albums') . ' WHERE `id`="' . $image['albumid'] . '";';
             $row = query_single_row($sql);
             $imageName = UTF8ToFilesystem(getAlbumFolder() . $row['folder'] . '/' . $image['filename']);
             if (file_exists($imageName)) {
                 if ($image['mtime'] != filemtime($imageName)) {
                     // file has changed since we last saw it
                     /* check metadata */
                     $metadata = getImageMetadata($imageName);
                     $set = '';
                     /* title */
                     $defaultTitle = substr($image['filename'], 0, strrpos($image['filename'], '.'));
                     if (empty($defaultTitle)) {
                         $defaultTitle = $image['filename'];
                     }
                     if ($defaultTitle == $image['title']) {
                         /* default title */
                         if (isset($metadata['title'])) {
                             $set = ',`title`="' . mysql_real_escape_string($metadata['title']) . '"';
                         }
                     }
                     /* description */
                     if (!isset($row['desc'])) {
                         if (isset($metadata['desc'])) {
                             $set .= ', `desc`="' . mysql_real_escape_string($metadata['desc']) . '"';
                         }
                     }
                     /* tags */
                     if (isset($metadata['tags'])) {
                         $tags = $metadata['tags'];
                         storeTags($tags, $image['id'], 'images');
                     }
                     /* location, city, state, and country */
                     if (isset($metadata['location'])) {
                         $set .= ', `location`="' . mysql_real_escape_string($metadata['location']) . '"';
                     }
                     if (isset($metadata['city'])) {
                         $set .= ', `city`="' . mysql_real_escape_string($metadata['city']) . '"';
                     }
                     if (isset($metadata['state'])) {
                         $set .= ', `state`="' . mysql_real_escape_string($metadata['state']) . '"';
                     }
                     if (isset($metadata['country'])) {
                         $set .= ', `country`="' . mysql_real_escape_string($metadata['country']) . '"';
                     }
                     /* credit & copyright */
                     if (isset($metadata['credit'])) {
                         $set .= ', `credit`="' . escape($metadata['credit']) . '"';
                     }
                     if (isset($metadata['copyright'])) {
                         $set .= ', `copyright`="' . escape($metadata['copyright']) . '"';
                     }
                     /* date (for sorting) */
                     $newDate = strftime('%Y-%m-%d %T', filemtime($imageName));
                     if (isset($metadata['date'])) {
                         $dt = dateTimeConvert($metadata['date']);
                         if ($dt !== false) {
                             // flaw in exif/iptc data?
                             $newDate = $dt;
                         }
                     }
                     $set .= ', `date`="' . $newDate . '"';
                     /* update DB is necessary */
                     $sql = "UPDATE " . prefix('images') . " SET `EXIFValid`=0,`mtime`=" . filemtime($imageName) . $set . " WHERE `id`='" . $image['id'] . "'";
                     query($sql);
                 }
             } else {
                 $sql = 'DELETE FROM ' . prefix('images') . ' WHERE `id`="' . $image['id'] . '";';
                 $result = query($sql);
                 $sql = 'DELETE FROM ' . prefix('comments') . ' WHERE `type` IN (' . zp_image_types('"') . ') AND `ownerid` ="' . $image['id'] . '";';
                 $result = query($sql);
             }
             if (array_sum(explode(" ", microtime())) - $start >= 10) {
                 return $image['id'];
                 // avoide excessive processing
             }
         }
         /* clean the comments table */
         /* do the images */
         $imageids = query_full_array('SELECT `id` FROM ' . prefix('images'));
         /* all the image IDs */
         $idsofimages = array();
         foreach ($imageids as $row) {
             $idsofimages[] = $row['id'];
         }
         $commentImages = query_full_array("SELECT DISTINCT `ownerid` FROM " . prefix('comments') . 'WHERE `type` IN (' . zp_image_types('"') . ')');
         /* imageids of all the comments */
         $imageidsofcomments = array();
         foreach ($commentImages as $row) {
             $imageidsofcomments[] = $row['ownerid'];
         }
         $orphans = array_diff($imageidsofcomments, $idsofimages);
         /* image ids of comments with no image */
         if (count($orphans) > 0) {
             /* delete dead comments from the DB */
             $firstrow = array_pop($orphans);
             $sql = "DELETE FROM " . prefix('comments') . " WHERE `type` IN (" . zp_image_types("'") . ") AND `ownerid`='" . $firstrow . "'";
             foreach ($orphans as $id) {
                 $sql .= " OR `ownerid`='" . $id . "'";
             }
             query($sql);
         }
         /* do the same for album comments */
         $albumids = query_full_array('SELECT `id` FROM ' . prefix('albums'));
         /* all the album IDs */
         $idsofalbums = array();
         foreach ($albumids as $row) {
             $idsofalbums[] = $row['id'];
         }
         $commentAlbums = query_full_array("SELECT DISTINCT `ownerid` FROM " . prefix('comments') . 'WHERE `type`="albums"');
         /* album ids of all the comments */
         $albumidsofcomments = array();
         foreach ($commentAlbums as $row) {
             $albumidsofcomments[] = $row['ownerid'];
         }
         $orphans = array_diff($albumidsofcomments, $idsofalbums);
         /* album ids of comments with no album */
         if (count($orphans) > 0) {
             /* delete dead comments from the DB */
             $firstrow = array_pop($orphans);
             $sql = "DELETE FROM " . prefix('comments') . "WHERE `type`='albums' AND `ownerid`='" . $firstrow . "'";
             foreach ($orphans as $id) {
                 $sql .= " OR `ownerid`='" . $id . "'";
             }
             query($sql);
         }
         /* clean the tags table */
         /* do the images */
         $tagImages = query_full_array("SELECT DISTINCT `objectid` FROM " . prefix('obj_to_tag') . 'WHERE `type` IN (' . zp_image_types('"') . ')');
         /* imageids of all the comments */
         $imageidsoftags = array();
         foreach ($tagImages as $row) {
             $imageidsoftags[] = $row['objectid'];
         }
         $orphans = array_diff($imageidsoftags, $idsofimages);
         /* image ids of comments with no image */
         if (count($orphans) > 0) {
             /* delete dead tags from the DB */
             $firstrow = array_pop($orphans);
             $sql = "DELETE FROM " . prefix('obj_to_tag') . " WHERE `type` IN (" . zp_image_types('"') . ") AND (`objectid`='" . $firstrow . "'";
             foreach ($orphans as $id) {
                 $sql .= " OR `objectid`='" . $id . "'";
             }
             $sql .= ')';
             query($sql);
         }
         /* do the same for album tags */
         $tagAlbums = query_full_array("SELECT DISTINCT `objectid` FROM " . prefix('obj_to_tag') . 'WHERE `type`="albums"');
         /* album ids of all the comments */
         $albumidsoftags = array();
         foreach ($tagAlbums as $row) {
             $albumidsoftags[] = $row['objectid'];
         }
         $orphans = array_diff($albumidsoftags, $idsofalbums);
         /* album ids of comments with no album */
         if (count($orphans) > 0) {
             /* delete dead tags from the DB */
             $firstrow = array_pop($orphans);
             $sql = "DELETE FROM " . prefix('obj_to_tag') . "WHERE `type`='albums' AND `objectid`='" . $firstrow . "'";
             foreach ($orphans as $id) {
                 $sql .= " OR `objectid`='" . $id . "'";
             }
             query($sql);
         }
     }
     return false;
 }
 /**
  * Constructor for class-image
  * 
  * Do not call this constructor directly unless you really know what you are doing!
  * Use instead the function newImage() which will instantiate an object of the 
  * correct class for the file type.
  *
  * @param object &$album the owning album
  * @param sting $filename the filename of the image
  * @return Image
  */
 function _Image(&$album, $filename)
 {
     // $album is an Album object; it should already be created.
     if (!is_object($album)) {
         return NULL;
     }
     $this->classSetup($album, $filename);
     // Check if the file exists.
     if (!file_exists($this->localpath) || is_dir($this->localpath)) {
         $this->exists = false;
         return NULL;
     }
     // This is where the magic happens...
     $album_name = $album->name;
     $new = parent::PersistentObject('images', array('filename' => $filename, 'albumid' => $this->album->id), 'filename', false, empty($album_name));
     $this->getExifData();
     // prime the exif fields
     if ($new) {
         $this->updateDimensions();
         $metadata = getImageMetadata($this->localpath);
         $this->set('EXIFValid', 1);
         $newDate = '';
         if (isset($metadata['date'])) {
             $dt = dateTimeConvert($metadata['date']);
             if ($dt !== false) {
                 // flaw in exif/iptc data?
                 $newDate = $dt;
             }
         }
         if (empty($newDate)) {
             $newDate = strftime('%Y/%m/%d %T', filemtime($this->localpath));
         }
         $this->setDateTime($newDate);
         $alb = $this->album;
         if (!is_null($alb)) {
             if (is_null($alb->getDateTime()) || getOption('album_use_new_image_date')) {
                 $this->album->setDateTime($newDate);
                 //  not necessarily the right one, but will do. Can be changed in Admin
                 $this->album->save();
             }
         }
         if (isset($metadata['title'])) {
             $title = $metadata['title'];
         } else {
             $title = $this->getDefaultTitle();
         }
         $this->set('title', sanitize($title, 2));
         if (isset($metadata['desc'])) {
             $this->set('desc', sanitize($metadata['desc'], 1));
         }
         if (isset($metadata['tags'])) {
             $this->setTags(sanitize($metadata['tags'], 3));
         }
         if (isset($metadata['location'])) {
             $this->setLocation(sanitize($metadata['location'], 3));
         }
         if (isset($metadata['city'])) {
             $this->setCity(sanitize($metadata['city'], 3));
         }
         if (isset($metadata['state'])) {
             $this->setState(sanitize($metadata['state'], 3));
         }
         if (isset($metadata['country'])) {
             $this->setCountry(sanitize($metadata['country'], 3));
         }
         if (isset($metadata['credit'])) {
             $this->setCredit(sanitize($metadata['credit'], 1));
         }
         if (isset($metadata['copyright'])) {
             $this->setCopyright(sanitize($metadata['copyright'], 1));
         }
         $this->set('mtime', filemtime($this->localpath));
         apply_filter('new_image', $this);
         $this->save();
     }
 }