Jump to content

Recommended Posts

Hello

I do not know why but this code seems to work fine only in my xampp local insallation but not in the remote server. I just see a black square instead of the number 0000000 when I run this code in the remote server through the link http://www.arreglaordenador.com/numberimage.php

 

and the code in numberimage.php is:

 
<?php
$fontDir = $rootPath . "fonts\\";
$fn = (isset($_GET['fn'])) ? $_GET['fn'] : "ARIAL.ttf";
$fs = (isset($_GET['fs'])) ? $_GET['fs'] : 10;
$maxWidth = 80 * $fs;

//$colorArray = array ("red", "green", "blue", "cyan", "magenta", "yellow", "black", "white", "gray");
  $colorArray = array ("black", "black", "black", "black", "black", "black", "black", "black", "black");
$color = (isset($_GET['color'])) ? $colorArray[$_GET['color']] : $colorArray[rand(0, ];
$txt = (isset($_GET['txt'])) ? $_GET['txt'] : "0000000";
$coords = imagettfbbox($fs, 0, $fontDir . $fn, $txt);
$w = abs($coords[4]) + 8;
$h = abs($coords[5]) + 8;
$img = imagecreatetruecolor($w, $h);
$red = imagecolorallocate($img, 255,0,0);
$green = imagecolorallocate($img, 0,255,0);
$blue = imagecolorallocate($img, 0,0,255);
$cyan = imagecolorallocate($img, 0,255,255);
$magenta = imagecolorallocate($img, 255,0,255);
$yellow = imagecolorallocate($img, 255,255,0);
$black = imagecolorallocate($img, 0,0,0);
$white = imagecolorallocate($img, 255,255,255);
$gray = imagecolorallocate($img, 127,127,127);
$fill = ($color == "white") ? $black : $white;
imagefill($img, 0, 0, $fill);
$position = 4;
if (isset($_GET['position'])) $position = $_GET['position']; 

imagettftext($img, $fs, 0, 0, $h-$position, $$color, "$fontDir$fn", $txt);

header("content-type: image/jpeg");
imagejpeg($img, "", 100);
imagedestroy($img);
?>

 

Does any of you have any suggestion?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/185098-square-instead-of-number/
Share on other sites

For debugging purposes, add the following two lines of code immediately after the first opening <?php tag and temporarily comment out the header() and imagejpeg() statements -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

 

I see three possible problems -

 

1) You are not defining $rootPath

2) You are using a \\ as a path separator, which on a UNIX/LINUX operating system has no meaning. You should always use / as a path separator (under Windows, php converts it to a \ before it passes it to the operating system.)

3) Under UNIX/LINUX, you generally must use an absolute file system path for the font files.

 

There are about a half dozen more possible problems which the error_reporting/display_errors lines should uncover.

Ok, thanks for those two lines for debugging. They helped me to know the problem was the $rootPath variable. Now, if you see the number is displayed, however the background is displayed in black color making the number very difficult to see. However, I do not understand why this just happens remotely and not locally.

 

<?php
$fontDir = "fonts/";
$fn = (isset($_GET['fn'])) ? $_GET['fn'] : "arial.ttf";
$fs = (isset($_GET['fs'])) ? $_GET['fs'] : 10;
$maxWidth = 80 * $fs;

$colorArray = array ("red", "green", "blue", "cyan", "magenta", "yellow", "black", "white", "gray");
// $colorArray = array ("black", "black", "black", "black", "black", "black", "black", "black", "black");
$color = (isset($_GET['color'])) ? $colorArray[$_GET['color']] : $colorArray[rand(0, ];
$txt = (isset($_GET['txt'])) ? $_GET['txt'] : "0000000";
$coords = imagettfbbox($fs, 0, $fontDir . $fn, $txt);
$w = abs($coords[4]) + 8;
$h = abs($coords[5]) + 8;
$img = imagecreatetruecolor($w, $h);
$red = imagecolorallocate($img, 255,0,0);
$green = imagecolorallocate($img, 0,255,0);
$blue = imagecolorallocate($img, 0,0,255);
$cyan = imagecolorallocate($img, 0,255,255);
$magenta = imagecolorallocate($img, 255,0,255);
$yellow = imagecolorallocate($img, 255,255,0);
$black = imagecolorallocate($img, 0,0,0);
$white = imagecolorallocate($img, 255,255,255);
$gray = imagecolorallocate($img, 127,127,127);
$fill = ($color == "white") ? $black : $white;
imagefill($img, 0, 0, $fill);
$position = 4;
if (isset($_GET['position'])) $position = $_GET['position']; 

imagettftext($img, $fs, 0, 0, $h-$position, $$color, "$fontDir$fn", $txt);

header("content-type: image/jpeg");
imagejpeg($img, "", 100);
imagedestroy($img);
?>

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.