php slim 配置,php – 使用Slim框架提供图像/集合

我猜你是在正确的轨道,但你错过了一些点.

我做了一个新的安装(“苗条/苗条”:“^ 2.6”).这是我的目录结构.

├── composer.json

├── composer.lock

├── public

│ ├── .htaccess

│ └── index.PHP

├── Resources

│ └── Images

│ └── smoke.svg

└── vendor

这是我的Slim应用程序文件index.PHP.顺便说一下,我没有花时间重新调整我留给你的图像.

该应用程序包含资产路径和测试图像路径.

require_once '../vendor/autoload.PHP';

$app = new \Slim\Slim();

$app->get('/assets/:height/:width/:id/:type',function($height,$width,$id,$type) use ($app) {

$dir = dirname(__DIR__)."/Resources/Images/";

$im = new Imagick();

$im->setBackgroundColor(new ImagickPixel('transparent'));

$svg = file_get_contents($dir.$id.'.svg');

$im->readImageBlob($svg);

$im->setImageFormat("png32");

$im->resizeImage($height,Imagick::FILTER_LANCZOS,1);

$app->response->header('Content-Type','content-type: image/'.$type );

echo $im;

$im->destroy();

});

$app->get('/test-image',function() use ($app) {

echo 'png';

});

$app->run();

.htacess文件的内容是;

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.PHP [QSA,L]

您还需要将AllowOverride部分添加到虚拟主机文件,以便能够使用.htacess文件中的指令.这个去了virtualhost.conf

AllowOverride All

Order allow,deny

Allow from all

不要忘记重启或重新加载apache.

然后浏览到http://< virtualhost> / test-image它应该不错.

更新:

在您发表评论后,我设置了一个安装了Nginx的docker实例.

这是我的virtualhost配置文件.

server {

server_name default;

root /var/www/default/public;

index index.PHP;

client_max_body_size 100M;

fastcgi_read_timeout 1800;

location / {

try_files $uri /index.PHP?$args;

}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)${

expires max;

log_not_found off;

access_log off;

}

location ~ \.PHP${

fastcgi_split_path_info ^(.+\.PHP)(/.+)$;

fastcgi_pass unix:/var/run/PHP5-fpm.sock;

fastcgi_index index.PHP;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

}

}

并更改了PHP代码一点点使用正确的URL为图像生成器路由.我命名了路由并使用urlFor方法来检索框架本身生成的正确url.

error_reporting(E_ALL);

ini_set('display_errors',1);

require_once '../vendor/autoload.PHP';

$app = new \Slim\Slim();

$app->get('/assets/:height/:width/:id/:type','content-type: image/'.$type );

echo $im;

$im->destroy();

})->name('generate-image');

$app->get('/test-image',function() use ($app) {

$imageUrl = $app->urlFor('generate-image',array('height' => '200','width' => '200','id' => 'smoke','type' => 'png')

);

echo ''.%24imageUrl.'';

});

$app->run();

当我浏览到http:// localhost / test-image url时,一切看起来都很完美.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值