Captcha image not showing - php

Ok...so...i'm trying the create my own Captcha to use on a webpage i'm working on.
Since i'm pretty much a noob on php i followed a youtube guide
i followed EVERY SINGLE STEP in this video, everything he did i did too, i dowloaded the same image lybrary(AnonymousClippings.tff btw) and etc BUT for some reason my captcha image is not showing.
I have to files, one for the captcha and one for the index(to call the image function so i can see the image) and use it in a form.
i'm sure the reason for not showing the image is pretty stupid but i just can't figure what's the problem.
if you know a easier way to create my own captcha i would apreciate.
captcha.php
<?php
class captcha{
private static $captcha = "__captcha__";
private static $font;
private static $width = 70;
private static $height = 70;
private static $font_size = 40;
private static $character_width = 40;
private static function session_start(){
if(!isset($_SESSION)){
session_start();
}
}
private static function set_font(){
self::$font = self::$captcha;
$AnonymousClippings = "HERE GOES ALL POSSIBLE CAHRS THAT THE CAPTCHA WILL BE ABLE TO SHOW FROM AnonymousClippings LYBRARY";
$handle = fopen(self::$font,"w+");
fwrite($handle, base64_encode($AnonymousClippings));
fclose($handle);
return self::$font;
}
public static function get_random(){
$type = rand(0,2);
switch ($type){
case 2:
$random = chr(rand(65, 90));
break;
case 1:
$random = chr(rand(97, 122));
break;
default:
$random = chr(rand(0,9));
break;
}
return $random;
}
private static function get_width(){
return self::$width;
}
private static function get_height(){
return self::$height;
}
private static function generate_code($length){
self::session_start();
$code=null;
for($i=0;$i<$length;$i++){
$code.=self::get_random();
}
$_SESSION[self::$captcha] = $code;
self::$width = $length * self::$character_width;
return $code;
}
public static function image(){
$length = 6;
$code = self::generate_code($length);
self::set_font();
ob_start();
$image = imagecreatetruecolor(self::get_width(),self::get_height());
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, self::get_width(), self::get_height(), $white);
for($dot=0;$dot<2000;$dot++){
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$dot_color = imagecolorallocate($image, $red, $green, $blue);
$x1 = rand(0,self::get_width());
$y1= rand(0,self::get_height());
$x2 = $x1 +1;
$y2 = $y1 +1;
imageline($image, $x1, $y1, $x2, $y2, $dot_color);
}
for($start = $length*(-1); $start<0; $start++){
$color = imagecolorallocate($image, rand(0,177), rand(0,177), rand(0,177));
$character = substr($code, $start,1);
$x =($start+6)*self::$character_width;
$y = rand(self::get_height()-20, self::get_height() -10);
imagettftext($image, self::$font_size, 0, $x, $y, $color, self::$font, $character);
}
imagepng($image);
imagedestroy($image);
$source = ob_get_contents();
ob_end_clean();
unlink(self::$font);
return "data:image/png;base64,".base64_encode($source);
}
public static function get_code(){
self::session_start();
return $_SESSION[self::$captcha];
}
}
?>
index1.php
<?php
require_once("captcha.php");
if(isset($_POST["code"])){
if($_POST["code"]== captcha::get_code()){
echo "good";
}else{
echo "wrong";
}
}
?>
<form method="post">
<img src=" <?php captcha::image() ?>"/><br>
<input type="text" name="code"/><br>
<input type="submit" value="Check"/>
</form>

The methods end up returning the data but you never echo the output.
return "data:image/png;base64,".base64_encode($source);
Partially solved:
<img src="<?php echo captcha::image(); ?>"/><br>
Taken from the comments, next error is font path:
self::$font = self::$captcha;
Don't set em to the same value. $font must point to a font path (like ./arial.ttf).

Related

Make sure imagepng has written the file on the server

I have a function which adds text under a existing qr-code image.
In some cases the return is faster than the server has written the image on the filesystem, so that other function got issues.
How can I make sure that everything is done before I return the path to the image?
At the moment I am trying to use a while-loop, but I am also really unhappy with it. That causes also timeouts and crashes on my server (no idea why).
function addTextToQrCode($oldImage, $text)
{
$newImage = $oldImage;
$newImage = str_replace(".png", "_original.png", $newImage);
copy($oldImage, $newImage);
$image = imagecreatefrompng($oldImage);
$black = imagecolorallocate($image, 0, 0, 0);
$fontSize = 20;
$textWidth = imagefontwidth($fontSize) * strlen($text);
$textHeight = imagefontheight($fontSize);
$x = imagesx($image) / 2 - $textWidth / 2;
$y = imagesy($image) - $textHeight - 3;
imagestring($image, 5, $x, $y, $text, $black);
$filePath = "/qrCodes/qrcode".$text.'.png';
imagepng($image, $filePath);
while(!$this->checkIfNewQrCodeIsOnFileSystem($filePath)){
$this->checkIfNewQrCodeIsOnFileSystem($filePath);
}
return $filePath;
}
function checkIfNewQrCodeIsOnFileSystem($filePath) {
if (file_exists($filePath)) {
return true;
} else {
return false;
}
}
Check only imagepng(). The solution i would prefer.
function addTextToQrCode($oldImage, $text)
{
$newImage = $oldImage;
$newImage = str_replace(".png", "_original.png", $newImage);
copy($oldImage, $newImage);
$image = imagecreatefrompng($oldImage);
$black = imagecolorallocate($image, 0, 0, 0);
$fontSize = 20;
$textWidth = imagefontwidth($fontSize) * strlen($text);
$textHeight = imagefontheight($fontSize);
$x = imagesx($image) / 2 - $textWidth / 2;
$y = imagesy($image) - $textHeight - 3;
imagestring($image, 5, $x, $y, $text, $black);
$filePath = "/qrCodes/qrcode".$text.'.png';
if (imagepng($image, $filePath) === true) {
return $filePath;
}
}

Blank pages when trying to do captcha

Hello i am learning php now and developing website for my education. I am facing problem if i try to add captcha image. I don't know where is the problem but instead of working captcha i get blank page.
I even tried few already done captchas from github but get same problem i think the problem could be with fonts but i am not sure "ts probably my stupidity and i am doing something wrong :D" . Anyway if anyone can help me with it it would be great.
code i tried to use:
captcha.php
<?php
class captcha {
private static $captcha = "__captcha__";
public static $font;
private static $width = 70;
private static $height = 70;
private static $font_size = 40;
private static $character_width = 40;
private static function session_exists() {
return isset($_SESSION);
}
private static function set_font() {
self::$font = self::$captcha;
$AnonymousClippings ='there is inserted chars from font you can';
self::$font = tempnam(sys_get_temp_dir(), self::$captcha);
$handle = fopen(self::$font,"w+");
fwrite($handle,base64_decode($AnonymousClippings));
fclose($handle);
return self::$font;
}
private static function get_random() {
$type = rand(0,2);
switch($type) {
case 2:
$random = chr(rand(65,90));
break;
case 1:
$random = chr(rand(97,122));
break;
default:
$random = rand(0,9);
}
return $random;
}
private static function generate_code($length) {
$code = null;
for($i = 0; $i < $length; $i++) {
$code .= self::get_random();
}
if(self::session_exists()) {
$_SESSION[self::$captcha] = $code;
}
self::$width = $length * self::$character_width;
return $code;
}
private static function get_width() {
return self::$width;
}
private static function get_height() {
return self::$height;
}
public static function image() {
$length = 6;
$code = self::generate_code($length);
self::set_font();
ob_start();
$image = imagecreatetruecolor(self::get_width(),self::get_height());
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, self::get_width(), self::get_height(), $white);
for($dot = 0; $dot < 2000; $dot++) {
$r = rand(0,255);
$g = rand(0,255);
$b = rand(0,255);
$dot_color = imagecolorallocate($image, $r, $g, $b);
$x1 = rand(0, self::get_width());
$y1 = rand(0, self::get_height());
$x2 = $x1 + 1;
$y2 = $y1 + 1;
imageline($image, $x1, $y1, $x2, $y2, $dot_color);
}
for($start = - $length; $start < 0; $start++) {
$color = imagecolorallocate($image, rand(0,177), rand(0,177), rand(0,177));
$character = substr($code, $start, 1);
$x = ($start+6) * self::$character_width;
$y = rand(self::get_height() - 20, self::get_height() - 10);
imagettftext($image, self::$font_size, 0, $x, $y, $color, self::$font, $character);
}
imagepng($image);
imagedestroy($image);
$source = ob_get_contents();
ob_end_clean();
unlink(self::$font);
return "data:image/png;base64,".base64_encode($source);
}
public static function get_code() {
if(self::session_exists()) {
return $_SESSION[self::$captcha];
}
return rand();
}
}
index.php file
<?php
session_start();
require_once("captcha.php");
if(isset($_POST['rCaptcha'])) {
echo captcha::image();
exit;
}
else if(isset($_POST["code"])) {
if($_POST["code"] == captcha::get_code()) {
echo "Good";
}
else {
echo "Bad";
}
echo "<br/>";
}
?>
<script>
function refreshCaptcha(target) {
var req = new XMLHttpRequest();
req.open("POST", window.location, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
target.src = req.responseText;
}
}
req.send("rCaptcha=true");
}
</script>
<form method="post" autocomplete="off">
<fieldset>
<legend>PHP Captcha</legend>
<input type="text" name="code" placeholder="Captcha Code" /><br/>
<img src="<?= captcha::image() ?>" onclick="refreshCaptcha(this)"
title="click to refresh" /><br/>
<input type="submit" value="Check" /><br/>
</fieldset>
</form>
For a start add those two lines in the beginning of your index.php file (after <?php):
error_reporting(-1);
ini_set('display_errors', 'On');
Then you will see the errors produced by php. It will be much more easy to debug your code!
In this case a semicolon is missing in line 16 in your captcha.php file:
$AnonymousClippings ='there is inserted chars from font you can'

Simple captcha php script not displaying text on image

I was hoping to get in touch with someone on a situation that I cannot find the solution to anywhere.
I am trying to create a captcha on my website using php and although I was able to create an image and create the random captcha text.
I am unable to over lay the two. Here is my code:
<?PHP
session_start();
function generateRandomString($length = 10) {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
}
function security_image(){
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
//$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
// $image = #imagecreate($width, $height) or die('GD not installed');
global $image;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
//$textbox = imagettfbbox($font_size, 0, $font, $code);
//$textbox = imagettfbbox($font_size, 0, $randomString);
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
// imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);
$images = glob("*.png");
foreach ($images as $image_to_delete) {
#unlink($image_to_delete);
}
imagepng($image, "image" . $_SESSION['count'] . ".png");
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();
?>
I have no idea what I’m doing wrong. I’ve spent over 10 hours on this “display text” issue. I don’t understand and I am desperate for help. I even downloaded working captcha version from other resources that break once I upload it to my server. I have no idea whats going on. At first I thought there was something wrong with my server but the fact that I can even create the pixels, lines image means that it is at least working.
Please help!!!! 
UPDATE---------------------------------------------
Thank you for your suggestions. Here is the edited code. I'm still getting the same issue.
<?PHP
session_start();
function security_image(){
global $image;
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
/*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
*/
$x = ($width) / 2;
$y = ($height) / 2;
imagettftext($image,$font, 4, $x, $y, $word);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();?>
i made some suggested changes but the code seems to still do the same thing. Just display the lines and pixels as expected but the text still is missing... ?
There are some several "errors" in your functions, let's fix them:
In generateRandomString()
generateRandomString($length = 10)
$lenght is not used its scope.
$text_color = imagecolorallocate($image, 0, 0, 0);
$image is uninitialized
In security_image() function:
$textbox is uninitialized
$text_color and $word is uninitialized.
And Wrong parameter count for imagettftext() You add 7 parameters, and forget the font file parameter.
found the problem. using this:
http://php.net/manual/en/function.imagettftext.php
i was able to see that the font location was incorrect. using the example on that page and editing it to my needs worked.

How can I have a nice radial transparent gradient applied to an image with PHP?

How can one specify an image and apply a radial transparent gradient where it fades out radially. I do not have Imagemagick installed.
Marginal example:
Introduction
I think you should get Imagemagick installed because what you want is a simple vignette effect, You can easily so that with ImageMagic (convert input.jpg -background black -vignette 70x80 output.png) without having to loop every pixel which can be very slow when dealing with large images
Original Image
$file = __DIR__ . "/golf.jpg";
Effect 1
$image = new imagick($file);
$image->vignetteImage(20, 20, 40, - 20);
header("Content-Type: image/png");
echo $image;
Effect 2
$image = new imagick($file);
$image->vignetteImage(100, 100, 200, 200);
header("Content-Type: image/png");
echo $image;
vignette with GD
Well if you are forced to use GB ... Use can use this cool vignette script
function vignette($im) {
$width = imagesx($im);
$height = imagesy($im);
$effect = function ($x, $y, &$rgb) use($width, $height) {
$sharp = 0.4; // 0 - 10 small is sharpnes,
$level = 0.7; // 0 - 1 small is brighter
$l = sin(M_PI / $width * $x) * sin(M_PI / $height * $y);
$l = pow($l, $sharp);
$l = 1 - $level * (1 - $l);
$rgb['red'] *= $l;
$rgb['green'] *= $l;
$rgb['blue'] *= $l;
};
for($x = 0; $x < imagesx($im); ++ $x) {
for($y = 0; $y < imagesy($im); ++ $y) {
$index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($im, $index);
$effect($x, $y, $rgb);
$color = imagecolorallocate($im, $rgb['red'], $rgb['green'], $rgb['blue']);
imagesetpixel($im, $x, $y, $color);
}
}
return (true);
}
Faster GD vignette approach
A better approached used in GD Filter testing would be ... to create a mask and over lay
$overlay = 'vignette_white.png';
$png = imagecreatefrompng($overlay);
imagecopyresampled($filter, $png, 0, 0, 0, 0, $width, $height, $width, $height);
Full Code
Cool Demos Of the filter combination
The only disadvantage is that The image must be the same size with the mask for the effect to look cool
Conclusion
If this is what you mean by radial transparent gradient then i advice you to get ImageMagic if not at least the lady the picture is cute.
Thanks to the function linked by #Baba I was able to alter the script to allow for semi transparent vignette effect.
<?php
class PhotoEffect
{
private $_photoLocation;
private $_width;
private $_height;
private $_type;
private $_originalImage;
private $_afterImage;
/**
* Load image URL in constructor
*/
final public function __construct($photoLocation)
{
$this->_photoLocation = $photoLocation;
if (!$size = #getimagesize($this->_photoLocation)){
throw new Exception('Image cannot be handled');
}
$this->_width = $size[0];
$this->_height = $size[1];
$this->_type = $size[2];
switch ( $this->_type ) {
case IMAGETYPE_GIF:
$this->_originalImage = imagecreatefromgif($this->_photoLocation);
break;
case IMAGETYPE_JPEG:
$this->_originalImage = imagecreatefromjpeg($this->_photoLocation);
break;
case IMAGETYPE_PNG:
$this->_originalImage = imagecreatefrompng($this->_photoLocation);
break;
default:
throw new Exception('Unknown image type');
}
}
/**
* Destroy created images
*/
final private function __destruct() {
if (!empty($this->_originalImage))
{
imagedestroy($this->_originalImage);
}
if (!empty($this->_afterImage))
{
imagedestroy($this->_afterImage);
}
}
/**
* Apply vignette effect
*/
final public function Vignette($sharp=0.4, $level=1, $alpha=1)
{
if (empty($this->_originalImage))
{
throw new Exception('No image');
}
if (!is_numeric($sharp) || !($sharp>=0 && $sharp<=10))
{
throw new Exception('sharp must be between 0 and 10');
}
if (!is_numeric($level) || !($level>=0 && $level<=1))
{
throw new Exception('level must be between 0 and 10');
}
if (!is_numeric($alpha) || !($alpha>=0 && $alpha<=10))
{
throw new Exception('alpha must be between 0 and 1');
}
$this->_afterImage = imagecreatetruecolor($this->_width, $this->_height);
imagesavealpha($this->_afterImage, true);
$trans_colour = imagecolorallocatealpha($this->_afterImage, 0, 0, 0, 127);
imagefill($this->_afterImage, 0, 0, $trans_colour);
for($x = 0; $x < $this->_width; ++$x){
for($y = 0; $y < $this->_height; ++$y){
$index = imagecolorat($this->_originalImage, $x, $y);
$rgb = imagecolorsforindex($this->_originalImage, $index);
$l = sin(M_PI / $this->_width * $x) * sin(M_PI / $this->_height * $y);
$l = pow($l, $sharp);
$l = 1 - $level * (1 - $l);
$rgb['red'] *= $l;
$rgb['green'] *= $l;
$rgb['blue'] *= $l;
$rgb['alpha'] = 127 - (127 * ($l*$alpha));
$color = imagecolorallocatealpha($this->_afterImage, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
imagesetpixel($this->_afterImage, $x, $y, $color);
}
}
}
/**
* Ouput PNG with correct header
*/
final public function OutputPng()
{
if (empty($this->_afterImage))
{
if (empty($this->_originalImage))
{
throw new Exception('No image');
}
$this->_afterImage = $this->_originalImage;
}
header('Content-type: image/png');
imagepng($this->_afterImage);
}
/**
* Save PNG
*/
final public function SavePng($filename)
{
if (empty($filename)) {
throw new Exception('Filename is required');
}
if (empty($this->_afterImage))
{
if (empty($this->_originalImage))
{
throw new Exception('No image');
}
$this->_afterImage = $this->_originalImage;
}
imagepng($this->_afterImage, $filename);
}
}
/**
* How to use
*/
$effect = new PhotoEffect('test.jpg');
$effect->Vignette();
$effect->OutputPng();
?>
Working phpfiddle with the only image I could find on their server, so not that big.
I am aware that this is a PHP related question, but you can achieve nice transparent gradients with use of javascript and html5 canvas element.
So I wrote this small script that:
detects browsers that support canvas element, if browser doesn't support canvas (fortunately only a few percent of users are left) then full image is displayed.
creates canvas and appends element after image
arguments in the create_gradient() function can be changed for custom shapes
it works with all popular image formats ( tested with .jpg, .bmp, .gif, .png )
you can add more ‘colorstops’ ( grd.addColorStop() ) to change the flow of the gradient
script
window.onload = function() {
if ( typeof CanvasRenderingContext2D !== 'function' ) {
document.getElementById('gradient-image').style.visibility = "visible";
return;
}
var image = document.getElementById('gradient-image');
// these are the default values, change them for custom shapes
create_gradient( image, image.width/2, image.height/2, image.height/4, image.width/2, image.height/2, image.height/2 );
}
function create_gradient( image, start_x, start_y, start_r, end_x, end_y, end_r ){
var canvas = document.createElement('canvas');
var parent = image.parentNode;
if ( parent.lastchild == image ) parent.appendChild(canvas);
else parent.insertBefore(canvas, image.nextSibling);
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
var grd = context.createRadialGradient( start_x, start_y, start_r, end_x, end_y, end_r );
grd.addColorStop(0, 'rgba(0,0,0,1)' );
// grd.addColorStop(0.2, 'rgba(0,0,0,0.8)' );
grd.addColorStop(1, 'rgba(0,0,0,0)' );
context.fillStyle = grd;
context.fillRect(0, 0, image.width, image.height);
var grd_data = context.getImageData(0, 0, image.width, image.height);
context.drawImage( image, 0, 0);
var img_data = context.getImageData(0, 0, image.width, image.height);
var grd_pixel = grd_data.data;
var img_pixel = img_data.data;
var length = img_data.data.length
for ( i = 3; i < length; i += 4 ) {
img_pixel[i] = grd_pixel[i];
}
context.putImageData(img_data, 0, 0);
}
html
<img id="gradient-image"src="">
css
#gradient-image {
position: absolute;
visibility: hidden;
}
There is also a vignette filter available in this php library, which only uses GD:
https://github.com/elgervb/imagemanipulation

Imagettftext - incorrect line spacing

I have an problem with PHP function imagettftext. I have code for generating card images from database with text informations. And with some cards I have problem - words are written over each other (like here).
My code looks like this (font size changes depending on the length of the text)
$length = strlen($cardInfo->description);
if ($length < 15) {
$divide = 15;
$fontSize = 16;
$lineHeight = 25;
$startPos = 220;
} else if ($length < 70) {
$divide = 25;
$fontSize = 12;
$lineHeight = 18;
$startPos = 210;
} else if ($length < 110) {
$divide = 28;
$fontSize = 10;
$lineHeight = 14;
$startPos = 210;
} else {
$divide = 38;
$fontSize = 8;
$lineHeight = 13;
$startPos = 210;
}
$description = wordwrap($cardInfo->description, $divide, ">>>");
$lines = explode(">>>", $description);
$count = 0;
foreach ($lines as $line) {
$position = $count * $lineHeight;
$count++;
imagettftext($image, $fontSize, 0, 28, ($startPos + $position), $black, $font, $line);
}
and the text in the database looks like this:
Oblehací stroj
Imunita vůči střelám /Tato jednotka je imunní vůči střeleckým zraněním/
Other problem is with the line wrapping: here. I don't know why the word "jídlo" is on the next line.
Thank you for any answers!
Long time ago, I have written a quite complex class to archive a similar task.
I don't have this code anymore, but the steps are fairly simple.
First: Don't rely on the calculations, php does on rare fonts.
Php's wordwrap-function is senseless here, because you deal with a charset-width (e.g. tracking) unknown to php. Wordwrap assumes, that all characters have the same char-width.
So, you have to build your own wordwrap-function using imagettfbbox. Then, you'll have to determine the size of the lowercase "x"-letter and the uppercase "X"-letter. These letters are the norm to calculate your own line-height/line-spacing. I also recommend you to manually separate words, since PHP does not always recognize the white-space-width correctly.
Hope this could help you...
This works for me. You need arial.ttf and http://dark-project.cz/CardDatabase/cards/lehky_katapult.png.
class Test_Canvas {
protected $res=null;
public function __construct($width, $height) {
$this->res = imagecreatetruecolor($width, $height);
imagealphablending($this->res, true);
imagesavealpha($this->res, true);
imagefill($this->res, 0, 0, imagecolorallocatealpha($this->res, 0, 0, 0, 127));
}
public function copyTo(Test_Canvas $canvas, $x, $y) {
imagecopyresampled($canvas->res, $this->res, $x, $y, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
}
public function getWidth() {
return imagesx($this->res);
}
public function getHeight() {
return imagesy($this->res);
}
public function saveAsPNG() {
imagepng($this->res);
}
}
class Test_Canvas_Image_PNG extends Test_Canvas {
public function __construct($filename) {
$res = imagecreatefrompng($filename);
$w = imagesx($res);
$h = imagesy($res);
parent::__construct($w, $h);
imagecopymerge($this->res, $res, 0, 0, 0, 0, $w, $h, 100);
}
}
class Test_Canvas_Textarea extends Test_Canvas {
private $text;
private $fontsize;
private $fontfile;
public function __construct($width, $height, $text, $fontsize, $fontfile) {
parent::__construct($width, $height);
$this->text = $text;
$this->fontsize = $fontsize;
$this->fontfile = $fontfile;
$this->removeDuplicateWhitespace();
$this->formatText();
$this->applyText();
}
private function removeDuplicateWhitespace() {
$this->text = preg_replace('/[ \t]+/', ' ', $this->text);
}
private function formatText() {
$lines = explode("\n", $this->text);
$res = array();
foreach ($lines as $line) {
$res[] = $this->insertAdditionalLinebreaks($line);
}
$this->text = join("\n", $res);
}
private function insertAdditionalLinebreaks($line) {
$words = $this->splitWords($line);
$res = array();
$line = "";
while(count($words)) {
$word = array_shift($words);
$testLine = "{$line} {$word}";
$width = $this->getTextWidth($testLine);
if($width > $this->getWidth()) {
$res[] = $line;
$line = $word;
} elseif(!count($words)) {
$res[] = $testLine;
} else {
$line = $testLine;
}
}
return join("\n", $res);
}
private function getTextWidth($text) {
$boundaries = imagettfbbox($this->fontsize, 0, $this->fontfile, $text);
$x1 = min($boundaries[0], $boundaries[6]);
$x2 = max($boundaries[2], $boundaries[4]);
return $x2 - $x1;
}
private function splitWords($text) {
return explode(' ', $text);
}
private function applyText() {
$lines = explode("\n", $this->text);
foreach($lines as $lineNo => $line) {
imagettftext($this->res, $this->fontsize, 0, 0, ($lineNo + 1) * ($this->fontsize + 5), imagecolorallocate($this->res, 0, 0, 0), $this->fontfile, $line);
}
}
}
$rootPath = dirname(__FILE__).'/';
$imageFilename = "{$rootPath}test.png";
$description = "Oblehací stroj\nImunita vuci strelám /Tato jednotka je imunní vuci streleckým zranením/ ";
$description .= $description;
$description .= $description;
header('Content-Type: image/png');
$canvas = new Test_Canvas_Image_PNG($imageFilename);
$text = new Test_Canvas_Textarea(179, 92, $description, 9, 'arial.ttf');
$text->copyTo($canvas, 25, 193);
$canvas->saveAsPNG();
You shouldn't use such estimates for the box you'll need.
The function imagettfbbox() can give you a definite answer to the box you'll need to display the text.
http://nl3.php.net/manual/en/function.imagettfbbox.php
Hope that helps.

Categories