- protected function getFacebookImage(): array {
- //Set texts
- $texts = $this->facebookImage['texts'] ?? [];
-
- //Set default source
- $source = $this->facebookImage['source'] ?? 'png/facebook.png';
-
- //Set default source
- $updated = $this->facebookImage['updated'] ?? strtotime('last week');
-
- //Set source path
- $src = $this->config['path']['public'].'/'.$source;
-
- //Set cache path
- //XXX: remove extension and store as png anyway
- $cache = $this->config['path']['cache'].'/facebook/'.substr($source, 0, strrpos($source, '.')).'.'.$this->config['facebook']['width'].'x'.$this->config['facebook']['height'].'.png';
-
- //Set destination path
- $dest = $this->config['path']['public'].'/'.$this->facebookImage['destination'];
-
- //Set asset
- $asset = '@RapsysAir/'.$this->facebookImage['destination'];
-
- //With up to date generated image
- if (
- is_file($dest) &&
- ($stat = stat($dest)) &&
- $stat['mtime'] >= $updated
- ) {
- //Get image size
- //TODO: see if it works every time
- list ($width, $height) = getimagesize($dest);
-
- //Return image data
- return [
- //TODO: see if it works every time
- 'image' => $this->stack->getCurrentRequest()->getUriForPath($this->asset->getUrl($asset), true),#.'?fbrefresh='.$stat['mtime'],
- 'image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))),
- 'image:height' => $height,
- 'image:width' => $width
- ];
- //With image candidate
- } elseif (is_file($src)) {
- //Create image object
- $image = new \Imagick();
-
- //With cache image
- if (is_file($cache)) {
- //Read image
- $image->readImage($cache);
- //Without we generate it
- } else {
- //Check target directory
- if (!is_dir($dir = dirname($cache))) {
- //Create filesystem object
- $filesystem = new Filesystem();
-
- try {
- //Create dir
- //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
- $filesystem->mkdir($dir, 0775);
- } catch (IOExceptionInterface $e) {
- //Throw error
- throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
- }
- }
-
- //Read image
- $image->readImage($src);
-
- //Crop using aspect ratio
- //XXX: for better result upload image directly in aspect ratio :)
- $image->cropThumbnailImage($this->config['facebook']['width'], $this->config['facebook']['height']);