spescina/imgproxy

Laravel 的图片代理

2.1.6 2014-10-14 07:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:23:45 UTC


README

Build Status Coverage Status

ImageProxy

Laravel 4 包,用于即时图像裁剪和缩放。底层使用 Timthumb。

安装与使用

composer.json 中添加

"require": {
    "spescina/imgproxy": "2.x"
}

运行 composer update

app/config/app.php 文件中添加服务提供者

"Spescina\Imgproxy\ImgproxyServiceProvider"

运行 php artisan asset:publish spescina/imgproxy 发布包资源

运行 php artisan config:publish spescina/imgproxy 发布包配置

ImageProxy 默认配置为使用 Apache。如果你使用 nginx,请将以下内容添加到你的网站配置文件中

rewrite ^/packages/spescina/imgproxy/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*) /packages/spescina/imgproxy/timthumb.php?w=$1&h=$2&zc=$3&q=$4&src=$5;

使用包外观生成资源 URL

ImgProxy::link("path/to/image.jpg", 100, 80)

这将生成类似这样的链接

http://www.yourdomain.com/packages/spescina/imgproxy/100/80/path/to/image.jpg

该链接将使用存储在 public/path/to 文件夹中的原始 image.jpg 文件生成一个 100 x 80 像素的图像。

参数

link 函数接受 5 个参数

  • 图像路径
  • 宽度
  • 高度
  • 质量 - 可选 [0..100] - 默认: 90
  • 缩放/裁剪 - 可选 [0,1,2,3] - 默认: 1

缩放/裁剪

这些是支持的值

  • 0 - 调整大小以适合指定尺寸(不裁剪)
  • 1 - 裁剪并调整大小以最适合尺寸(默认)
  • 2 - 按比例调整大小以将整个图像放入指定尺寸,如果需要则添加边框
  • 3 - 按比例调整大小,调整缩放图像的大小,以确保没有边框间隙

配置

包配置

发布包配置文件后,可以在 app/config/packages/spescina/imgproxy/config.php 文件中更改包的行为。

以下是当前选项

  • 重写 - 默认: true - 如果为 false,则生成查询字符串 URI 而不是美观的 URI

Timthumb 配置

可以在 public/packages/spescina/imgproxy/timthumb-config.php 文件中编辑 Timthumb 配置。

目前,这些是默认值

define ("DEBUG_ON", false);

define ("DEBUG_LEVEL", 3);

define ("FILE_CACHE_MAX_FILE_AGE", 86400);

define ("FILE_CACHE_SUFFIX", ".imgproxy.cache");

define ("FILE_CACHE_PREFIX", "");

define ("FILE_CACHE_DIRECTORY", "../../../../app/storage/cache/imgproxy");

define ("NOT_FOUND_IMAGE", "./nophoto.gif");

define ("ERROR_IMAGE", "./nophoto.gif");

define ("PNG_IS_TRANSPARENT", FALSE);

define ("DEFAULT_Q", 90);

Laravel Forge 的完整 nginx 示例

server {
    rewrite_log on;

    listen 80;
    server_name my_site.com;
    root /home/forge/my_site.com/public;

    auth_basic "Restricted";
    auth_basic_user_file /home/forge/my_site.com/public/.htpasswd;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl on;
    # ssl_certificate;
    # ssl_certificate_key;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        rewrite ^/packages/spescina/imgproxy/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*) /packages/spescina/imgproxy/timthumb.php?w=$1&h=$2&zc=$3&q=$4&src=$5;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/my_site.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}