skip to Main Content

I created a simple image on a php page, code is below.
I need to take the url of this image as a direct link for other websites.
I tried the same code on a webhosting provider and everything works as it should, but when I use the same code on my vps, it doesn’t work as a direct link.
If I check from web browser they’re identical so what am I missing?

I also noticed that if I use it on Telegram for example, only the one which works generate a thumbnail.

<?php

header("Content-type: image/png");
$im     = imagecreatefrompng("image/orange.png");
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'font/font.ttf';
$string = "test";
imagettftext($im, 20, 0, 20, 230, $white, $font, $string);
imagepng($im);
imagedestroy($im);

?>

EDIT : Could it affect the fact that to access the link in vps I use the IP adress of the machine while for the webhosting I use their domain?

2

Answers


  1. There are a catalog of reasons why it might not work – far too many to list here. But the reason I’m posting is that you don’t seem to have made any attempt to diagnose the problem yourself. Maybe you don’t know how.

    Your first step is to make sure the logging is working as it should on your platform. Write some code with deliberate mistakes in it. Make sure you can see those mistakes in your logs (and only in your browser for a dev environment). If not, find out why.

    Then try running your image script – and see if it is throwing any errors.

    Another good place to look for issues is in web developer tools – a non-200 status code should be a red flag that there is something very wrong with your code.

    But you also need to learn how to code defensively. Your code should detect issues and respond to them sensibly. Particularly when the thread of execution touches things outside the PHP runtime (like files).

    <?php
    
    $file="image/orange.png";
    $im     = imagecreatefrompng($file);
    if ($im===false) {
       bailout("failed to read $file as an image");
    }
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    $font = 'font/font.ttf';
    $string = "test";
    if (false===imagettftext($im, 20, 0, 20, 230, $white, $font, $string)) {
        bailout("imagettftext fail");
    }
    header("Content-type: image/png"); // note this moved here
    if (false===imagepng($im)) {
        bailout("imagepng failed");
    }
    imagedestroy($im);
    exit;
    
    function bailout($msg)
    {
       header("Content-type: text/html", True, 519); // yes I know its not html
       trigger_error($msg); // record specificreason in log
       print $msg;
       exit;
    }
    

    There is a whole lot other you might check for in your code – but they will mostly be caught/reported by PHP.

    Login or Signup to reply.
  2. you should be able to use this code in your vps using an IP.

    follow these questions/steps to find the problem :

    • Do you have a running web server on your vps? you can test this by creating a phpinfo() file and trying to access it
    • Is gd extension enabled in your php.ini? you can check this again with phpinfo()
    • there might be some other errors happening, to check this you can use the code below and see if it shows any error
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
    header("Content-type: image/png");
    $im     = imagecreatefrompng("image/orange.png");
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    $font = 'font/font.ttf';
    $string = "test";
    imagettftext($im, 20, 0, 20, 230, $white, $font, $string);
    imagepng($im);
    imagedestroy($im);
    

    there might be a file missing for example orange.png or font.ttf

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search