public Photo AddPhotoTypeToPhoto(Photo photo, PhotoType photoType)
        {
            var doSave = false;

            if(photo.PhotoTypes == null)
            {
                photo.PhotoTypes = new List<PhotoType>{photoType};
                doSave = true;
            }
            else
            {
                if(photo.PhotoTypes.All(p => p.SystemName.ToLower() != photoType.SystemName.ToLower()))
                {
                    photo.PhotoTypes.Add(photoType);
                    doSave = true;
                }
            }

            if(doSave)
            {
                Save(photo);
            }

            return photo;
        }
        public static void ResizeImage(Photo photo, PhotoType fromType, PhotoType targetType)
        {
            if(PhotoManager.PhotoTypeExist(photo, targetType))
            {
                //TODO: double check photoType really exists on disk.
                return;
            }

            ResizeImage(photo, fromType, targetType, null);
        }
        public static string GetPhotoFullPath(Photo photo, PhotoType photoType)
        {
            if (photo == null || photoType == null) { throw new ArgumentException("Missing Photo or PhotoType"); }

            if (photo.PhotoTypes != null && photo.PhotoTypes.Any(f => f.SystemName.ToLower() == photoType.SystemName.ToLower()))
            {
                var typeFolder = photo.PhotoTypes.First(f => f.SystemName.ToLower() == photoType.SystemName.ToLower()).Directory;
                return HttpContext.Current.Server.MapPath(string.Format("{0}/{1}/{2}", photo.BasePhotoVirtualPath, typeFolder, photo.FileName));
            }

            throw new Exception(string.Format("Original photo not defined for Photo Id: {0}!", photo.Id));
        }
        public Photo CreateFotka(string ownerId, string fullFileName, DateTime dateUploaded)
        {
            string photoSystemName;

            try
            {
                photoSystemName = RenameToSystemFileName(fullFileName);
            }
            catch(Exception ex)
            {
                throw;
            }

            var photoToSave = new Photo {FileName = photoSystemName, OwnerId = new ObjectId(ownerId), DateUploaded = dateUploaded};
            Save(photoToSave);

            return GetPhoto(photoToSave.Id);
        }
        public static void ResizeImage(Photo photo, PhotoType fromType, PhotoType targetType, string savePath)
        {
            var thumb = DoResizeImage(photo, fromType, targetType);

            if(string.IsNullOrEmpty(savePath))
            {
                SaveImage(thumb, photo, targetType);
            }
            else
            {
                SaveImage(thumb, savePath, photo.FileName);
            }
        }
        private static void SaveImage(Bitmap thumbImage, Photo photo, PhotoType targetType)
        {
            var targetTypeDirectory = HttpContext.Current.Server.MapPath(string.Format(@"{0}\{1}", photo.BasePhotoVirtualPath, targetType.Directory));

            SaveImage(thumbImage, targetTypeDirectory, photo.FileName);
        }
 public static int[] GetImageDimensions(Photo photo, PhotoType photoType)
 {
     var fullPath = GetPhotoFullPath(photo, photoType);
     return GetImageDimensions(fullPath);
 }
        private static Bitmap DoResizeImage(Photo photo, PhotoType fromType, PhotoType targetType)
        {
            var fullPath = GetPhotoFullPath(photo, fromType);
            var origImage = LoadImage(fullPath);

            int resX = targetType.X;
            int resY;
            float ratio;

            //obrazek na sirku nebo ctverec:
            if (origImage.Width >= origImage.Height)
            {
                ratio = (float)origImage.Height / (float)origImage.Width;
                resY = Convert.ToInt32(resX * ratio);
            }
            else //obrazek na vysku.
            {
                if(targetType.Y.HasValue) //mam zadanou vysku
                {
                    resY = targetType.Y.Value;
                    ratio = (float)origImage.Width / (float)origImage.Height;
                    resX = Convert.ToInt32(resY * ratio);
                }
                else //pokud nemam zadanou vysku, musim to resizovat podle zadane sirky
                {
                    ratio = (float)origImage.Height / (float)origImage.Width;
                    resY = Convert.ToInt32(resX * ratio);
                }
            }

            var thumbImage = new Bitmap(resX, resY, origImage.PixelFormat);
            Graphics g = Graphics.FromImage(thumbImage);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            var oRectangle = new Rectangle(0, 0, resX, resY);
            g.DrawImage(origImage, oRectangle);

            if (resX < origImage.Width)
            {
                var si = new SharpeningImage();
                si.Filter(thumbImage);
            }

            origImage.Dispose();

            return thumbImage;
        }
 public GalleryPhoto(Photo photo, int order)
 {
     Id = photo.Id;
     OwnerId = photo.OwnerId;
     FileName = photo.FileName;
     DateUploaded = photo.DateUploaded;
     DateCreated = photo.DateCreated;
     Description = photo.Description;
     PhotoTypes = photo.PhotoTypes;
     Order = order;
 }
 public void Delete(Photo photo)
 {
     _photos.Collection.Remove(Query.EQ("_id", photo.Id));
 }
 public static bool PhotoTypeExist(Photo photo, PhotoType photoType)
 {
     return photo.PhotoTypes.Any(tf => tf.Id == photoType.Id);
 }
 public void Save(Photo photo)
 {
     _photos.Collection.Save(photo);
 }
        public Photo RemovePhotoTypeFromPhoto(Photo photo, PhotoType ptToBeRemoved)
        {
            if(photo.PhotoTypes != null)
            {
                if(photo.PhotoTypes.Any(pt => pt.Id == ptToBeRemoved.Id))
                {
                    photo.PhotoTypes.Remove(ptToBeRemoved);
                    Save(photo);
                }
            }

            return photo;
        }
        public List<OrigPhotosWaiting> GetWaitingPhotos(User user)
        {
            var retColl = new List<OrigPhotosWaiting>();
            var currentUserDir = user.UserNameSEO;
            var files = GetFilesInUploadDirectory(currentUserDir);
            if (files.Length > 0)
            {
                var typeManager = new PhotoTypeManager();
                var uploadTempType = typeManager.GetBySystemName("minithumb");
                var uploadType = typeManager.GetBySystemName("upload");

                for (int i = 0; i < files.Length; i++)
                {
                    var photoAlreadyInDb = GetPhotoByFileName(files[i].Name);

                    string fileName;
                    ObjectId id;
                    if (photoAlreadyInDb == null) //fotku sme jeste nezpracovavali
                    {
                        fileName = RenameToSystemFileName(files[i].FullName);
                        var fotka = new Photo { FileName = fileName, User = user, DateUploaded = files[i].CreationTime };

                        Save(fotka);
                        AddPhotoTypeToPhoto(fotka, uploadType);
                        try
                        {
                            ImageProcessingManager.ResizeImage(fotka, uploadType, uploadTempType);
                            AddPhotoTypeToPhoto(fotka, uploadTempType);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        id = fotka.Id;
                    }
                    else
                    {
                        fileName = photoAlreadyInDb.FileName;
                        id = photoAlreadyInDb.Id;
                    }

                    var photoWaiting = new OrigPhotosWaiting { FileName = fileName, UploadedDate = files[i].CreationTime, Id = id.ToString() };

                    //X,Y dimensions of originally uploaded photo - uncomment if you need to diplay it on ProcessUploadPhotos view.
                    /*
                    var origPath = HttpContext.Current.Server.MapPath(string.Format("/{0}/{1}/{2}/{3}", ConfigurationManager.AppSettings["GalleryRootDirVirtualPath"], currentUserDir, uploadType.Adresar, fileName));
                    var origDimensions = ImageProcessingManager.GetImageDimensions(origPath);
                    photoWaiting.X = origDimensions[0];
                    photoWaiting.Y = origDimensions[1];
                    */

                    photoWaiting.ThumbPath = string.Format("/{0}/{1}/{2}/{3}", ConfigurationManager.AppSettings["GalleryRootDirVirtualPath"], currentUserDir, uploadTempType.Directory, fileName);

                    retColl.Add(photoWaiting);
                }
            }
            return retColl;
        }