/** * Get the Glide server instance that powers the image API */ public static function server() { if (!isset(static::$_server)) { $api = new Glide\Api\Api(\CMF\Image::manager(), array(new Glide\Manipulators\Orientation(), new \CMF\Glide\Manipulators\Crop(), new Glide\Manipulators\Size(2000 * 2000), new Glide\Manipulators\Brightness(), new Glide\Manipulators\Contrast(), new Glide\Manipulators\Gamma(), new Glide\Manipulators\Sharpen(), new Glide\Manipulators\Filter(), new Glide\Manipulators\Blur(), new Glide\Manipulators\Pixelate(), new Glide\Manipulators\Watermark(\CMF\Storage::adapter()), new Glide\Manipulators\Background(), new Glide\Manipulators\Border(), new Glide\Manipulators\Encode())); static::$_server = new Glide\Server(\CMF\Storage::adapter(), \CMF\Storage::adapter(APPPATH . 'cache/image'), $api); } return static::$_server; }
protected function outputImage($path, $params) { try { // Output the image using glide server ob_clean(); \CMF\Image::server()->outputImage($path, $this->getImageParams($params)); } catch (\Exception $e) { // Output a placeholder - exception could mean file doesn't exist, or some other environment issue if (!\CMF\Image::server()->getSource()->has('assets/images/placeholder.png')) { \CMF\Image::server()->setSource(\CMF\Storage::adapter(CMFPATH . 'modules/image/public')); } \CMF\Image::server()->outputImage('assets/images/placeholder.png', array('w' => intval($params['w']), 'h' => intval($params['h']), 'bg' => 'efefef', 'fit' => 'fill')); } }
/** * Alias for \CMF\Image::getUrl, for backwards compatibility */ public static function getCropUrl($image, $width, $height, $crop_id = 'main', $crop_mode = 2, $params = array()) { return \CMF\Image::getUrl($image, $width, $height, $crop_id, $crop_mode, $params); }
public function w_h($mode, $w, $h, $bgcolor = '#FFF', $bw = false) { // Translate the mode to something more readable $mode = $this->mode = isset($this->modes[strval($mode)]) ? $this->modes[strval($mode)] : strval($mode); // Init the image info with the filename format, and output if a cache has been produced $output = $this->_init_image($w . 'x' . $h . '_' . $mode . ($bw ? '_bw' : '')); if (!is_null($output)) { return $output; } // Load the image and apply the filters switch ($this->mode) { case 'cr': $img = \Image::load($this->pathinfo['path'])->config('bgcolor', $bgcolor)->crop_resize($w, $h); break; case 'fr': $img = \Image::load($this->pathinfo['path']); $sizes = $img->sizes(); if ($sizes->width > $w || $sizes->height > $h) { $img->config('bgcolor', $bgcolor)->resize($w, $h); } break; case 'pr': $img = \Image::load($this->pathinfo['path'])->config('bgcolor', $bgcolor)->resize($w, $h, true, true); break; default: $img = \Image::load($this->pathinfo['path'])->config('bgcolor', $bgcolor)->resize($w, $h, true, false); break; } if ($bw === true) { $img->grayscale(); } // Save it $img->save($this->resized); // Return the newly created filename return $this->resized; }
/** * Sync files for a single model */ public static function syncFileFieldsFor($model) { if (!\Config::get('cmf.cdn.enabled')) { return; } $metadata = $model->metadata(); $fields = $metadata->fieldMappings; $staticFiles = array(); $localAdaper = static::adapter(); $cdn = static::getCDNAdapter(); foreach ($fields as $field_name => $field) { if ($field['type'] != 'image' && $field['type'] != 'file') { continue; } $fileId = null; $field_value = $model->{$field_name}; if (is_array($field_value) && !empty($field_value['src'])) { $path = static::getDocumentRoot() . $field_value['src']; if (file_exists($path)) { $files = \DB::query("SELECT * FROM `_files` WHERE `path` = :path AND `storage` = 'local' ORDER BY `id` DESC LIMIT 1")->bind('path', $field_value['src'])->execute(); if (!count($files)) { // Update file entry to the database $result = \DB::insert('_files')->set(array('path' => $field_value['src'], 'url' => '/' . $field_value['src'], 'storage' => 'local', 'type' => $metadata->name, 'field' => $field_name, 'created_at' => date('Y-m-d H:i:s', filectime($path)), 'updated_at' => date('Y-m-d H:i:s', filemtime($path))))->execute(); $fileId = intval(@$result[0]); } else { $fileId = intval($files->get('id')); } if ($field['type'] == 'image' && !empty($fileId)) { $cdnFiles = \DB::query("SELECT * FROM `_files` WHERE `field` = :field AND `type` = :type AND `storage` = 'cdn' GROUP BY `params` ORDER BY `id` DESC")->bind('field', $field_name)->bind('type', $metadata->name)->execute()->as_array(); foreach ($cdnFiles as $cdnFile) { if (!empty($cdnFile['params']) && ($params = @unserialize($cdnFile['params']))) { $params['path'] = $params['url'] = $field_value['src']; \CMF\Image::getUrlFromParams($params); continue; } \CMF\Image::getUrlFromParams(array('url' => '/' . ltrim($field_value['src'], '/'))); } } else { if ($field['type'] == 'file' && !empty($fileId)) { $cachedFiles = \DB::query("SELECT * FROM `_files` WHERE `path` = :path AND `storage` = 'cdn' ORDER BY `id` DESC LIMIT 1")->bind('path', $field_value['src'])->execute(); if (!count($cachedFiles)) { // Update cached file entry to the database $cachedResult = \DB::insert('_files')->set(array('path' => $field_value['src'], 'url' => '/' . $field_value['src'], 'storage' => 'cdn', 'type' => $metadata->name, 'field' => $field_name, 'created_at' => date('Y-m-d H:i:s', filectime($path)), 'updated_at' => date('Y-m-d H:i:s', filemtime($path))))->execute(); $cachedFileId = intval(@$cachedResult[0]); } else { $cachedFileId = intval($cachedFiles->get('id')); } if (\Fuel::$is_cli) { \Cli::write($field_value['src']); } if (!empty($cachedFileId)) { // Just upload files straight to the CDN $staticFiles[] = $field_value['src']; } } } } } else { // Field is blank. We should probably remove any remote copies of the file to save on disk space } } // Upload the static files foreach ($staticFiles as $staticFile) { try { // Write the file to CDN if it doesn't exist there if (!$cdn->has($staticFile)) { if (\Fuel::$is_cli) { \Cli::write("uploading '{$staticFile}' to CDN"); } $cdn->write($staticFile, $localAdaper->read($staticFile), array('visibility' => 'public')); } } catch (\Exception $e) { } } }