Beispiel #1
0
 public function resolve()
 {
     $hash = $this->name;
     if (!is_null($thumb = \System\Cache\Thumb::from_hash($hash))) {
         if ($thumb->check()) {
             $this->file_point = \System\Image::from_path(BASE_DIR . $thumb->get_path());
             $this->file_path = $this->file_point->get_path();
             $this->exists = $this->file_point->exists();
         } else {
             throw new \System\Error\File('Failed to generate image thumb.');
         }
     } else {
         $this->exists = false;
     }
 }
Beispiel #2
0
 public function val_get()
 {
     if ($this->resolved_value) {
         $val = $this->resolved_value;
     } else {
         $val = $this->form()->input_value($this->name);
     }
     if (gettype($val) == 'string') {
         $val = \System\Json::decode($val);
     }
     if (is_array($val)) {
         if ($val['method'] == 'url') {
             $val = \System\Image::fetch($val['url'])->temp()->unload();
             $val->temp = false;
             $val->method = 'keep';
         } else {
             if ($val['method'] == 'upload') {
                 $file = $this->form()->request->post($val['upload']);
                 $file = \System\Image::from_tmp($file['tmp_name'], $val['name']);
                 $file->temp();
                 $file->temp = false;
                 $file->method = 'keep';
                 $file->mime = $val['mime'];
                 $val = $file;
             } else {
                 if ($val['method'] == 'save') {
                     $file = \System\Image::from_path($val['path']);
                     $file->read_meta();
                     $val = $file;
                 } else {
                     if ($val['method'] == 'drop') {
                         $val = null;
                     }
                 }
             }
         }
     }
     $this->resolved_value = $val;
     return $val;
 }
Beispiel #3
0
 /** Create guest user
  * @return System\User
  */
 public static function guest()
 {
     return new self(array("user_id" => 0, "nick" => 'Anonymous', "image" => \System\Image::from_path("/share/pixmaps/pwf/anonymous_user.png")));
 }
Beispiel #4
0
 /** Get image GD resource
  * @param string $path   Path to image
  * @param int    $format GD format constant
  * @return resource
  */
 public static function get_gd_resource(self $img)
 {
     $im = null;
     switch ($img->format()) {
         case 1:
             $im = imagecreatefromgif($img->get_path());
             break;
         case 2:
             $im = imagecreatefromjpeg($img->get_path());
             break;
         case 3:
             $im = imagecreatefrompng($img->get_path());
             break;
     }
     if (!is_resource($im)) {
         throw new \System\Error\File(sprintf('Failed to open image "%s". File is not readable or format "%s" is not supported.', $path, self::get_suffix($format)));
     }
     return $im;
 }
Beispiel #5
0
 /** Generate thumb using GD library
  * @param self $obj         Instance of image
  * @return bool
  */
 public static function gen_gd(self $obj)
 {
     $obj->image->refresh_info();
     $coords = array('w_new' => $obj->width, 'h_new' => $obj->height, 'w_org' => intval($obj->image->width()), 'h_org' => intval($obj->image->height()));
     $tpth = BASE_DIR . $obj->get_path();
     \System\Directory::check(dirname($tpth));
     if ($coords['w_new'] < $coords['w_org'] || $coords['h_new'] < $coords['h_org']) {
         $coords = self::calc_thumb_coords($coords, $obj->crop);
         $im = \System\Image::get_gd_resource($obj->image);
         $th = imagecreatetruecolor($coords['w_new'], $coords['h_new']);
         $trans = $obj->image->format() == 3;
         if ($trans) {
             $transparent = imagecolorallocatealpha($th, 0, 0, 0, 127);
             imagefill($th, 0, 0, $transparent);
         } else {
             $wh = imagecolorallocate($th, 255, 255, 255);
             imagefill($th, 0, 0, $wh);
         }
         imagecopyresampled($th, $im, intval($coords['dst_x']), intval($coords['dst_y']), 0, 0, intval($coords['xw']), intval($coords['xh']), $coords['w_org'], $coords['h_org']);
         if (file_exists($tpth)) {
             unlink($tpth);
         }
         if ($trans) {
             imagealphablending($th, false);
             imagesavealpha($th, true);
             imagepng($th, $tpth);
         } else {
             imagejpeg($th, $tpth, 99);
         }
         imagedestroy($th);
     } else {
         $obj->image->copy($tpth);
     }
     return $obj;
 }
<?php

$items = \Impro\Event::get_all()->fetch();
$attrs = array('image');
foreach ($items as $item) {
    foreach ($attrs as $attr) {
        if ($item->{$attr}) {
            $opts = $item->{$attr}->get_opts();
            if (empty($opts)) {
                $avatar = $item->{$attr};
                if (strpos($item->{$attr}->path, '/') === 0) {
                    $item->{$attr}->path = BASE_DIR . $item->{$attr}->path;
                }
            } else {
                if (any($opts['file_path'])) {
                    $avatar = \System\Image::from_path($opts['file_path']);
                    if ($avatar) {
                        $avatar->keep = true;
                    }
                } else {
                    $avatar = null;
                }
            }
            if (!is_null($avatar)) {
                try {
                    $avatar->save();
                } catch (Exception $e) {
                    $avatar = null;
                }
            }
            $item->{$attr} = $avatar;
Beispiel #7
0
 public static function get_schema()
 {
     static::check_model();
     $attrs = array();
     $schema = array();
     $cname = get_called_class();
     $list = static::get_attr_list();
     foreach ($list as $name) {
         if ($name == static::get_id_col()) {
             continue;
         }
         $attr = static::get_attr($name);
         $attr['name'] = $name;
         switch ($attr['type']) {
             case 'bool':
                 $attr['type'] = 'boolean';
                 break;
             case 'varchar':
                 $attr['type'] = 'string';
                 break;
             case 'json':
                 $attr['type'] = 'object';
                 break;
             case self::REL_HAS_ONE:
                 $nm = $attr['model'];
                 $attr['type'] = 'model';
                 $attr['subtype'] = 'has_one';
                 $attr['bound_to'] = static::get_rel_bound_to($name);
                 break;
             case self::REL_BELONGS_TO:
                 $attr['type'] = 'model';
                 break;
             case self::REL_HAS_MANY:
                 $rm = $attr['model'];
                 if (empty($attr['is_bilinear'])) {
                     $attr['bound_to'] = static::get_rel_bound_to($name);
                     $attr['foreign_key'] = $rm::get_belongs_to_id($attr['bound_to']);
                 } else {
                     $attr['foreign_key'] = static::get_id_col();
                 }
                 $attr['type'] = 'collection';
                 break;
         }
         // Convert attribute model bindings
         if (isset($attr['model'])) {
             $attr['model'] = \System\Loader::get_model_from_class($attr['model']);
         }
         // Convert attribute value options
         if (isset($attr['options'])) {
             if (isset($attr['options'][0]) && $attr['options'][0] == 'callback') {
                 $opts = $attr['options'];
                 array_shift($opts);
                 $opts = call_user_func($opts);
             } else {
                 $opts = $attr['options'];
             }
             $attr['options'] = array();
             foreach ($opts as $opt_value => $opt_name) {
                 $attr['options'][] = array('name' => $opt_name, 'value' => $opt_value);
             }
         }
         // Word 'default' is keyword in some browsers, so pwf-models use 'def' instead
         if (isset($attr['default'])) {
             $attr['def'] = $attr['default'];
             unset($attr['default']);
             if (in_array($attr['type'], array('image'))) {
                 $attr['def'] = \System\Image::from_path($attr['def'])->to_object();
             } else {
                 if (in_array($attr['type'], array('file', 'sound'))) {
                     $attr['def'] = \System\File::from_path($attr['def'])->to_object();
                 }
             }
         }
         if (is_array($attr)) {
             unset($attr[0]);
             $attrs[] = $attr;
         }
     }
     if (any($cname::$conversions)) {
         $schema['conversions'] = $cname::$conversions;
     }
     $schema['attrs'] = $attrs;
     return $schema;
 }