function MediaThumbnail($Media, $Data = FALSE) { $Media = (array) $Media; if (GetValue('ThumbPath', $Media)) { $Src = Gdn_Upload::Url(ltrim(GetValue('ThumbPath', $Media), '/')); } else { $Width = GetValue('ImageWidth', $Media); $Height = GetValue('ImageHeight', $Media); if (!$Width || !$Height) { $Height = MediaModel::ThumbnailHeight(); if (!$Height) { $Height = 100; } SetValue('ThumbHeight', $Media, $Height); return DefaultMediaThumbnail($Media); } $RequiresThumbnail = FALSE; if (MediaModel::ThumbnailHeight() && $Height > MediaModel::ThumbnailHeight()) { $RequiresThumbnail = TRUE; } elseif (MediaModel::ThumbnailWidth() && $Width > MediaModel::ThumbnailWidth()) { $RequiresThumbnail = TRUE; } $Path = ltrim(GetValue('Path', $Media), '/'); if ($RequiresThumbnail) { $Src = Url('/utility/thumbnail/' . GetValue('MediaID', $Media, 'x') . '/' . $Path, TRUE); } else { $Src = Gdn_Upload::Url($Path); } } if ($Data) { $Result = array('src' => $Src, 'width' => GetValue('ThumbWidth', $Media), 'height' => GetValue('ThumbHeight', $Media)); } else { $Result = Img($Src, array('class' => 'ImageThumbnail', 'width' => GetValue('ThumbWidth', $Media), 'height' => GetValue('ThumbHeight', $Media))); } return $Result; }
public function UtilityController_Thumbnail_Create($Sender, $Args) { $SubPath = implode('/', $Args); $Path = MediaModel::PathUploads() . "/{$SubPath}"; if (!file_exists($Path)) { throw NotFoundException('File'); } // Figure out the dimensions of the upload. $ImageSize = getimagesize($Path); $SHeight = $ImageSize[1]; $SWidth = $ImageSize[0]; $Options = array(); $ThumbHeight = MediaModel::ThumbnailHeight(); $ThumbWidth = MediaModel::ThumbnailWidth(); if (!$ThumbHeight || $SHeight < $ThumbHeight) { $Height = $SHeight; $Width = $SWidth; } else { $Height = $ThumbHeight; $Width = round($Height * $SWidth / $SHeight); } if ($ThumbWidth && $Width > $ThumbWidth) { $Width = $ThumbWidth; if (!$ThumbHeight) { $Height = round($Width * $SHeight / $SWidth); } else { $Options['Crop'] = TRUE; } } $TargetPath = MediaModel::PathUploads() . "/thumbnails/{$SubPath}"; if (!file_exists(dirname($TargetPath))) { mkdir(dirname($TargetPath), 0777, TRUE); } Gdn_UploadImage::SaveImageAs($Path, $TargetPath, $Height, $Width, $Options); $Url = MediaModel::Url("/thumbnails/{$SubPath}"); Redirect($Url, 302); // Gdn_FileSystem::ServeFile($TargetPath, basename($Path), '', 'inline'); }
/** * Create and display a thumbnail of an uploaded file. */ public function UtilityController_Thumbnail_Create($Sender, $Args = array()) { $MediaID = array_shift($Args); if (!is_numeric($MediaID)) { array_unshift($Args, $MediaID); } $SubPath = implode('/', $Args); // Fix mauling of protocol:// URLs. $SubPath = preg_replace('/:\\/{1}/', '://', $SubPath); $Name = $SubPath; $Parsed = Gdn_Upload::Parse($Name); // Get actual path to the file. $Path = Gdn_Upload::CopyLocal($SubPath); if (!file_exists($Path)) { throw NotFoundException('File'); } // Figure out the dimensions of the upload. $ImageSize = getimagesize($Path); $SHeight = $ImageSize[1]; $SWidth = $ImageSize[0]; if (!in_array($ImageSize[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { if (is_numeric($MediaID)) { // Fix the thumbnail information so this isn't requested again and again. $Model = new MediaModel(); $Media = array('MediaID' => $MediaID, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => NULL); $Model->Save($Media); } $Url = Asset('/plugins/FileUpload/images/file.png'); Redirect($Url, 301); } $Options = array(); $ThumbHeight = MediaModel::ThumbnailHeight(); $ThumbWidth = MediaModel::ThumbnailWidth(); if (!$ThumbHeight || $SHeight < $ThumbHeight) { $Height = $SHeight; $Width = $SWidth; } else { $Height = $ThumbHeight; $Width = round($Height * $SWidth / $SHeight); } if ($ThumbWidth && $Width > $ThumbWidth) { $Width = $ThumbWidth; if (!$ThumbHeight) { $Height = round($Width * $SHeight / $SWidth); } else { $Options['Crop'] = TRUE; } } $TargetPath = "thumbnails/{$Parsed['Name']}"; $ThumbParsed = Gdn_UploadImage::SaveImageAs($Path, $TargetPath, $Height, $Width, $Options); // Cleanup if we're using a scratch copy if ($ThumbParsed['Type'] != '' || $Path != MediaModel::PathUploads() . '/' . $SubPath) { @unlink($Path); } if (is_numeric($MediaID)) { // Save the thumbnail information. $Model = new MediaModel(); $Media = array('MediaID' => $MediaID, 'ThumbWidth' => $ThumbParsed['Width'], 'ThumbHeight' => $ThumbParsed['Height'], 'ThumbPath' => $ThumbParsed['SaveName']); $Model->Save($Media); } $Url = $ThumbParsed['Url']; Redirect($Url, 301); // Gdn_FileSystem::ServeFile($TargetPath, basename($Path), '', 'inline'); }