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)); }
/** * 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.")); } }