简体   繁体   English

如何动态使用 PHP 脚本作为 img 标签并在 url 中传递文本和图像名称/类型来执行 PHP 脚本

[英]How can I dynamically use PHP script as img tag and pass text and image name/type in url to execute PHP script

I'm pretty much trying to do my own internal placeholder.com functionality.我几乎正在尝试做我自己的内部 placeholder.com 功能。 Where I can pass an image name and text in a URL to a PHP script but have the URL act like an image.我可以将 URL 中的图像名称和文本传递给 PHP 脚本,但 URL 就像图像一样。

I have a PHP script that will dynamically insert text over an image using the GD PHP library .我有一个 PHP 脚本,它将使用GD PHP 库在图像上动态插入文本。 This part works fine but the URL is (for example): https://my-domain.com/images/image.php?text=Hello%20World这部分工作正常,但 URL 是(例如): https://my-domain.com/images/image.php?text=Hello%20World : https://my-domain.com/images/image.php?text=Hello%20World

<?php
//http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php
//https://stackoverflow.com/questions/13267846/how-to-add-text-to-an-image-with-php-gd-library
//https://www.php.net/manual/en/book.image.php
//https://stackoverflow.com/questions/43965548/how-to-wrap-text-written-on-an-image-with-the-gd-library
  //Set the Content Type
  header('Content-type: image/jpeg');

  $text = $_GET['text'];

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('paper-2056025_1920.jpg');

  // Allocate A Color For The Text
  $color = imagecolorallocate($jpg_image, 0, 0, 0);

  // Set Path to Font File
  $font_path = 'SourceSansPro-Light.ttf';

  // Set Text to Be Printed On Image
  //$text = "This is a sunset!";

  // Print Text On Image
  imagettftext($jpg_image, 120, 0, 600, 618, $color, $font_path, $text);

  // Send Image to Browser
  imagejpeg($jpg_image);

  // Clear Memory
  imagedestroy($jpg_image);
?>

How can I change the URL so I can use this in <img> tags and pass the image name and file type and text for example: https://my-domain.com/images/my-picture.jpg?text=Hello%20World or https://my-domain.com/images/smiley.png?text=More%20Text如何更改 URL 以便我可以在<img>标签中使用它并传递图像名称和文件类型以及文本,例如: https://my-domain.com/images/my-picture.jpg?text=Hello%20World : https://my-domain.com/images/my-picture.jpg?text=Hello%20World text https://my-domain.com/images/my-picture.jpg?text=Hello%20Worldhttps://my-domain.com/images/smiley.png?text=More%20Text

For example, Placeholder.com does this by passing the text and image name/type in the URL which allows you to use this as an image tag: https://via.placeholder.com/728x90.png?text=Visit+WhoIsHostingThis.com+Buyers+Guide例如,Placeholder.com 通过在 URL 中传递文本和图像名称/类型来实现此目的,该 URL 允许您将其用作图像标签: https://via.placeholder.com/728x90.png?text=Visit+WhoIsHostingThis.com+Buyers+Guide : https://via.placeholder.com/728x90.png?text=Visit+WhoIsHostingThis.com+Buyers+Guide text https://via.placeholder.com/728x90.png?text=Visit+WhoIsHostingThis.com+Buyers+Guide

在此处输入图片说明

There are similar questions but nothing looks like it's specific to my use case: Create Image From Url Any File Type有类似的问题,但似乎没有什么特定于我的用例: 从 Url 任何文件类型创建图像

You can configure your web server for getting file extension and other vars.您可以配置您的 Web 服务器以获取文件扩展名和其他变量。 Why you don`t use rewrite features in your web server?为什么不在 Web 服务器中使用重写功能?

so send all requests to your x.php and parse file extension and query strings.所以将所有请求发送到您的 x.php 并解析文件扩展名和查询字符串。

example for nginx: nginx的例子:

       rewrite ^/index.php?(.*)$ /$1 permanent;

        try_files $uri @rewriteapp;
        location @rewriteapp {
                rewrite ^(.*)$ /index.php/$1 last;
        }
var_dump($_SERVER['REQUEST_URI']);
//string(17) "/image.png?text=2"

尝试显示来自 PHP 脚本的回显图像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目标:</p><p> 1)我在 web 根目录之外有一个图像文件夹,其中放置了来自 web 应用程序的所有图像(每个图像路径都保存在数据库表中);</p><p> 2) 我想根据用户的请求获取该图像。</p><p> 很简单,对吧?</p><p> 我做了:</p><p> a) 创建一个 class 来读取图像(具有我记得的所有安全功能和保护措施)。 它(成功地)返回使用相应的 mime 类型编码(base64)的图像。</p><p> b) 出于演示目的,我创建了一个 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在这里唯一的目标是从我的 web 应用程序中的任何其他地方调用一个脚本,该脚本输出具有相应 mime 类型的 base64 编码图像。</p><p> c) 创建了一个脚本“agivenscript.php”,它通过 GET 接收图像名称,实例化我的 class 并在一切顺利时获取 base64 编码图像。 然后它与图像相呼应。</p><p> 怎么了:</p><ul><li> 当我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一个魅力;</li><li> 此外,如果我将 $output64encodedwithmimetype 内容直接放在 index.php 的 src 标记中,它也可以工作。</li><li> 但是,当我尝试&lt;img src="agivenscript.php?img=name.jpeg"&gt;时它不起作用。</li></ul><p> 由于 base64 编码的图像显然很好(并且具有 mime 类型),我错过了什么? 任何想法?</p><p> 预先感谢。</p></div> - Trying to display an echoed image from a PHP script in and `<img src=“…”` tag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Script标记内执行PHP函数? - How Can I execute a PHP function inside a Script tag? 我可以使用img标记的源来使用PHP从URL捕获图像吗? - Can I use the source of an img tag to capture an image from a URL using PHP? 在img标签中阻止php脚本 - block script php in a img tag 如何编写php文件脚本以显示图像 <img src=“/img.php?imageID=32” /> ? - How do I script a php file to display an image like <img src=“/img.php?imageID=32” />? 尝试显示来自 PHP 脚本的回显图像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目标:</p><p> 1)我在 web 根目录之外有一个图像文件夹,其中放置了来自 web 应用程序的所有图像(每个图像路径都保存在数据库表中);</p><p> 2) 我想根据用户的请求获取该图像。</p><p> 很简单,对吧?</p><p> 我做了:</p><p> a) 创建一个 class 来读取图像(具有我记得的所有安全功能和保护措施)。 它(成功地)返回使用相应的 mime 类型编码(base64)的图像。</p><p> b) 出于演示目的,我创建了一个 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在这里唯一的目标是从我的 web 应用程序中的任何其他地方调用一个脚本,该脚本输出具有相应 mime 类型的 base64 编码图像。</p><p> c) 创建了一个脚本“agivenscript.php”,它通过 GET 接收图像名称,实例化我的 class 并在一切顺利时获取 base64 编码图像。 然后它与图像相呼应。</p><p> 怎么了:</p><ul><li> 当我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一个魅力;</li><li> 此外,如果我将 $output64encodedwithmimetype 内容直接放在 index.php 的 src 标记中,它也可以工作。</li><li> 但是,当我尝试&lt;img src="agivenscript.php?img=name.jpeg"&gt;时它不起作用。</li></ul><p> 由于 base64 编码的图像显然很好(并且具有 mime 类型),我错过了什么? 任何想法?</p><p> 预先感谢。</p></div> - Trying to display an echoed image from a PHP script in and `<img src=“…”` tag Zend Framework:img src标签中的PHP脚本导致图像损坏 - Zend Framework: PHP script in img src tag causing broken image 使用PHP脚本执行JS <script> tag on the homepage only - Use a PHP script to execute a JS <script> tag on the homepage only 如何刮一个<script type = “text / javascript”> tag in php? - how to scrape a <script type = “text / javascript”> tag in php? 如何在wordpress网站内使用PHP查找散列的bundle.js文件并将相应的文件名插入script标签? - How can I use PHP inside a wordpress site to find a hashed bundle.js file and insert the appropriate file name into the script tag? 如何将输入从PHP脚本传递到另一个脚本? - How can I pass the input from a PHP script to another script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM