Bkwld\Croppa\URL::generate PHP Метод

generate() публичный Метод

Insert Croppa parameter suffixes into a URL. For use as a helper in views when rendering image src attributes.
public generate ( string $url, integer $width = null, integer $height = null, array $options = null ) : string
$url string URL of an image that should be cropped
$width integer Target width
$height integer Target height
$options array Addtional Croppa options, passed as key/value pairs. Like array('resize')
Результат string The new path to your thumbnail
    public function generate($url, $width = null, $height = null, $options = null)
    {
        // Extract the path from a URL and remove it's leading slash
        $path = $this->toPath($url);
        // Skip croppa requests for images the ignore regexp
        if (isset($this->config['ignore']) && preg_match('#' . $this->config['ignore'] . '#', $path)) {
            return $this->pathToUrl($path);
        }
        // Defaults
        if (empty($path)) {
            return;
        }
        // Don't allow empty strings
        if (!$width && !$height) {
            return $this->pathToUrl($path);
        }
        // Pass through if empty
        $width = $width ? round($width) : '_';
        $height = $height ? round($height) : '_';
        // Produce width, height, and options
        $suffix = '-' . $width . 'x' . $height;
        if ($options && is_array($options)) {
            foreach ($options as $key => $val) {
                if (is_numeric($key)) {
                    $suffix .= '-' . $val;
                } elseif (is_array($val)) {
                    $suffix .= '-' . $key . '(' . implode(',', $val) . ')';
                } else {
                    $suffix .= '-' . $key . '(' . $val . ')';
                }
            }
        }
        // Assemble the new path
        $parts = pathinfo($path);
        $path = trim($parts['dirname'], '/') . '/' . $parts['filename'] . $suffix;
        if (isset($parts['extension'])) {
            $path .= '.' . $parts['extension'];
        }
        $url = $this->pathToUrl($path);
        // Secure with hash token
        if ($token = $this->signingToken($url)) {
            $url .= '?token=' . $token;
        }
        // Return the $url
        return $url;
    }

Usage Example

Пример #1
0
 public function testSecure()
 {
     $url = new URL(['signing_key' => 'test']);
     $this->assertEquals('/path/file-200x100.png?token=dc0787d205f619a2b2df8554c960072e', $url->generate('/path/file.png', 200, 100));
     $url = new URL(['signing_key' => 'test']);
     $this->assertNotEquals('/path/file-200x100.png?token=dc0787d205f619a2b2df8554c960072e', $url->generate('/path/file.png', 200, 200));
 }