1

I'm pretty much trying to do my own internal placeholder.com functionality. Where I can pass an image name and text in a URL to a PHP script but have the URL act like an image.

I have a PHP script that will dynamically insert text over an image using the GD PHP library. This part works fine but the URL is (for example): 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

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

enter image description here

There are similar questions but nothing looks like it's specific to my use case: Create Image From Url Any File Type

ADyson
  • 57,178
  • 14
  • 51
  • 63
Derek
  • 4,747
  • 7
  • 44
  • 79
  • 1
    why can't you use your script in `img` tags right now? It's unclear. What goes wrong when you try? if you want to pass the filename then make the URL something like `https://my-domain.com/images/image.php?name=my-picture.jpg&text=Hello%20World` and then you can get the file name in the script using `$_GET["name"]` – ADyson Nov 28 '19 at 16:19
  • do you use which web server? [nginx-apache] – delirehberi Nov 28 '19 at 16:20
  • P.S. Placeholder.com probably has some special webserver config to direct requests with a .jpg (or similar) extension to a script handler (such as PHP or whatever), so it really executes a specific script, and then extracts the filename from the URL string. But that's just over-complicating things. You don't _need_ to do that. It makes the URLs look a tiny bit nicer, but that's about all. So yeah go for it if you want, configure your webserver to redirect those requests and then use PHP to fish the filename from the URL, but if I were you I'd do it the simpler way using a GET parameter. – ADyson Nov 28 '19 at 16:23
  • @ADyson, I can use `https://my-domain/images/image.php?text=Hello` right now but the link/url isn't pretty as it points to a PHP script and not an image. Some page builders or WYSIWYG editors may throw an error because it's not the right file type. I also don't have the logic for passing the image name/file and replacing in the url – Derek Nov 28 '19 at 16:24
  • @delirehberi my testing environment is on apache but my production will be on nginx. – Derek Nov 28 '19 at 16:25
  • 1
    _"Some page builders or WYSIWYG editors may throw an error because it's not the right file type"_ ...can you give an example? I'd expect they'd be more bothered about the content-type of the response than having a file extension on the URL. – ADyson Nov 28 '19 at 16:25
  • _"the link/url isn't pretty"_ ...see my second comment above. It's true, but so what? Do people really care what the URL looks like? Based on my recent experiences, most users don't even glance at the URL bar (I know this from dealing with support calls where they have no idea what page they're on etc etc). And if this is in an `img` tag, it'll be completely invisible to anyone who isn't inspecting your HTML source code. – ADyson Nov 28 '19 at 16:26
  • _"I also don't have the logic for passing the image name/file and replacing in the url"_ ...see my first comment (refresh to update if you haven't already, I edited it to add more info) – ADyson Nov 28 '19 at 16:29
  • 2
    Just a warning... If you do this kind of dynamic image generation, you should probably cache the generated images (so you don't need to re-generate the same image with the same text over and over) and also sign the requests. If not, someone could simply just do a for-loop to generate new images and sink your server. Image processing puts a heavy load on the server. – M. Eriksson Nov 28 '19 at 16:29

1 Answers1

0

You can configure your web server for getting file extension and other vars. Why you don`t use rewrite features in your web server?

so send all requests to your x.php and parse file extension and query strings.

example for 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"
delirehberi
  • 470
  • 3
  • 17