Hello Guys ,

I want to create a script that convert a text file or Nfo file to an image(format is .PNG).
What it does is simply load the txt and output it as an image
Here is my code:

<?php
if(isset($_FILES['image'])){
    $errors = array();
    $allowed_ext = array('txt');
    $file_name     = $_FILES['image']['name']; 
    $file_ext     = strtolower(end(explode('.', $file_name)));
    $path =  $_SERVER['DOCUMENT_ROOT'].'/fileupload'.'/images/';
    if(in_array($file_ext, $allowed_ext) ===false){
        $errors [] = 'Extension not allowed.';
    }
    if ($file_ext =='txt') {
    $font_file = "C:/WINDOWS/Fonts/cour.ttf";
    $nfo_file_lines = file($file_name);
    $width = 0;
    for($i = 0; $i < count($nfo_file_lines); $i++) {
        $box = imagettfbbox($file_size, 0, $font_file, $nfo_file_lines[$i]);
        $width = max($width, $box[2]);
    }
    $image = imagecreate($width, $file_size * (count($nfo_file_lines) + 1));
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);
    for($i = 0; $i < count($nfo_file_lines); $i++) {
        imagettftext($image, $file_size, 0, 0, $file_size * ($i + 1), $text_color, $font_file, $nfo_file_lines[$i]);
    }
    imagepng($image);         //show image
    imagepng($image,$path);  //saves generated png in $path
    imagedestroy($image);    //des
}   

    }
    else{
     foreach($errors as $error){
        echo 'Contact WebMaster as only he can fix:D', '<br />';
     }
    }       
?>

However i am getting there errors , please help me out

Warning: file(1337day.txt) [function.file]: failed to open stream: No such file or directory in E:\AppServ\www\fileupload\test\upload.php on line 14

Warning: imagecreate() [function.imagecreate]: Invalid image dimensions in E:\AppServ\www\fileupload\test\upload.php on line 20

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 21

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 22

Warning: imagettftext() expects parameter 1 to be resource, boolean given in E:\AppServ\www\fileupload\test\upload.php on line 24

Warning: imagepng(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 26

Warning: imagepng(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 27

Warning: imagedestroy(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 28

Thanks a ton guys

Member Avatar for diafol

Warning: file(1337day.txt) [function.file]: failed to open stream: No such file or directory in E:\AppServ\www\fileupload\test\upload.php on line 14

The file doesn't exist OR more probable, you haven't put the right filepath/details for the file.
I'd use:

$fileinfo = pathinfo($file);
$file_ext = $fileinfo['extension'];

Also from what I can see, you haven't moved it to your uploads folder. The file only exists temporarily. If you don't want to upload the file to a folder, I think you can use a reference to the temp file itself.

Warning: imagecreate() [function.imagecreate]: Invalid image dimensions in E:\AppServ\www\fileupload\test\upload.php on line 20

I can't see $file_size referenced anywhere before line 19

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in E:\AppServ\www\fileupload\test\upload.php on line 21

Errors following mostly to do with $image - which can't be an image if it's not created properly due to preceding error.

my aim is to load the txt file temporarily , then stores it in $path and then destroy temporary file

<?php
if(isset($_FILES['image'])){
    $errors = array();
    $allowed_ext = array('txt');
    $file_name     = $_FILES['image']['name']; 
    $file_ext     = strtolower(end(explode('.', $file_name)));
    $file_size = $_FILES['image']['name']['size'];
    $path =  $_SERVER['DOCUMENT_ROOT'].'/fileupload'.'/images/';
    if(in_array($file_ext, $allowed_ext) ===false){
        $errors [] = 'Extension not allowed.';
    }
    if ($file_ext =='txt') {
    $font_file = "C:/WINDOWS/Fonts/cour.ttf";
    $nfo_file_lines = file($file_name);
    $width = 0;
    for($i = 0; $i < count($nfo_file_lines); $i++) {
        $box = imagettfbbox($file_size, 0, $font_file, $nfo_file_lines[$i]);
        $width = max($width, $box[2]);
    }
    $image = imagecreate($width, $file_size * (count($nfo_file_lines) + 1));
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);
    for($i = 0; $i < count($nfo_file_lines); $i++) {
        imagettftext($image, $file_size, 0, 0, $file_size * ($i + 1), $text_color, $font_file, $nfo_file_lines[$i]);
    }
    imagepng($image);         //show image
    imagepng($image,$path);  //saves generated png in $path
    imagedestroy($image);    //destroy temp image
}   

    }
    else{
     foreach($errors as $error){
        echo 'Contact WebMaster as only he can fix:D', '<br />';
     }
    }       
?>

What u mean by reference to temp file itself
Modify this code and add this referance for me please.
Thanks

Member Avatar for diafol

What u mean by reference to temp file itself
Modify this code and add this referance for me please.

No. As a programmer, that's your job. I'll give you an example:

<?php
    if(isset($_FILES['file'])){
        $file = $_FILES['file']['tmp_name'];
        echo file_get_contents($file);
    }
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="go" />

</form>

Reference the tmp_file NOT the file itself

I got error

lol.i just noticed that i missed a line
I remembered it by your example
Thanks
Case solved:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.