Monday 3 September 2018

PHP - Code works on localhost, but not on a live site

This question already has an answer here:

  • imagejpeg(); not generating image on host server , working fine on localhost 2 answers
Ok, here is the problem. This code works on localhost, but not on a live site
<?php
session_start();
header('Content-type: image/jpeg');

$text = $_SESSION['secure'];
$font_size = 30;
$image_width  = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);

for($x=1; $x<=30; $x++){
  $x1 = rand(1, 100);
  $y1 = rand(1, 100);
  $x2 = rand(1, 100);
  $y2 = rand(1, 100);
  imageline($image, $x1, $y1, $x2, $y2, $line_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>

The localhost and the webhosting are both running the same php version
Take a look at this picture before you post anything, please!:

try to look at error.log (APACHE), it could also be possible that there is a session problem, you could try to hide all errors by using "error_reporting(0);
try this:
<?php
@session_start();
error_reporting(0);
@header('Content-type: image/jpeg');

$text = $_SESSION['secure'];
$font_size = 30;

$image_width = 120;
$image_height = 40;

$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);

for ($x=1; $x<=30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);

imageline($image, $x1, $y1, $x2, $y2, $line_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);

?>

but the best, would be to fix the error, so look @ the error.log
if you got any error message, you could tell us - so we will try to help you :)

0 comments:

Post a Comment