【问题标题】:Append text string to end of image file on page load / download在页面加载/下载时将文本字符串附加到图像文件的末尾
【发布时间】:2021-02-19 14:58:55
【问题描述】:

我想实时将这样的内容(上帝帮助我)附加到一个低流量的网站:

当页面加载时,一些资源(图像)被加载(懒惰地)让我们假设页面通常会提供 image1.png、image2.png 和 image3.png

我想将 "username:%user%" 连接到 image1/2/3.png 的末尾(不是作为水印,而是将字符串添加到文件本身),其中用户是登录 wp_user通过实时将它们复制到临时目录并从那里提供它们。

虽然这是一个非正统的请求,但有没有任何现实的方法可以使用 nginx 或 wordpress 来做到这一点?

包括 site.conf

server {
server_name test.com www.test.com;
root /var/www/test.com/public_html/;
index index.php index.html index.htm index.nginx-debian.html;
client_max_body_size 100M;
location / {
     proxy_max_temp_file_size 0;
     try_files $uri $uri/ /index.php?q=$uri&$args; 
}

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
    }
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    location = /xmlrpc.php {
      deny all;
      log_not_found off;
      return 200;
    }

listen 443 ssl; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

【问题讨论】:

  • 不知道您的 WP 页面是如何组织的,但通常您可以使用 PHP GDImageMagick 扩展进行任何图像处理(当然您需要它们出现在您的服务器上/托管)。也可以在没有任何临时文件即时生成输出的情况下执行此操作。
  • 感谢您的洞察力。我过去有很多编码经验,但自从互联网早期(1998 年左右)以来,我对 PHP 的了解很少。我认为其中任何一个都应该适合我,因为我将它自托管在 DigitalOcean 液滴上
  • 自托管是一个很大的优势 :) 几乎每个共享主机都可以选择使用 GD 扩展,不幸的是,ImageMagick 更强大,但托管服务提供商的选项中经常缺少这些选项。无论如何,您的任务并不复杂,需要 ImageMagick 高级功能。
  • 我正在查看文档,我看到很多关于向图像添加文本(可见)但没有将其添加到文件本身的内容。比如 $f = fopen("file.png","wr"); fwrite(f, $username) fclose($f) echo

标签: wordpress nginx woocommerce


【解决方案1】:

来了。首先,一个 nginx 配置部分。如果wp-content/uploads/master目录下只有PNG文件,可以用更简单的

location ^~ /wp-content/uploads/master/ {
    rewrite ^ /pngproxy.php last;
}

(请注意^~ 修饰符,在它的帮助下,此位置将优先于location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { ... } 之一。如果该目录中有其他类型的文件,请使用正则表达式匹配位置

location ~ ^/wp-content/uploads/master/.+\.png$ {
    rewrite ^ /pngproxy.php last;
}

(应该是之前location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { ... })这样,任何对/wp-content/uploads/master/ 目录中的PNG 文件的请求都将转到pngproxy.php 脚本。

现在是 PHP 部分。将此文件命名为 pngproxy.php 并将其放入 WordPress 根目录:

<?php
// load WordPress core
require_once('wp-load.php');

if ( is_user_logged_in() // if WordPress user is logged in
     // get an original URI from 'REQUEST_URI' FastCGI parameter and check if it is a path to .png file
     // regex can be changed to be more strict, for example including the '/wp-content/uploads/master/' path prefix
     && preg_match( '/^\\/([^?]*\\.png)(?:$|\\?)/', $_SERVER['REQUEST_URI'], $matches )
     // and the file exists
     && file_exists( ABSPATH . $matches[1] )
     // and it is readable
     && ( $png = file_get_contents( ABSPATH . $matches[1] ) ) ) {

        $current_user = wp_get_current_user();
        // add user name signature
        $png .= "username:%{$current_user->user_login}%";
        // output the headers
        header( 'Content-Type: image/png' );
        header( 'Content-Length: ' . strlen( $png ) );
        // disable any sort of cache
        header( 'Cache-Control: no-cache, no-store, must-revalidate' );
        // output the data
        echo $png;

} else {
    // generate HTTP 404 Not Found
    header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    // or use HTTP 403 Forbidden if you like it more
    //header( $_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2015-02-02
    • 2022-12-11
    • 1970-01-01
    相关资源
    最近更新 更多