从php的图像文件中回显文件名

时间:2019-03-09 20:21:06

标签: php

大家好,我需要一些帮助。我可以这样做吗?我正在研究具有此PHP配置的新脚本。

class crudMedthods{   
getDataUser(Data) async {
             return await 
Firestore.instance.collection('users').document(Data).get();   } }

          class LoginScreen extends StatefulWidget {   @override  
          _LoginScreenState createState() => _LoginScreenState(); }

          class _LoginScreenState extends State<LoginScreen> {  
          TextEditingController username = TextEditingController();  
          SharedPreferences sharedPreferences;    
     crudMedthods crudObj = crudMedthods();     
     var user;

            Future<void> signIn() async {
              final formState = _formKey.currentState;
              if (formState.validate()) {
                formState.save();
                await FirebaseAuth.instance.signInWithEmailAndPassword(
                    email: username.text, password: password.text);
                try {
                  Navigator.push(
                      context, MaterialPageRoute(builder: (context) => HomeScreen()));
                } catch (e) {
                  print(e.message);
                }
              }   }

            @override   Widget build(BuildContext context) {
              return MaterialApp(
                home: Scaffold(
                  resizeToAvoidBottomPadding: false,
                  body: ListView(children: <Widget>[
                    Column(children: <Widget>[TextFormField(
                      controller: username,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                            borderSide: BorderSide(
                                color: Colors.lightBlue[50], width: 10),
                            borderRadius: BorderRadius.circular(26.0)),
                        contentPadding: EdgeInsets.symmetric(vertical: 17.0, horizontal: 10.0),
                        hintText: "username",
                        fillColor: Colors.teal[200].withOpacity(0.3),
                        filled: true,
                        labelText: 'Email',
                      ),
                      keyboardType: TextInputType.emailAddress,
                    ),
                      RaisedButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(25.0),
                            side: BorderSide(
                                width: 1, color: Color(0xffd50000))),
                        color: Color(0xffd50000),
                        onPressed: () async {
                          crudObj.getDataUser(username.text).then((results) {
                            setState(() {
                              user = results;
                            });
                          });
                          signIn();
                          sharedPreferences =
                          await SharedPreferences.getInstance();
                          sharedPreferences.setString(
                              "username", username.text);
                          sharedPreferences.setBool(
                              "isAdmin", user["isAdmin"]);
                        },
                        child: Text(
                          'Sign in with Email',
                          style: TextStyle(
                            fontSize: 20,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ])
                  ]),
                ),
              );   
     } }

echo配置会在每次重新加载og:image和og:title时更改图像文件,并使用将显示在og:image上的图像名称自动更改标题

所有人都可以使用,但是会显示在og:image上的图像会在标题中显示图像的类型,例如love.jpg

它像那样显示

<?php
function random_pic($dir, $nf=3)
{
$files = glob($dir . DIRECTORY_SEPARATOR . '*.jpg');
shuffle($files);
return array_pop($files);
}
//$number = rand(); ?v=$number
$path = 'images';
$file_images = random_pic($path, 1);
$image  = file_get_contents($file_images);
$og_img = base64_encode($image); 
?>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="<?php echo basename($file_images)?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://YourDomain.com" />
<meta property="og:image" content="http://YourDomain.com/<?php echo $file_images ?>" />

问题是我如何在图像标题中隐藏jpg

<meta property="og:title" content="Love.jpg" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://yourdomain.com" />
<meta property="og:image" content="http://yourdomain.com/images/Love.jpg"/>
<meta property="og:description" content="Liked it? Click Here for More."/>
<meta property="og:site_name" content="yourdomain.com - Love Test?"/>

先谢谢您

1 个答案:

答案 0 :(得分:1)

您可以使用pathinfo尝试以下操作:

<?php
function random_pic($dir, $nf=3) {
    $files = glob($dir . DIRECTORY_SEPARATOR . '*.jpg');
    shuffle($files);
    return array_pop($files);
}

$path = 'images';
$file_images = random_pic($path, 1);
$fileinfo = pathinfo($file_images);
$image  = file_get_contents($file_images);
$og_img = base64_encode($image); 
?>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="<?= $fileinfo['filename'] ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://YourDomain.com" />
<meta property="og:image" content="http://YourDomain.com/<?= $file_images ?>" />