/**
* Create and display a thumbnail of an uploaded file.
*/
public function utilityController_mediaThumbnail_create($sender, $media_id)
{
// When it makes it into core, it will be available in
// functions.general.php
require 'generate_thumbnail.php';
$model = new Gdn_Model('Media');
$media = $model->getID($media_id, DATASET_TYPE_ARRAY);
if (!$media) {
throw notFoundException('File');
}
// Get actual path to the file.
$local_path = Gdn_Upload::copyLocal($media['Path']);
if (!file_exists($local_path)) {
throw notFoundException('File');
}
$file_extension = pathinfo($local_path, PATHINFO_EXTENSION);
// Generate new path for thumbnail
$thumb_path = $this->getBaseUploadDestinationDir() . '/' . 'thumb';
// Grab full path with filename, and validate it.
$thumb_destination_path = $this->getAbsoluteDestinationFilePath($local_path, $file_extension, $thumb_path);
// Create thumbnail, and grab debug data from whole process.
$thumb_payload = generate_thumbnail($local_path, $thumb_destination_path, array('height' => c('Plugins.FileUpload.ThumbnailHeight', 128)));
if ($thumb_payload['success'] === true) {
// Thumbnail dimensions
$thumb_height = round($thumb_payload['result_height']);
$thumb_width = round($thumb_payload['result_width']);
// Move the thumbnail to its proper location. Calling SaveAs with
// cloudfiles enabled will trigger the move to cloudfiles, so use
// same path for each arg in SaveAs. The file will be removed from the local filesystem.
$parsed = Gdn_Upload::parse($thumb_destination_path);
$target = $thumb_destination_path;
// $parsed['Name'];
$Upload = new Gdn_Upload();
$filepath_parsed = $Upload->saveAs($thumb_destination_path, $target, array('source' => 'content'));
// Save thumbnail information to DB.
$model->save(array('MediaID' => $media_id, 'StorageMethod' => $filepath_parsed['Type'], 'ThumbWidth' => $thumb_width, 'ThumbHeight' => $thumb_height, 'ThumbPath' => $filepath_parsed['SaveName']));
// Remove cf scratch copy, typically in cftemp, if there was actually a file pulled in from CF.
if (strpos($local_path, 'cftemp') !== false) {
if (!unlink($local_path)) {
// Maybe add logging for local cf copies not deleted.
}
}
$url = $filepath_parsed['Url'];
} else {
// Fix the thumbnail information so this isn't requested again and again.
$model->save(array('MediaID' => $media_id, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => ''));
$url = asset('/plugins/FileUpload/images/file.png');
}
redirect($url, 301);
}