[英]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%20World
或https://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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.