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

9
  • 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"] Commented Nov 28, 2019 at 16:19
  • do you use which web server? [nginx-apache] Commented Nov 28, 2019 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. Commented Nov 28, 2019 at 16:23
  • 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. Commented Nov 28, 2019 at 16:25
  • 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. Commented Nov 28, 2019 at 16:29

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.