function buildThumbnail() { //set cropping offset $cropX = floor(($this->image->width - $this->cropWidth) / 2); $cropY = floor(($this->image->height - $this->cropHeight) / 2); //check thumbs directory exists and create it if not if (!file_exists(dirname($this->thumbPath))) { Singapore::mkdir(dirname($this->thumbPath)); } //if file is remote then copy locally first if ($this->image->isRemote()) { $ip = @fopen($this->imagePath, "rb"); $tp = @fopen($this->thumbPath, "wb"); if ($ip && $tp) { while (fwrite($tp, fread($ip, 4095))) { } fclose($tp); fclose($ip); $this->imagePath = $this->thumbPath; } } switch ($this->config->thumbnail_software) { case "im": //use ImageMagick v5.x $cmd = '"' . $this->config->pathto_convert . '"'; if ($this->forceSize) { $cmd .= " -crop {$this->cropWidth}x{$this->cropHeight}+{$this->cropX}+{$this->cropY}"; } $cmd .= " -geometry {$this->thumbWidth}x{$this->thumbHeight}"; if ($this->image->type == 2) { $cmd .= " -quality " . $this->config->thumbnail_quality; } if ($this->config->progressive_thumbs) { $cmd .= " -interlace Plane"; } if ($this->config->remove_jpeg_profile) { $cmd .= ' +profile "*"'; } $cmd .= ' ' . escapeshellarg($this->imagePath) . ' ' . escapeshellarg($this->thumbPath); exec($cmd); break; case "im6": //use ImageMagick v6.x $cmd = '"' . $this->config->pathto_convert . '"'; $cmd .= ' ' . escapeshellarg($this->imagePath); if ($this->config->progressive_thumbs) { $cmd .= " -interlace Plane"; } if ($this->image->type == 2) { $cmd .= " -quality " . $this->config->thumbnail_quality; } if ($this->forceSize) { $cmd .= " -crop {$this->cropWidth}x{$this->cropHeight}+{$this->cropX}+{$this->cropY}"; } $cmd .= " -resize {$this->thumbWidth}x{$this->thumbHeight}"; if ($this->config->remove_jpeg_profile) { $cmd .= ' +profile "*"'; } $cmd .= ' ' . escapeshellarg($this->thumbPath); exec($cmd); break; case "gd2": case "gd1": default: //use GD by default //read in image as appropriate type switch ($this->image->type) { case 1: $image = ImageCreateFromGIF($this->imagePath); break; case 3: $image = ImageCreateFromPNG($this->imagePath); break; case 2: default: $image = ImageCreateFromJPEG($this->imagePath); break; } if ($image) { switch ($this->config->thumbnail_software) { case "gd2": //create blank truecolor image $thumb = ImageCreateTrueColor($this->thumbWidth, $this->thumbHeight); //resize image with resampling ImageCopyResampled($thumb, $image, 0, 0, $cropX, $cropY, $this->thumbWidth, $this->thumbHeight, $this->cropWidth, $this->cropHeight); break; case "gd1": default: //create blank 256 color image $thumb = ImageCreate($this->thumbWidth, $this->thumbHeight); //resize image ImageCopyResized($thumb, $image, 0, 0, $cropX, $cropY, $this->thumbWidth, $this->thumbHeight, $this->cropWidth, $this->cropHeight); break; } } /*else { $thumb = ImageCreate($this->thumbWidth, $this->thumbHeight); $bg = ImageColorAllocate($thumb, 255, 255, 255); $text = ImageColorAllocate($thumb, 255, 0, 0); ImageString($thumb, 1, 0, 0, "Cannot load source image", $text); }*/ //set image interlacing @ImageInterlace($thumb, $this->config->progressive_thumbs); //output image of appropriate type switch ($this->image->type) { case 1: //GIF images are saved as PNG //GIF images are saved as PNG case 3: ImagePNG($thumb, $this->thumbPath); break; case 2: default: ImageJPEG($thumb, $this->thumbPath, $this->config->thumbnail_quality); break; } @ImageDestroy($image); @ImageDestroy($thumb); } //set file permissions on newly created thumbnail @chmod($this->thumbPath, octdec($this->config->file_mode)); }
/** * Fetches gallery info for the specified gallery and immediate children. * @param string gallery id * @param string language code spec for this request (optional) * @param int number of levels of child galleries to fetch (optional) * @return sgGallery the gallery object created */ function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null) { $gal = new sgGallery($galleryId, $parent); if ($language == null) { $translator = Translator::getInstance(); $language = $translator->language; } //try to open language specific metadata $fp = @fopen($this->config->base_path . $this->config->pathto_galleries . $galleryId . "/metadata.{$language}.csv", "r"); //if fail then try to open generic metadata if (!$fp) { $fp = @fopen($this->config->base_path . $this->config->pathto_galleries . $galleryId . "/metadata.csv", "r"); } if ($fp) { while ($temp[] = fgetcsv($fp, 2048)) { } fclose($fp); list($gal->filename, $gal->thumbnail, $gal->owner, $gal->groups, $gal->permissions, $gal->categories, $gal->name, $gal->artist, $gal->email, $gal->copyright, $gal->desc, $gal->summary, $gal->date) = $temp[1]; //only fetch individual images if child galleries are required if ($getChildGalleries) { for ($i = 0; $i < count($temp) - 3; $i++) { $gal->images[$i] = new sgImage($temp[$i + 2][0], $gal, $this->config); list(, $gal->images[$i]->thumbnail, $gal->images[$i]->owner, $gal->images[$i]->groups, $gal->images[$i]->permissions, $gal->images[$i]->categories, $gal->images[$i]->name, $gal->images[$i]->artist, $gal->images[$i]->email, $gal->images[$i]->copyright, $gal->images[$i]->desc, $gal->images[$i]->location, $gal->images[$i]->date, $gal->images[$i]->camera, $gal->images[$i]->lens, $gal->images[$i]->film, $gal->images[$i]->darkroom, $gal->images[$i]->digital) = $temp[$i + 2]; //get image size and type list($gal->images[$i]->width, $gal->images[$i]->height, $gal->images[$i]->type) = @GetImageSize($gal->images[$i]->realPath()); } //otherwise just fill in empty images } else { if (count($temp) > 3) { for ($i = 0; $i < count($temp) - 3; $i++) { $gal->images[$i] = new sgImage($temp[$i + 2][0], $gal); } } } } else { //no metadata found so use iifn method implemented in superclass return parent::getGallery($galleryId, $parent, $getChildGalleries, $language); } //discover child galleries $dir = Singapore::getListing($this->config->base_path . $this->config->pathto_galleries . $galleryId . "/"); if ($getChildGalleries) { //but only fetch their info if required too foreach ($dir->dirs as $gallery) { $gal->galleries[] = $this->getGallery($galleryId . "/" . $gallery, $gal, $getChildGalleries - 1, $language); } } else { //otherwise just copy their names in so they can be counted $gal->galleries = $dir->dirs; } return $gal; }
/** * Fetches gallery info for the specified gallery and immediate children. * @param string gallery id * @param string language code spec for this request (optional) * @param int number of levels of child galleries to fetch (optional) */ function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null) { $gal = new sgGallery($galleryId, $parent); if ($language == null) { $language = $this->config->default_language; } //try to open language specific gallery info $res = $this->query("SELECT * FROM " . $this->config->sql_prefix . "galleries " . "WHERE id='" . $this->escape_string($galleryId) . "' " . "AND lang='" . $this->escape_string($language) . "'"); //if fail then try to open generic gallery info if (!$res || !$this->num_rows($res)) { $res = $this->query("SELECT * FROM " . $this->config->sql_prefix . "galleries " . "WHERE id='" . $this->escape_string($galleryId) . "' and lang=''"); } //if that succeeds then get galleries from db if ($res && $this->num_rows($res)) { $galinfo = $this->fetch_array($res); $gal->filename = $galinfo['filename']; $gal->owner = $galinfo['owner']; $gal->groups = $galinfo['groups']; $gal->permissions = $galinfo['permissions']; $gal->categories = $galinfo['categories']; $gal->name = $galinfo['name']; $gal->artist = $galinfo['artist']; $gal->email = $galinfo['email']; $gal->copyright = $galinfo['copyright']; $gal->desc = $galinfo['description']; $gal->summary = $galinfo['summary']; $gal->date = $galinfo['date']; $gal->hits = $galinfo['hits']; $gal->lasthit = $galinfo['lasthit']; //try to open language specific image info $res = $this->query("SELECT * FROM " . $this->config->sql_prefix . "images " . "WHERE galleryid='" . $this->escape_string($galleryId) . "' " . "AND lang='" . $this->escape_string($language) . "'"); //if fail then try to open generic image info if (!$res || !$this->num_rows($res)) { $res = $this->query("SELECT * FROM " . $this->config->sql_prefix . "images " . "WHERE galleryid='" . $this->escape_string($galleryId) . "' and lang=''"); } for ($i = 0; $i < $this->num_rows($res); $i++) { $imginfo = $this->fetch_array($res); $gal->images[$i] = new sgImage($imginfo['filename'], $gal); $gal->images[$i]->thumbnail = $imginfo['thumbnail']; $gal->images[$i]->owner = $imginfo['owner']; $gal->images[$i]->groups = $imginfo['groups']; $gal->images[$i]->permissions = $imginfo['permissions']; $gal->images[$i]->categories = $imginfo['categories']; $gal->images[$i]->name = $imginfo['name']; $gal->images[$i]->artist = $imginfo['artist']; $gal->images[$i]->email = $imginfo['email']; $gal->images[$i]->copyright = $imginfo['copyright']; $gal->images[$i]->desc = $imginfo['description']; $gal->images[$i]->location = $imginfo['location']; $gal->images[$i]->date = $imginfo['date']; $gal->images[$i]->camera = $imginfo['camera']; $gal->images[$i]->lens = $imginfo['lens']; $gal->images[$i]->film = $imginfo['film']; $gal->images[$i]->darkroom = $imginfo['darkroom']; $gal->images[$i]->digital = $imginfo['digital']; $gal->images[$i]->width = $imginfo['width']; $gal->images[$i]->height = $imginfo['height']; $gal->images[$i]->type = $imginfo['type']; $gal->images[$i]->hits = $imginfo['hits']; $gal->images[$i]->lasthit = $imginfo['lasthit']; } } else { //no record found so use iifn method implemented in parent class return parent::getGallery($galleryId, $parent, $getChildGalleries, $language); } //discover child galleries $dir = Singapore::getListing($this->config->base_path . $this->config->pathto_galleries . $galleryId . "/"); if ($getChildGalleries) { //but only fetch their info if required too foreach ($dir->dirs as $gallery) { $gal->galleries[] = $this->getGallery($galleryId . "/" . $gallery, $gal, $getChildGalleries - 1, $language); } } else { //otherwise just copy their names in so they can be counted $gal->galleries = $dir->dirs; } return $gal; }
/** * Adds the contents of an uploaded archive to the database. * * @return boolean true on success; false otherwise */ function addMultipleImages() { //find system temp directory if (!($systmpdir = $this->findTempDirectory())) { return $this->pushError($this->translator->_g("Unable to find temporary storage space.")); } //create new temp directory in system temp dir but stop after 100 attempts while (!Singapore::mkdir($tmpdir = $systmpdir . "/" . uniqid("sg")) && $tries++ < 100) { } $archive = $_FILES["sgArchiveFile"]["tmp_name"]; if (!is_uploaded_file($archive)) { return $this->pushError($this->translator->_g("Could not upload file.")); } //decompress archive to temp $cmd = escapeshellcmd($this->config->pathto_unzip); $cmd .= ' -d "' . escapeshellcmd(realpath($tmpdir)); $cmd .= '" "' . escapeshellcmd(realpath($archive)) . '"'; if (!exec($cmd)) { return $this->pushError($this->translator->_g("Could not decompress archive.")); } //start processing archive contents $wd = $tmpdir; $contents = $this->getListing($wd, $this->config->recognised_extensions); //cope with archives contained within a directory if (empty($contents->files) && count($contents->dirs) == 1) { $contents = $this->getListing($wd .= '/' . $contents->dirs[0], $this->config->recognised_extensions); } $success = true; //add any images to current gallery foreach ($contents->files as $image) { //check image is valid and ignore it if it isn't if (!preg_match("/\\.(" . $this->config->recognised_extensions . ")\$/i", $image)) { $imgInfo = GetImageSize($wd . '/' . $image); switch ($imgInfo[2]) { case 1: $image .= '.gif'; break; case 2: $image .= '.jpg'; break; case 3: $image .= '.png'; break; case 6: $image .= '.bmp'; break; case 7: case 8: $image .= '.tif'; break; default: $this->pushMessage($this->translator->_g("Uploaded image '%s' has unrecognised extension and image type could not be determined from file contents.", $image)); continue; } } $path = $this->config->pathto_galleries . $this->gallery->id . "/" . $image; $srcImage = $image; if (file_exists($path)) { switch ($this->config->upload_overwrite) { case 1: //overwrite $this->deleteImage($image); break; case 2: //generate unique for ($i = 0; file_exists($path); $i++) { $pivot = strrpos($srcImage, "."); $image = substr($srcImage, 0, $pivot) . '-' . $i . substr($srcImage, $pivot, strlen($srcImage) - $pivot); $path = $this->config->base_path . $this->config->pathto_galleries . $this->gallery->id . "/" . $image; } break; case 0: //raise error //raise error default: $this->pushError($this->translator->_g("File '%s' already exists.")); $success = false; continue; } } copy($wd . '/' . $srcImage, $path); // try to change file-permissions @chmod($path, octdec($this->config->file_mode)); $img = new sgImage($image, $this->gallery); $img->name = strtr(substr($image, strrpos($image, "/"), strrpos($image, ".") - strlen($image)), "_", " "); list($img->width, $img->height, $img->type) = GetImageSize($path); //leave owner of guest-uploaded files as default '__nobody__' if (!$this->user->isGuest()) { $img->owner = $this->user->username; } $this->gallery->images[] = $img; } //add any directories as subgalleries, if allowed if ($this->config->allow_dir_upload == 1 && !$this->user->isGuest() || $this->config->allow_dir_upload == 2 && $this->user->isAdmin()) { foreach ($contents->dirs as $gallery) { $path = $this->config->pathto_galleries . $this->gallery->id . "/" . $gallery; if (file_exists($path)) { switch ($this->config->upload_overwrite) { case 1: //overwrite $this->deleteGallery($this->gallery->id . '/' . $gallery); break; case 2: //generate unique for ($i = 0; file_exists($path); $i++) { $path = $this->config->pathto_galleries . $this->gallery->id . "/" . $gallery . '-' . $i; } break; case 0: //raise error //raise error default: $this->pushError($this->translator->_g("File '%s' already exists.")); $success = false; continue; } } //move from temp dir to gallery rename($wd . '/' . $gallery, $path); //change directory permissions (but not contents) @chmod($path, octdec($this->config->directory_mode)); } } //if images were added save metadata if (!empty($contents->files)) { $this->io->putGallery($this->gallery) or $this->pushError($this->translator->_g("Unable to save metadata.")); } //if subgalleries were added reload gallery data if (!empty($contents->dirs)) { $this->selectGallery(); } //remove temporary directory $this->rmdir_all($tmpdir); if ($success) { return $this->pushMessage($this->translator->_g("Archive contents added.")); } else { return $this->pushError($this->translator->_g("Some archive contents could not be added.")); } }
/** * Recursively deletes all directories and files in the specified directory. * USE WITH EXTREME CAUTION!! * @returns boolean true on success; false otherwise * @static */ function rmdir_all($wd) { if (!($dp = opendir($wd))) { return false; } $success = true; while (false !== ($entry = readdir($dp))) { if ($entry == "." || $entry == "..") { continue; } if (is_dir("{$wd}/{$entry}")) { $success &= Singapore::rmdir_all("{$wd}/{$entry}"); } else { $success &= unlink("{$wd}/{$entry}"); } } closedir($dp); $success &= rmdir($wd); return $success; }