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?

Link to comment
Share on other sites

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.