示例#1
0
文件: exif.php 项目: anqqa/Anqh
 /**
  * Create new exif model with exif data
  *
  * @param   string  $filename
  * @param   int     $image_id of parent image
  * @return  Exif_Model
  */
 public static function factory($filename = false, $image_id = false)
 {
     $exif_model = new Exif_Model();
     if ($filename) {
         // read exif data from file
         $exif_data = Exif::factory($filename)->read();
         if (!empty($exif_data)) {
             // set image_id if given
             if ($image_id) {
                 $exif_data['image_id'] = (int) $image_id;
             }
             // validate
             if ($exif_model->validate($exif_data)) {
                 // save our model only if image_id was given
                 if ($exif_model->image_id) {
                     $exif_model->save();
                 }
             }
         }
     }
     return $exif_model;
 }
示例#2
0
文件: image.php 项目: anqqa/Anqh
 /**
  * Create view and thumb images with magic
  *
  * @param   mixed   $config  config key or array
  * @param   string  $upload  filename
  * @param   int     $author_id
  * @param   string  $description
  * @return  Image_Model
  */
 public static function factory($config, $upload, $author_id = null, $description = null)
 {
     // does the image actually exists
     if (!file_exists($upload['tmp_name'])) {
         return null;
     }
     // load from config array or config file
     $config = is_array($config) ? $config : Kohana::config($config);
     $sizes = $config['sizes'];
     $format = $config['format'];
     // get image info
     $image_info = getimagesize($upload['tmp_name']);
     $extension = empty($format) || is_bool($format) ? Image::$allowed_types[$image_info[2]] : $format;
     // create Image model
     $image_model = new Image_Model();
     $image_model->format = $extension;
     $image_model->original_size = $upload['size'];
     $image_model->original_width = $image_info[0];
     $image_model->original_height = $image_info[1];
     if ($author_id) {
         $image_model->author_id = (int) $author_id;
     }
     if ($description) {
         $image_model->description = trim($description);
     }
     $image_model->save();
     // use the image model id as our base filename
     $filename = $image_model->id;
     $path = DOCROOT . 'images/' . url::id2path($filename) . '/';
     $original = upload::save($upload, $filename . '.' . $extension, $path);
     // create exif model
     $exif_model = Exif_Model::factory($original, $image_model->id);
     // create image sizes
     if (!empty($sizes)) {
         foreach ($sizes as $type => $size) {
             // create new image if needed
             if (!isset($image)) {
                 $image = new Image($original);
             }
             // resize image is larger than current size
             if ($image->width > $size['width'] || $image->height > $size['height']) {
                 $image->resize($size['width'], $size['height']);
             }
             // image filename is based on the type
             $size_filename = $path . $filename . '_' . ($type == 'normal' ? 'n' : 't') . '.' . $extension;
             $image->save($size_filename);
             $image_info = getimagesize($size_filename);
             switch ($type) {
                 case 'normal':
                     $image_model->width = $image_info[0];
                     $image_model->height = $image_info[1];
                     break;
                 case 'thumb':
                     $image_model->thumb_width = $image_info[0];
                     $image_model->thumb_height = $image_info[1];
                     break;
             }
         }
     }
     // save image size changes
     $image_model->save();
     return $image_model;
 }