public function xapply(\Siciarek\PhotoGalleryBundle\Entity\File $file)
 {
     $success = false;
     $src_path = $this->config["uploads_directory"] . $file->getPath();
     if ($this->angle !== 0 and file_exists($src_path) and is_readable($src_path) and is_writable($src_path)) {
         $src_image = $this->imagecreatefrom($src_path, $file->getMimeType());
         $trg_image = imagerotate($src_image, $this->angle, 0);
         if ($trg_image !== false) {
             $success = $this->saveimage($trg_image, $file->getMimeType(), $src_path);
         }
         imagedestroy($src_image);
         imagedestroy($trg_image);
     }
     if ($success === false) {
         throw new \Exception("Image can not be modified.");
     }
     if ($this->angle !== 180) {
         $width = $file->getWidth();
         $height = $file->getHeight();
         $file->setWidth($height);
         $file->setHeight($width);
     }
     $file->setFileSize(filesize($src_path));
 }
 protected function createImages($files, $album, $images_visible = true, $seq_number = 0, $fileinfo = array())
 {
     $config = $this->container->getParameter("siciarek_photo_gallery.config");
     $sequence_number = $seq_number;
     if ($files === null) {
         return;
     }
     $index = 0;
     foreach ($files as $file) {
         $original_name = $file->getClientOriginalName();
         $fkey = $index++ . $original_name;
         $original_name = trim($original_name);
         $original_name = preg_replace("|([^/]+)\$|", "\$1", $original_name);
         $original_name = preg_replace("/\\.\\w+\$/", "", $original_name);
         $original_name = preg_replace("/_/", " ", $original_name);
         $original_name = preg_replace("/\\s+/", " ", $original_name);
         $original_name = trim($original_name);
         $original_name = empty($original_name) ? null : $original_name;
         $info = (is_array($fileinfo) and array_key_exists($fkey, $fileinfo)) ? $fileinfo[$fkey] : array();
         $title = array_key_exists("title", $info) ? $info["title"] : $original_name;
         $description = array_key_exists("description", $info) ? $info["description"] : null;
         $is_visible = array_key_exists("is_visible", $info) ? $info["is_visible"] : $images_visible;
         $album_id = array_key_exists("album_id", $info) ? $info["album_id"] : $album->getId();
         $source_path = $file->getPathName();
         // Initial values:
         $image_extension = "jpg";
         $thumbnail_width = 200;
         $thumbnail_height = 150;
         $thumbnail_mime_type = "image/jpg";
         $thumbnail_extension = "jpg";
         $image = new Image();
         $image->setTitle($title);
         $image->setDescription($description);
         $image->setIsVisible($is_visible);
         $image->setSequenceNumber(++$sequence_number);
         $image->setAlbum($album);
         $this->em->persist($image);
         $this->em->flush();
         $this->em->refresh($image);
         $thumbnail = new Image();
         $thumbnail->setIsVisible($image->getIsVisible());
         $thumbnail->setSequenceNumber(0);
         $this->em->persist($thumbnail);
         $image->setThumbnail($thumbnail);
         $this->em->persist($image);
         $this->em->flush();
         $origdir = sprintf("/%02d/originals", $album->getId());
         $imdir = sprintf("/%02d/images", $album->getId());
         $thdir = sprintf("/%02d/thumbnails", $album->getId());
         if (!file_exists($config["uploads_directory"] . $origdir)) {
             mkdir($config["uploads_directory"] . $origdir, 0777, true);
         }
         if (!file_exists($config["uploads_directory"] . $imdir)) {
             mkdir($config["uploads_directory"] . $imdir, 0777, true);
         }
         if (!file_exists($config["uploads_directory"] . $thdir)) {
             mkdir($config["uploads_directory"] . $thdir, 0777, true);
         }
         $origpath = sprintf("%s/%02d.%s", $origdir, $image->getId(), $image_extension);
         $impath = sprintf("%s/%02d.%s", $imdir, $image->getId(), $image_extension);
         $thumpath = sprintf("%s/%02d.%s", $thdir, $image->getId(), $thumbnail_extension);
         $original_path = $config["uploads_directory"] . $origpath;
         $image_path = $config["uploads_directory"] . $impath;
         $thumbnail_path = $config["uploads_directory"] . $thumpath;
         // Image file adjustment:
         $img_width = 640;
         $img_height = 480;
         $wide_img_width = 1000;
         $imagine = new \Imagine\Gd\Imagine();
         $im = $imagine->open($source_path)->save($original_path);
         $img_size = new \Imagine\Image\Box($img_width, $img_height);
         $wide_img_size = new \Imagine\Image\Box($wide_img_width, $img_height);
         $im = $im->thumbnail($img_size)->save($image_path);
         if ($im->getSize()->getHeight() < 200) {
             $im = $imagine->open($source_path)->save($original_path);
             $im = $im->thumbnail($wide_img_size)->save($image_path);
         }
         $image_file_size = filesize($image_path);
         $image_width = $im->getSize()->getWidth();
         $image_height = $im->getSize()->getHeight();
         $image_mime_type = $file->getClientMimeType();
         $imfile = new E\File();
         $imfile->setWidth($image_width);
         $imfile->setHeight($image_height);
         $imfile->setMimeType($image_mime_type);
         $imfile->setFileSize($image_file_size);
         $imfile->setPath($impath);
         $this->em->persist($imfile);
         $this->em->flush();
         $this->em->refresh($imfile);
         $image->setFile($imfile);
         // Thumbnail:
         $thmb_size = new \Imagine\Image\Box($thumbnail_width, $thumbnail_height);
         $th = $im->thumbnail($thmb_size)->save($thumbnail_path);
         $thumbnail_file_size = filesize($thumbnail_path);
         $thumbnail_width = $th->getSize()->getWidth();
         $thumbnail_height = $th->getSize()->getHeight();
         $thfile = new E\File();
         $thfile->setWidth($thumbnail_width);
         $thfile->setHeight($thumbnail_height);
         $thfile->setMimeType($thumbnail_mime_type);
         $thfile->setFileSize($thumbnail_file_size);
         $thfile->setPath($thumpath);
         $this->em->persist($thfile);
         $this->em->flush();
         $this->em->refresh($thfile);
         $thumbnail->setFile($thfile);
         $this->em->persist($thumbnail);
         $this->em->persist($image);
         $this->em->flush();
         // Add watermark:
         $this->watermark->apply($original_path);
         $this->watermark->apply($image_path);
         $imfile->setFileSize(filesize($image_path));
         $this->em->persist($image);
         $this->em->flush();
     }
     if ($album->getCover() === null) {
         $this->setAlbumCover($album);
     }
 }