public void UpdatePhotoType(PhotoType type)
 {
     _photoRepo.UpdatePhotoType(type);
     _baseService.Cacher.Remove("ALL.PHOTO.TYPES");
 }
 public int InsertPhotoType(PhotoType type)
 {
     _baseService.Cacher.Remove("ALL.PHOTO.TYPES");
     return _photoRepo.InsertPhotoType(type);
 }
 public bool PhotoTypeExist(Photo photo, PhotoType photoType)
 {
     return photo.PhotoTypes.Any(tf => tf.PhotoTypeId == photoType.PhotoTypeId);
 }
        /*

        public void Delete(Photo photo)
        {
            _photos.Collection.Remove(Query.EQ("_id", 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 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)
            {
                _photoRepo.UpdatePhoto(photo);
                _baseService.Cacher.RemoveAll(AllCacheKeys(photo));
            }

            return photo;
        }
        /// <summary>
        /// Will create specific photo type and returns it's path as "{0}/{1}", photoType.Directory, photo.FileName
        /// </summary>
        /// <param name="photo"></param>
        /// <param name="photoType"></param>
        /// <returns>Path as "{0}/{1}", photoType.Directory, photo.FileName</returns>
        public string CreateTypeOfPhoto(Photo photo, PhotoType photoType)
        {
            if (photoType.SystemName.ToLower() != "orig")
            {
                var origType = _photoRepo.GetBySystemName("orig");
                try
                {
                    ImageProcessingService.ResizeImage(photo, origType, photoType);
                    AddPhotoTypeToPhoto(photo, photoType);
                }
                catch
                {
                    throw;
                }
            }

            return string.Format("{0}/{1}", photoType.Directory, photo.FileName);
        }
 public int InsertPhotoType(PhotoType type)
 {
     return Insert(type);
 }
 public void UpdatePhotoType(PhotoType type)
 {
     Update(type);
 }
        public 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}/{3}", photo.BasePhotoVirtualPath, photo.Owner.OwnerDirectory, typeFolder, photo.FileName));
            }

            throw new Exception(string.Format("Original photo not defined for Photo Id: {0}!", photo.PhotoId));
        }
        public ActionResult PhotoTypeEdit(PhotoTypeEdit ptEdit)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var photoType = new PhotoType
                        {
                            Name = ptEdit.Name, Directory = ptEdit.Directory, SystemName = ptEdit.SystemName, X = ptEdit.X
                        };
                    if (ptEdit.Y.HasValue)
                    {
                        photoType.Y = ptEdit.Y.Value;
                    }

                    if (ptEdit.PhotoTypeId.HasValue)
                    {
                        photoType.PhotoTypeId = ptEdit.PhotoTypeId.Value;
                        PhotoService.UpdatePhotoType(photoType);
                        ptEdit.AddOKMessage("Update typu proběhl úspěšně.");
                    }
                    else //Insert
                    {
                        int newId = PhotoService.InsertPhotoType(photoType);
                        ptEdit.AddOKMessage("Uložení nového typu proběhlo úspěšně, nové ID je {0}".Fmt(newId));
                    }
                }
                catch (Exception ex)
                {
                    ptEdit.AddErrorMessage("Při ukládání typu fotky došlo k chybě: " + ex.Message);
                }
            }
            else
            {
                ptEdit.AddErrorMessage("Některá povinná položka není vyplněná.");
            }

            return View(ptEdit);
        }
 public int[] GetImageDimensions(Photo photo, PhotoType photoType)
 {
     var fullPath = GetPhotoFullPath(photo, photoType);
     return GetImageDimensions(fullPath);
 }
        private void SaveImage(Bitmap thumbImage, Photo photo, PhotoType targetType)
        {
            var targetTypeDirectory = HttpContext.Current.Server.MapPath(string.Format(@"{0}\{1}\{2}", photo.BasePhotoVirtualPath, photo.Owner.OwnerDirectory, targetType.Directory));

            SaveImage(thumbImage, targetTypeDirectory, photo.FileName);
        }
        private 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;

            //TODO: tady je chyba!!!!!! + tahle metoda se vola divne moc casto ???

            if (targetType.Y.HasValue) //mam zadanou vysku
            {
                resY = targetType.Y.Value;

                if (resX == resY) //ctvercova transformace
                {
                    return MakeSquareTransfrom(resX, resY, origImage);
                }
            }

            //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 void ResizeImage(Photo photo, PhotoType fromType, PhotoType targetType, string savePath)
 {
     using (var thumb = DoResizeImage(photo, fromType, targetType))
     {
         if (string.IsNullOrEmpty(savePath))
         {
             SaveImage(thumb, photo, targetType);
         }
         else
         {
             SaveImage(thumb, savePath, photo.FileName);
         }
     }
 }
        public void ResizeImage(Photo photo, PhotoType fromType, PhotoType targetType)
        {
            if(PhotoTypeExist(photo, targetType))
            {
                var photoTypeFileName = GetPhotoFullPath(photo, targetType); //photo.GetPhotoUrl(targetType.SystemName);

                if(File.Exists(photoTypeFileName))
                {
                    return;
                }
            }

            ResizeImage(photo, fromType, targetType, null);
        }