Jump to content

Dynamic image doesn't work when it is in a class


The Little Guy

Recommended Posts

When I place the following into a class, the image doesn't build correctly, I don't know why...

 

The code (not in class):

$str = $_GET['str'];
$font = '../../../incl/fonts/dirty_and_classic.ttf';
$lineCount = 40; 
// Size of the font 
$fontSize = 160; 
// Height of the image 
$height = 90; 
// Width of the image 
$width = 500; 
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image"); 
// Set the Background Color RGB 
$backColor = imagecolorallocate($img_handle, 37, 39, 45);
// Set the Text Color RGB 
$txtColor = imagecolorallocate($img_handle, 255, 255, 255);
$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function'); 
$x = 25;
$y = 50;
imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function'); 
header('Content-Type: image/jpeg'); 
imagejpeg($img_handle, NULL, 100); 
imagedestroy($img_handle);

 

When I move it out of the class it works fine.

 

In class:

<?php
class Image{
public $font = '../../../incl/fonts/dirty_and_classic.ttf';
public function __construct(){
	//$this->font = $_SERVER['DOCUMENT_ROOT'].'/incl/fonts/dirty_and_classic.ttf';
}
public function buildImage($str, $save = false, $filename = null){
	$lineCount = 40; 
	// Size of the font 
	$fontSize = 160; 
	// Height of the image 
	$height = 90; 
	// Width of the image 
	$width = 500; 
	$img_handle = imagecreate ($width, $height) or die ("Cannot Create image"); 
	// Set the Background Color RGB 
	$backColor = imagecolorallocate($img_handle, 37, 39, 45);
	// Set the Text Color RGB 
	$txtColor = imagecolorallocate($img_handle, 255, 255, 255);
	//echo getcwd();exit;
	$str = "Ryan Naddy";
	$textbox = imagettfbbox($fontSize, 0, $this->font, $str) or die('Error in imagettfbbox function'); 
	$x = 25;
	$y = 50;
	imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $this->font , $str) or die('Error in imagettftext function'); 
	header('Content-Type: image/jpeg'); 
	imagejpeg($img_handle, NULL, 100); 
	imagedestroy($img_handle);
}
}
?>  

 

When I run it as a class:

 

include '../../../classes/Image.php';
$img = new Image();
$img->buildImage("Ryan Naddy");

 

All that happens is I get a broken image.

 

Anyone know why this happens?

When I run the script (not from the class), if I have an include in the script, it doesn't work, but if I comment out the include it does work.  The include looks like this:

 

include '../../../classes/Image.php';

 

and all that is, is my class

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.