加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用Slim框架提供图像/集合

发布时间:2020-12-13 13:20:59 所属栏目:PHP教程 来源:网络整理
导读:我需要将多个模块中常见的图像提供给公共文件夹外部的资源文件夹.我正在使用Slim Framework. app/- -Classes/ -vendor/ -Resources/- - Images/ -Admin/- - Styles/ - Scripts - index.php - init.php -Public/- - Styles/ - Scripts - index.php - init.php
我需要将多个模块中常见的图像提供给公共文件夹外部的资源文件夹.我正在使用Slim Framework.
app/-
    -Classes/
    -vendor/
    -Resources/-
               - Images/
    -Admin/-
           - Styles/
           - Scripts
           - index.php
           - init.php
    -Public/-
            - Styles/
            - Scripts
            - index.php
            - init.php

目前我已经创建了一个子域static.pro1.local /来提供本地图像.但现在我正在寻找其他方式.

在苗条的时候,我正在尝试根据需要制作动态创建和提供图像的路线

$app->get('/assets/:height/:width/:id/:type',function() use ($app) {
    header('Content-type: image/jpeg');
    $dir = dirname(__DIR__)."/Resources/Images/";
    $image = new Imagick($dir.$id.'.svg');
    /*
        Code to create images based on height,width,and change its format {svg to png} as required
    */
        $app->response->header('Content-Type','content-type: image/'.$type );
        echo $image;
    // $res->body($image);
});

我打算将它用作< img src =“/ assets / 200/400 / test / png”/>但我一直得到404错误.

我已经试过了

> http://help.slimframework.com/discussions/questions/93-how-get-image-for-a-specific-route
> http://help.slimframework.com/discussions/questions/674-best-practice-to-return-images
> http://help.slimframework.com/discussions/questions/359-file-download
> https://stackoverflow.com/questions/20439144/php-how-to-use-a-rest-controller-to-serve-images

还有其他一些.
我也找到了https://github.com/tuupola/slim-image-resize,但我需要使用SVG转换和其他一些它没有的东西.

我猜你是在正确的轨道,但你错过了一些点.
我做了一个新的安装(“苗条/苗条”:“^ 2.6”).这是我的目录结构.
├── composer.json
├── composer.lock
├── public
│ ├── .htaccess
│ └── index.php
├── Resources
│ └── Images
│     └── smoke.svg
└── vendor

这是我的Slim应用程序文件index.php.顺便说一下,我没有花时间重新调整我留给你的图像.
该应用程序包含资产路径和测试图像路径.

<?php
require_once '../vendor/autoload.php';

$app = new SlimSlim();
$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 '<img src="assets/100/200/smoke/png" />';
});

$app->run();

诀窍是在.htaccess的帮助下从url中删除index.php
在文档中有一个用于重写URL的部分; http://docs.slimframework.com/routing/rewrite/-文档也包括nginx指令.-

.htacess文件的内容是;

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

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

<Directory "/var/www/PUBLIC_DIRECTORY">
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

不要忘记重启或重新加载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.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

require_once '../vendor/autoload.php';

$app = new SlimSlim();
$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 '<img src="'.$imageUrl.'" />';
});

$app->run();

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读