public static Guid ComputeOverlay(string sourceFilePath) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); var processStartInfo = new ProcessStartInfo(); processStartInfo.WorkingDirectory = TempFileManager.GetTempDirectory(); processStartInfo.UseShellExecute = false; processStartInfo.ErrorDialog = false; processStartInfo.CreateNoWindow = true; processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; string oldFilePath = fileContainer.SourceFileItem.FilePath; string outputFilePath = null; try { LogManager.AddOverlayMessage("SourceFileName " + Path.GetFileName(oldFilePath), "Start Crop"); // resize + crop source image outputFilePath = Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".png"); processStartInfo.FileName = Path.Combine(FrontSettings.ImageMagickPath, "convert"); processStartInfo.Arguments = $"{Path.GetFileName(fileContainer.SourceFileItem.FilePath)} -resize \"{_finalWidth}x{_finalHeight}^\" -gravity Center -crop {_finalWidth}x{_finalHeight}+0+0 {Path.GetFileName(outputFilePath)}"; StartProcess(processStartInfo, 5000); fileContainer.SourceFileItem.FilePath = outputFilePath; LogManager.AddOverlayMessage("OutputFileName " + Path.GetFileName(outputFilePath), "End Crop"); IpfsDaemon.Queue(fileContainer.SourceFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(outputFilePath); LogManager.AddOverlayMessage(ex.ToString(), "Exception"); return(fileContainer.ProgressToken); } finally { TempFileManager.SafeDeleteTempFile(oldFilePath); } try { LogManager.AddOverlayMessage("SourceFileName " + Path.GetFileName(fileContainer.SourceFileItem.FilePath), "Start Overlay"); // watermark source image outputFilePath = Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".png"); processStartInfo.FileName = Path.Combine(FrontSettings.ImageMagickPath, "composite"); processStartInfo.Arguments = $"-gravity NorthEast {_overlayImagePath} {Path.GetFileName(fileContainer.SourceFileItem.FilePath)} {Path.GetFileName(outputFilePath)}"; StartProcess(processStartInfo, 5000); fileContainer.OverlayFileItem.FilePath = outputFilePath; LogManager.AddOverlayMessage("OutputFileName " + Path.GetFileName(outputFilePath), "End Overlay"); IpfsDaemon.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(outputFilePath); LogManager.AddOverlayMessage(ex.ToString(), "Exception"); return(fileContainer.ProgressToken); } return(fileContainer.ProgressToken); }
private static void StartProcess(ProcessStartInfo processStartInfo, int timeout) { LogManager.AddOverlayMessage(processStartInfo.FileName + " " + processStartInfo.Arguments, "Launch command"); using (Process process = Process.Start(processStartInfo)) { bool success = process.WaitForExit(timeout); if (!success) { throw new InvalidOperationException("Timeout : Le fichier n'a pas pu être encodé dans le temps imparti."); } if (process.ExitCode != 0) { throw new InvalidOperationException($"Le fichier n'a pas pu être encodé, erreur {process.ExitCode}."); } } }
public async Task <IActionResult> OverlayImage() { try { return(Ok(new { success = true, token = OverlayManager.ComputeOverlay(await GetFileToTemp()) })); } catch (Exception ex) { LogManager.AddOverlayMessage(LogLevel.Critical, "Exception non gérée", "Exception", ex); return(BadRequest(new { errorMessage = ex.Message })); } }
public static void ResizeImage(FileContainer fileContainer) { try { string oldFilePath = fileContainer.SourceFileItem.FilePath; using (Image sourceImage = Image.FromFile(oldFilePath)) { //create a bitmap to hold the new image using (var finalBitmap = new Bitmap(_finalWidth, _finalHeight)) { using (Graphics graphics = Graphics.FromImage(finalBitmap)) { Rectangle sourceRectangle; Rectangle destRectangle = new Rectangle(0, 0, _finalWidth, _finalHeight); // video verticale, garder largeur _finalWidth, couper la hauteur if ((double)sourceImage.Width / (double)sourceImage.Height < ((double)_finalWidth / (double)_finalHeight)) { int hauteur = sourceImage.Width * _finalHeight / _finalWidth; int yOffset = (sourceImage.Height - hauteur) / 2; sourceRectangle = new Rectangle(0, yOffset, sourceImage.Width, hauteur); } else // video horizontale, garder hauteur _finalHeight, couper la largeur { int largeur = sourceImage.Height * _finalWidth / _finalHeight; int xOffset = (sourceImage.Width - largeur) / 2; sourceRectangle = new Rectangle(xOffset, 0, largeur, sourceImage.Height); } graphics.DrawImage(sourceImage, destRectangle, sourceRectangle, GraphicsUnit.Pixel); } string outputFilePath = System.IO.Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".jpeg"); finalBitmap.Save(outputFilePath, ImageFormat.Jpeg); fileContainer.SourceFileItem.FilePath = outputFilePath; } } IpfsDaemon.Queue(fileContainer.SourceFileItem); TempFileManager.SafeDeleteTempFile(oldFilePath); } catch (Exception ex) { LogManager.AddOverlayMessage(ex.ToString(), "Exception"); } }
public static Guid ComputeOverlay(string sourceFilePath, int?x = null, int?y = null) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); ResizeImage(fileContainer); try { using (Image overlayImage = Image.FromFile(_overlayImagePath)) { using (Image sourceImage = Image.FromFile(fileContainer.SourceFileItem.FilePath)) { using (Graphics graphics = Graphics.FromImage(sourceImage)) { // Si position n'est pas fournie, centrer l'image if (x == null || y == null) { x = (sourceImage.Width / 2) - (overlayImage.Width / 2); y = (sourceImage.Height / 2) - (overlayImage.Height / 2); } graphics.DrawImage(overlayImage, x.Value, y.Value); } string outputFilePath = System.IO.Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".jpeg"); sourceImage.Save(outputFilePath, ImageFormat.Jpeg); fileContainer.OverlayFileItem.FilePath = outputFilePath; } } IpfsDaemon.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { LogManager.AddOverlayMessage(ex.ToString(), "Exception"); } return(fileContainer.ProgressToken); }
public static Guid ComputeOverlay(string sourceFilePath) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); var sourceFile = fileContainer.SourceFileItem; try { LogManager.AddOverlayMessage(LogLevel.Information, "SourceFileName " + Path.GetFileName(sourceFile.SourceFilePath), "Start Crop"); // resize + crop source image string arguments = $"{Path.GetFileName(sourceFile.SourceFilePath)} -resize \"{_finalWidth}x{_finalHeight}^\" -gravity Center -crop {_finalWidth}x{_finalHeight}+0+0 {Path.GetFileName(sourceFile.TempFilePath)}"; var process = new ProcessManager(Path.Combine(GeneralSettings.Instance.ImageMagickPath, "convert"), arguments, LogManager.OverlayLogger); bool success = process.Launch(5); if (!success) { TempFileManager.SafeDeleteTempFile(sourceFile.SourceFilePath); TempFileManager.SafeDeleteTempFile(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Error, "Erreur convert", "Erreur"); return(fileContainer.ProgressToken); } sourceFile.SetOutputFilePath(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Information, "OutputFileName " + Path.GetFileName(sourceFile.OutputFilePath), "End Crop"); IpfsDaemon.Instance.Queue(sourceFile); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(sourceFile.SourceFilePath); TempFileManager.SafeDeleteTempFile(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Critical, "Exception non gérée", "Exception", ex); return(fileContainer.ProgressToken); } // remplacement de l'image source string oldSourceFilePath = sourceFile.SourceFilePath; TempFileManager.SafeDeleteTempFile(oldSourceFilePath); sourceFile.SetSourceFilePath(sourceFile.TempFilePath); // changement de la source de OverlayFileItem fileContainer.OverlayFileItem.SetSourceFilePath(sourceFile.SourceFilePath); try { LogManager.AddOverlayMessage(LogLevel.Information, "SourceFileName " + Path.GetFileName(fileContainer.OverlayFileItem.SourceFilePath), "Start Overlay"); // watermark source image string arguments = $"-gravity NorthEast {_overlayImagePath} {Path.GetFileName(fileContainer.OverlayFileItem.SourceFilePath)} {Path.GetFileName(fileContainer.OverlayFileItem.TempFilePath)}"; var process = new ProcessManager(Path.Combine(GeneralSettings.Instance.ImageMagickPath, "composite"), arguments, LogManager.OverlayLogger); bool success = process.Launch(5); if (!success) { TempFileManager.SafeDeleteTempFile(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Error, "Erreur composite", "Erreur"); return(fileContainer.ProgressToken); } fileContainer.OverlayFileItem.SetOutputFilePath(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Information, "OutputFileName " + Path.GetFileName(fileContainer.OverlayFileItem.OutputFilePath), "End Overlay"); IpfsDaemon.Instance.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Critical, "Exception non gérée", "Exception", ex); return(fileContainer.ProgressToken); } return(fileContainer.ProgressToken); }