Create a image from file

@param string 	$file
@param string 	$type		jpg|png|gif

@return CCImage|false
351848
public static function create( $file, $type = null ) 
	{
		// when no type is given use the file extension
		if ( is_null( $type ) ) 
		{
			$type = CCStr::extension( $file );

			// validate type
			if ( !in_array( $type, static::$available_image_types ) )
			{
				$type = null;
			}
		}

		$image_data = getimagesize( $file );

		if ( $image_data === false ) 
		{
			return false;
		}

		$image = null;

		switch( $image_data['mime'] ) 
		{
			case 'image/gif':
				$image = imagecreatefromgif( $file );
			break;

			case 'image/jpeg';
				$image = imagecreatefromjpeg( $file );
			break;

			case 'image/png':
				$image = imagecreatefrompng( $file );
			break;

			default:
				// we dont support other image types
				return false;
			break;
		}

		// when the image type is still null we are going to use 
		// the mime type of the image 
		if ( is_null( $type ) )
		{
			$type = CCStr::suffix( $image_data['mime'], '/' );
		}

		return new static( $image, $type );
	}
970727
private function _saveBBoxRecord($file, $type)
    {
        fwrite($file, Util::packDouble(
            isset($this->boundingBox[$type]) ? $this->boundingBox[$type] : 0
        ));
    }
123171
public function upload( $file , $destination , $type = null){

        $name = $file->getClientOriginalName();
        $ext  = $file->getClientOriginalExtension();

        // Get the name first because after moving, the file doesn't exist anymore
        $new  = $file->move($destination,$name);
        $size = $new->getSize();
        $mime = $new->getMimeType();
        $path = $new->getRealPath();

        $image_name = basename($name,'.'.$ext);

        //resize
        if($type)
        {
            // resize if we have to
            $sizes = config('size.'.$type);
            $this->resize( $path, $image_name, $sizes['width'], $sizes['height']);
        }

        return array('name' => $name ,'ext' => $ext ,'size' => $size ,'mime' => $mime ,'path' => $path  );

	}
651129
public function load($file, $type = null)
    {
        $path   = $this->locator->locate($file);
        $config = array();

        $result = parse_ini_file($path, true);
        if (false === $result || array() === $result) {
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $file));
        }

        if (isset($result['parameters']) && is_array($result['parameters'])) {
            $config['parameters'] = array();
            foreach ($result['parameters'] as $key => $value) {
                $config['parameters'][$key] = $value;
            }
        }
    }
310446
public function create_perm_by_real($file, $type='file'){
    if ( !empty($file) &&
      \defined('BBN_APP_PATH') &&
      file_exists($file) &&
      // It must be a controller
      (strpos($file, '/mvc/public/') !== false)
    ){
      $is_file = $type === 'file';
      // Check if it's an external route
      foreach ( $this->routes as $i => $r ){
        if ( strpos($file, $r) === 0 ){
          // Remove route
          $f = substr($file, \strlen($r), \strlen($file));
          // Remove /mvc/public
          $f = substr($f, \strlen('/mvc/public'), \strlen($f));
          // Add the route's name to path
          $f = $i . $f;
        }
      }
      // Internal route
      if ( empty($f) ){
        $root_path = BBN_APP_PATH.'mvc/public/';
        if ( strpos($file, $root_path) === 0 ){
          // Remove root path
          $f = substr($file, \strlen($root_path), \strlen($file));
        }
      }
      if ( !empty($f) ){
        $bits = bbn\x::remove_empty(explode('/', $f));
        $code = $is_file ? bbn\str::file_ext(array_pop($bits), 1)[0] : array_pop($bits).'/';
        $id_parent = $this->options->from_code('page', 'bbn_permissions');
        foreach ( $bits as $b ){
          if ( !$this->options->from_code($b.'/', $id_parent) ){
            $this->options->add([
              'id_parent' => $id_parent,
              'code' => $b.'/',
              'text' => $b
            ]);
          }
          $id_parent = $this->options->from_code($b.'/', $id_parent);
        }
        if ( !$this->options->from_code($code, $id_parent) ){
          $this->options->add([
            'id_parent' => $id_parent,
            'code' => $code,
            'text' => $code
          ]);
        }
        return $this->options->from_code($code, $id_parent);
      }
      else if ( !$is_file ){
        return $this->options->from_code('page', 'bbn_permissions');
      }
      return true;
    }
    return false;
  }
484374
public function writeImage( $image, $file, $type) {
		switch ($type) {
			case 2:	//JPEG
			Imagejpeg( $image, $file);
			break;
			
			case 3:	//PNG
			Imagepng( $image, $file);
			break;
			
			default:
			$this->errorMsg="File format not supported.";
		}
	}
857630
public function generate($file = null) {

		if(null !== $file && false === is_string($file)) {
            return trigger_error(sprintf('Argument 1 passed to %s() must be of the type string, "%s" given', __METHOD__, gettype($file)), E_USER_ERROR);
        }

		if(null === $this -> image) {
			return trigger_error('Can not generate captcha image without valid background image. Set background image with setImage() method', E_USER_ERROR);
		}

		if(null === $this -> font) {
			return trigger_error('Can not generate captcha image without valid font. Set font with setFont() method', E_USER_ERROR);
		}

		//Generate and set text if not already set
		if(null === $this -> getText()) {
			$this -> setText($this -> generateText());
		}

		$text 	= $this -> getText();
		$image 	= $this -> image -> entity();
		$font 	= $this -> font -> entity();

		//Add image header
		if(null === $file) {
			Response :: addHeader('Content-type', $this -> image -> getMime());
		}
		else {
			
			$file = new File($file);

			if(false === in_array($file -> entity() -> getExtension(), ['png', 'jpg', 'jpeg'])) {
				return trigger_error(sprintf('Argument 1 passed to %s() must have the jpg, jpeg or png extension, "%s" given', __METHOD__, $file -> entity() -> getExtension()), E_USER_ERROR);
			}
		}

		//Create image
		switch(strtolower($image -> getExtension())) {
			
			case 'png' 	: $img = imagecreatefrompng($image -> getBasepath()); break;
			default 	: $img = imagecreatefromjpeg($image -> getBasepath());
		}

		//Generate output object
		$output = [];

		for($i = 0; $i < strlen($text); $i++) {

			$fontsize 	= rand($this -> fontsize['min'], $this -> fontsize['max']);
			$angle 		= rand($this -> angle['min'], $this -> angle['max']);
			$tb 		= imagettfbbox($fontsize, $angle, $font -> getBasepath(), $text[$i]);
			$color 		= imagecolorallocate($img, $this -> color['r'], $this -> color['g'], $this -> color['b']);
			$width 		= $image -> getWidth() / strlen($text);
			$width 		= $width < 2 ? 3 : $width;
			$tries 		= 0;

			if($tb[2] >= $width && $tries < 20) {

				$this -> fontsize['min']--;
				$i--;
				$tries++;

				continue;
			}

			$output[] 	= [

				'text' 		=> $text[$i], 
				'width' 	=> $tb[2], 
				'height' 	=> $tb[1],
				'angle'		=> $angle,
				'fontsize' 	=> $fontsize, 
				'color' 	=> $color
			];
		}

		foreach($output as $index => $char) {

			$x = $width * $index + rand(0, $width - $char['width']);
			$y = rand($char['fontsize'], $image -> getHeight() - $char['height']);
			
			imagettftext($img, $char['fontsize'], $char['angle'], $x, $y, $char['color'], $font -> getBasepath(), $char['text']);
		}
		
		$extension 	= $file !== null ? $file -> entity() -> getExtension() : $image -> getExtension();
		$file 		= $file !== null ? $file -> entity() -> getBasepath() : null;

		//Show image
		switch(strtolower($extension)) {
			
			case 'png' 	: imagepng($img, $file); break;
			default 	: imagejpeg($img, $file);
		}
	}
985975
public function file($file, $type, $filename = null)
    {
        $this->type = $type;

        if(is_file($file))
        {
            $this->payload['file'] = ['name' => $type, 'contents'=> fopen($file, 'r')];
            if($filename)
                $this->payload['file']['filename'] = $filename;
        }
        else
            $this->payload[$type] = $file;

        return $this;
    }
226888
public function doFilter($file, $arguments, $typeOptions)
    {
        if (! $arguments['grayscale']) {
            return;
        }

        $Imagine = new Imagine();
        $image = $Imagine->open($file);
        $image->effects()->grayscale();
        $image->save($file);
    }
1180084
final protected function canLoadAsset($file, $type) {
        $filename = basename($file);
        if (in_array($filename, $this->loadedAssets[$type]))
            return false;

        $this->loadedAssets[$type][] = $filename;
        return true;
    }
24081
public static function getInstance($type = 'file', $file = 'logger.log', $dir = null)
    {
        if (is_null($dir)) {
            $dir = date('Ymd');
        }
        $config = [
            'type' => $type,
            'file' => $file,
            'dir' => $dir,
        ];
        $key = md5(json_encode($config));

        if (empty(static::$_instance[$key]) || !(static::$_instance[$key] instanceof Logger)) {
            static::$_instance[$key] = new Logger($type, $file, $dir);
        }
        return static::$_instance[$key];
    }
859083
public static function open($file)
    {
        if (is_string($file)) {
            $file = new \SplFileInfo($file);
        }
        if (!$file->isFile()) {
            throw new ImageException('image file not exist');
        }
        return new self($file);
    }
750843
private function open_image( $file )
	{
		$sfile = new SFile($file);

		// If $file isn't an array, we'll turn it into one
		if ( !is_array($file) ) {
			$file = array(
				'type'		=> $sfile->getMimeType(),
				'tmp_name'	=> $file
			);
		}

		$mime = $file['type'];
		$file_path = $file['tmp_name'];

		switch ( $mime )
		{
			case 'image/pjpeg': // IE6
			case 'image/jpeg':	$img = @imagecreatefromjpeg( $file_path );	break;
			case 'image/gif':	$img = @imagecreatefromgif( $file_path );	break;
			case 'image/png':	$img = @imagecreatefrompng( $file_path );	break;
			default:				$img = false;								break;
		}

		return $img;
	}
779328
private function getFile($file, $type = 0)
    {
        $type == 1 && $file = $this->initBaseDir($file);//初始化路径
        //$file = str_replace([('/', '\\'], DIRECTORY_SEPARATOR, $file);
        $cacheFile = $this->getCacheFile($file);
        if ($this->options['autoUpdate']) {
            $tplFile = $this->getTplFile($file);
            if (!is_readable($tplFile)) {
                throw new FileCanNotReadableException(Lang::get('_TEMPLATE_FILE_NOT_FOUND_', $tplFile));
            }
            if (!is_file($cacheFile)) {
                if ($type !== 1 && !is_null($this->layout)) {
                    if (!is_readable($this->layout)) {
                        throw new FileCanNotReadableException(Lang::get('_TEMPLATE_FILE_NOT_FOUND_', $this->layout));
                    }
                }
                $this->compile($tplFile, $cacheFile, $type);
                return $cacheFile;
            }

            $compile = false;
            $tplMtime = filemtime($tplFile);
            $cacheMtime = filemtime($cacheFile);
            if ($cacheMtime && $tplMtime) {
                ($cacheMtime < $tplMtime) && $compile = true;
            } else {//获取mtime失败
                $compile = true;
            }

            if ($compile && $type !== 1 && !is_null($this->layout)) {
                if (!is_readable($this->layout)) {
                    throw new FileCanNotReadableException(Lang::get('_TEMPLATE_FILE_NOT_FOUND_', $this->layout));
                }
            }

            //当子模板未修改时判断布局模板是否修改
            if (!$compile && $type !== 1 && !is_null($this->layout)) {
                if (!is_readable($this->layout)) {
                    throw new FileCanNotReadableException(Lang::get('_TEMPLATE_FILE_NOT_FOUND_', $this->layout));
                }
                $layoutMTime = filemtime($this->layout);
                if ($layoutMTime) {
                    $cacheMtime < $layoutMTime && $compile = true;
                } else {
                    $compile = true;
                }
            }

            $compile && $this->compile($tplFile, $cacheFile, $type);
        }
        return $cacheFile;
    }
212748
public function get($type, $file = null)
    {
        if (!isset($this->paths[$type])) {
            $method = 'get'.Utils::camelize($type).'Dir';

            if (!method_exists($this, $method)) {
                return null;
            }

            $this->set($type, $this->$method());
        }

        $path = $this->paths[$type];
        if ($file) {
            if ($path instanceof pathmanager\FallbackInterface) {
                $path = $path->resolve($file);
            } else {
                $path .= '/' . ltrim($file, '/');
            }
        }

        return $path;
    }
559372
private function generateImages($type) {
        $imagable = \Yii::$app->shop_imagable;

        $nameFile = (new Security())->generateRandomString();
        $this->$type->saveAs($imagable->imagesPath . '/' . $nameFile . '.' . $this->$type->extension);
        $image_name = $imagable->create('shop-category/' . $type,
            $imagable->imagesPath . '/' . $nameFile . '.' . $this->$type->extension);
        unlink($imagable->imagesPath . '/' . $nameFile . '.' . $this->$type->extension);

        return $image_name;
    }
1269134
static protected function load($type, $file) {
    include $file;
    switch ($type) {
      case 'filters':
        self::$objects['filters'][] = $filter;
        break;
      case 'functions':
        self::$objects['functions'][] = $function;
        break;
      case 'tags':
        if (preg_match('/^([^\.]+)\.tag\.php$/', basename($file), $matches)) {
          $class = "Project_{$matches[1]}_TokenParser";
          if (class_exists($class)) {
            self::$objects['parsers'][] = new $class();
          }
        }
        break;
    }
  }