Newscoop\Image\Rendition::generateImage PHP Method

generateImage() public method

Generate image
public generateImage ( string $imagePath ) : Imagine\Gd\Image
$imagePath string
return Imagine\Gd\Image
    public function generateImage($imagePath)
    {
        $path = is_file(APPLICATION_PATH . '/../' . $imagePath) ? APPLICATION_PATH . '/../' . $imagePath : $imagePath;
        $imagine = ImageService::getImagine();
        $image = $imagine->open($path);
        $imageSize = $image->getSize();
        if ($this->isCrop()) {
            $cropSpecs = explode('_', $this->getSpecs());
            if (count($cropSpecs) === 1) {
                list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
                $image->resize(new Box($width, $height));
                list($left, $top, $width, $height) = ImageService::calculateCutout($width, $height, '50%', '50%', $this->width, $this->height);
                $image->crop(new Point($left, $top), new Box($width, $height));
            } else {
                list(, $x0, $y0, $x1, $y1) = $cropSpecs;
                $image->crop(new Point($x0, $y0), new Box($x1 - $x0, $y1 - $y0));
                $imageSize = $image->getSize();
                list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
                $image->resize(new Box($width, $height));
            }
        } else {
            list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
            $image->resize(new Box($width, $height));
        }
        return $image;
    }

Usage Example

示例#1
0
 /**
  * Generate image for given src
  *
  * @param string $src
  *
  * @return void
  */
 public function generateFromSrc($src)
 {
     $matches = array();
     if (!preg_match('#^([0-9]+)x([0-9]+)/([_a-z0-9]+)/([-_.:~%|a-zA-Z0-9]+)$#', $src, $matches)) {
         return;
     }
     list(, $width, $height, $specs, $imagePath) = $matches;
     $destFolder = rtrim($this->config['cache_path'], '/') . '/' . dirname(ltrim($src, './'));
     if (!realpath($destFolder)) {
         mkdir($destFolder, 0755, true);
     }
     if (!is_dir($destFolder)) {
         throw new \RuntimeException("Can't create folder '{$destFolder}'.");
     }
     $rendition = new Rendition($width, $height, $specs);
     $image = $rendition->generateImage($this->decodePath($imagePath));
     $image->save($destFolder . '/' . $imagePath);
     return $image;
 }
All Usage Examples Of Newscoop\Image\Rendition::generateImage