Пример #1
0
 /**
  * Save file into filesystem and insert information into database.
  * Checks extension and gives file mime type.
  * @param FileUpload $file
  * @param int $goods_id
  * @param string $name
  * @param string $description
  * @return bool success
  */
 public function insertPhoto($file, $goods_id, $name = '', $description = '')
 {
     $photo = new Photo($this->db);
     $filename = $file->getName();
     $ext = self::getExtensionByName($filename);
     // povolene pripony
     $enabledExt = explode(' ', self::ENABLED_EXTENSION);
     if (!in_array($ext, $enabledExt)) {
         return false;
     }
     $lastPhoto_id = $photo->insert(array('name' => $name ? $name : $filename, 'goods_id' => $goods_id, 'description' => $description));
     $photoFileName = $file->getTemporaryFile();
     $photoFileNameBig = self::SAVE_DIR . $lastPhoto_id . '.big.jpg';
     $photoFileNameSmall = self::SAVE_DIR . $lastPhoto_id . '.small.jpg';
     try {
         $src = imagecreatefromjpeg($photoFileName);
         list($width, $height) = getimagesize($photoFileName);
         $aspectRatio = $width / $height;
         if ($aspectRatio > 1) {
             $targetWidth = 800;
             $targetHeight = round(800 / $aspectRatio);
         } else {
             $targetWidth = round(800 * $aspectRatio);
             $targetHeight = 800;
         }
         $bigThumbnail = imagecreatetruecolor($targetWidth, $targetHeight);
         imagecopyresampled($bigThumbnail, $src, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
         imagejpeg($bigThumbnail, $photoFileNameBig, 75);
         $smallThumbnail = imagecreatetruecolor(round($targetWidth / 2.5), round($targetHeight / 2.5));
         imagecopyresampled($smallThumbnail, $src, 0, 0, 0, 0, round($targetWidth / 2.5), round($targetHeight / 2.5), $width, $height);
         imagejpeg($smallThumbnail, $photoFileNameSmall, 50);
     } catch (\Exception $e) {
         $photo = new Photo($this->db);
         $photo->where('id', $lastPhoto_id)->delete();
         return false;
     }
     return true;
 }