public JsonResult CropPicture(int entityID, int uploadID, double x, double y, double width, double height) { try { var upload = uploadRepository.GetById(uploadID); var entity = entityRepository.GetById(entityID); var currentEntity = SessionHelper.CurrentEntity; var citizen = SessionHelper.LoggedCitizen; var crop = new CropRectangle((int)x, (int)y, (int)width, (int)height); var result = uploadAvatarService.CanCropUpload(upload, entity, currentEntity, citizen, crop); if (result.IsError) { return(JsonError(result)); } var newUpload = uploadAvatarService.CropUploadAndMoveToAnotherLocation(upload, crop, UploadLocationEnum.Avatars); entity.ImgUrl = uploadAvatarService.GetRelativePathForLocation(newUpload); uploadRepository.SaveChanges(); return(JsonData(new { msg = "Avatar changed!", img = entity.ImgUrl })); } catch (Exception e) { return(UndefinedJsonError(e)); } }
public static Bitmap cropImage(Image image, CropRectangle crop) { var rect = new Rectangle(crop.X, crop.Y, crop.Width, crop.Height); Bitmap bmpImage = new Bitmap(image); return(bmpImage.Clone(rect, bmpImage.PixelFormat)); }
public MethodResult CanCropUpload(Upload upload, Entity entity, Entity currentEntity, Citizen currentCitizen, CropRectangle crop) { if (upload.UploadedByCitizenID != currentCitizen.ID) { return(new MethodResult("This is not your upload!")); } switch (entity.GetEntityType()) { case EntityTypeEnum.Citizen: if (entity.EntityID != currentEntity.EntityID) { return(new MethodResult("You cannot do that!")); } break; case EntityTypeEnum.Company: { var rights = companyService.GetCompanyRights(entity.Company, currentEntity, currentCitizen); if (rights.CanSwitch == false) { return(new MethodResult("You cannot do that!")); } break; } case EntityTypeEnum.Newspaper: { var rights = newspaperService.GetNewspaperRights(entity.Newspaper, currentEntity, currentCitizen); if (rights != NewspaperRightsEnum.Full) { return(new MethodResult("You cannot do that!")); } break; } case EntityTypeEnum.Party: { var role = partyService.GetPartyRole(currentCitizen, entity.Party); if (role < PartyRoleEnum.Manager) { return(new MethodResult("You cannot do that!")); } break; } } if (crop.X < 0 || crop.Y < 0) { return(new MethodResult("Wrong crop!")); } using (var image = Image.FromFile(GetFilePathForLocation(upload))) { if (crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height) { return(new MethodResult("Wrong crop!")); } } return(MethodResult.Success); }
/// <summary> /// Convert a media file via ffmpeg. /// /// </summary> /// <param name="inputFilepath">The input media file path.</param> /// <param name="outputFilepath">The output media file path.</param> /// <param name="outputFormat">The format to which to convert.</param> /// <param name="trimLength">Optional</param> /// <param name="trimFromPosition">Optional</param> /// <param name="audioBitRate">Optional</param> /// <param name="audioSampleRate">Optional</param> /// <param name="target">Optional</param> /// <param name="targetStandard">Optional</param> /// <param name="videoAspectRatio">Optional</param> /// <param name="videoBitRate">Optional</param> /// <param name="videoFps">Optional</param> /// <param name="videoSize">Optional</param> /// <param name="customWidth">Optional</param> /// <param name="customHeight">Optional</param> /// <param name="sourceCrop">Optional</param> /// <param name="baselineProfile">Optional</param> /// <returns></returns> public async Task ConvertFile(Uri inputFilepath, Uri outputFilepath, string outputFormat, TimeSpan?trimLength = null, TimeSpan?trimFromPosition = null, int?audioBitRate = null, AudioSampleRate?audioSampleRate = null, Target?target = null, TargetStandard?targetStandard = null, VideoAspectRatio?videoAspectRatio = null, int?videoBitRate = null, int?videoFps = null, VideoSize?videoSize = null, int?customWidth = null, int?customHeight = null, CropRectangle sourceCrop = null, bool?baselineProfile = null) { if (inputFilepath.LocalPath.Equals(outputFilepath.LocalPath + "." + outputFormat) || inputFilepath == null || outputFilepath == null) { throw new ArgumentException("Input and output paths cannot be the same or null."); } if (outputFormat == null) { throw new ArgumentException("Output format cannot be null."); } var inputFile = new MediaFile(inputFilepath.LocalPath); var outputFile = new MediaFile(outputFilepath.LocalPath + "." + outputFormat); var options = new ConversionOptions(); if (trimFromPosition.HasValue && trimLength.HasValue) { options.CutMedia(trimFromPosition.Value, trimLength.Value); } else if (trimLength.HasValue) { options.CutMedia(TimeSpan.FromSeconds(0), trimLength.Value); } if (audioBitRate.HasValue) { options.AudioBitRate = audioBitRate.Value; } if (audioSampleRate.HasValue) { options.AudioSampleRate = audioSampleRate.Value; } if (target.HasValue) { options.Target = target.Value; } if (targetStandard.HasValue) { options.TargetStandard = targetStandard.Value; } if (videoAspectRatio.HasValue) { options.VideoAspectRatio = videoAspectRatio.Value; } if (videoBitRate.HasValue) { options.VideoBitRate = videoBitRate.Value; } if (videoFps.HasValue) { options.VideoFps = videoFps.Value; } if (videoSize.HasValue) { options.VideoSize = videoSize.Value; } if (customWidth.HasValue) { options.CustomWidth = customWidth.Value; } if (customHeight.HasValue) { options.CustomHeight = customHeight.Value; } if (sourceCrop != null) { options.SourceCrop = sourceCrop; } if (baselineProfile.HasValue) { options.BaselineProfile = baselineProfile.Value; } await ffmpeg.ConvertAsync(inputFile, outputFile, options); }