예제 #1
0
 public function image_cache($model, $field, $id = null, $configuration = array(), $user_vars = array())
 {
     $configuration = array_merge(array('width' => 100, 'height' => 100, 'mode' => 1, 'format' => 'jpg', 'path_format' => '#cache/#model/#field/#id.#width.#height.#mode.#format', 'default' => ''), $configuration);
     $vars = array_merge(array('cache' => RA_CACHE_PATH, 'model' => $model, 'field' => $field, 'id' => $id, 'width' => $configuration['width'], 'height' => $configuration['height'], 'mode' => $configuration['mode'], 'format' => $configuration['format']), $user_vars);
     $path = Ra_StringHelper::simple_template($configuration['path_format'], $vars);
     $object = is_a($model, 'ActiveRecord') ? $model : ActiveRecord::model($model)->find($id);
     if ($object->{$field}) {
         if (file_exists($path) && file_exists($object->{$field}) && filemtime($object->{$field}) > filemtime($path)) {
             unlink($path);
         }
         if (!file_exists($path)) {
             if ($object->{$field}) {
                 Ra_DirectoryHelper::mkdir($path, true);
                 $image = new Ra_Image($object->{$field});
                 $image->resize($configuration['width'], $configuration['height'], $configuration['mode']);
                 $image->save($path);
             }
         }
     } else {
         $this->redirect_to('public/' . $configuration['default']);
     }
     return file_get_contents($path);
 }
예제 #2
0
 /**
  * Configure a field to accept one image file and automatic resize
  *
  * In the parameters you should pass one hash containing the name of resize (the key of hash)
  * and the resize configuration (the value)
  *
  * Resize configuration:
  * Its just a simple string following "WIDTHxHEIGHTxMODE", where:
  *  - WIDTH: the width of resized image
  *  - HEIGHT: the height of resized image
  *  - MODE: the resize mode, see Ra_Image library for a full description about
  *          resize modes
  * You can pass zero (0) for width OR height, this way the library will calculate the proportional
  * size.
  * You can use null to get a original version of image
  *
  * Example:
  *   $this->field_as_image("my_image_field", array("default" => "300x0x0", "thumbnail" => "30x30x1", "original" => null));
  */
 private static function _set_image($object, $field, $value, $configuration = array())
 {
     if (!$value['tmp_name']) {
         return $object->{$field};
     }
     $dir = RA_PUBLIC_PATH . '/uploads/';
     if (!is_dir($dir)) {
         mkdir($dir);
     }
     $new_name = self::normalize_filename($value['name']);
     $rel_path = self::create_uniq_path($dir, pathinfo($new_name, PATHINFO_FILENAME));
     $new_path = $dir . $rel_path;
     $ext = pathinfo($new_name, PATHINFO_EXTENSION);
     foreach ($configuration as $key => $config) {
         try {
             if ($config) {
                 //get info
                 list($width, $height, $mode) = explode("x", $config);
                 //resize
                 $image = new Ra_Image($value['tmp_name']);
                 $image->resize((int) $width, (int) $height, (int) $mode);
                 $image->save("{$new_path}.{$key}.{$ext}");
                 $image->destroy();
             } else {
                 $image = new Ra_Image($value['tmp_name']);
                 $image->save("{$new_path}.{$key}.{$ext}");
                 $image->destroy();
             }
             //remove previous file
             $old_path = $dir . self::parse_image_path($object->{$field}, $key);
             if (is_file($old_path)) {
                 unlink($old_path);
             }
         } catch (Exception $e) {
             throw $e;
             return $object->{$field};
         }
     }
     return $rel_path . ".*." . $ext;
 }