We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,154 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Getting error when trying to convert text to an image

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

3
Contributors
5
Replies
15 Hours
Discussion Span
10 Months Ago
Last Updated
6
Views
Question
Answered
Khav
Junior Poster
103 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,662 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

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

Khav
Junior Poster
103 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,662 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

I got error

pursefocus
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Khav
Junior Poster
103 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 10 Months Ago by diafol and pursefocus

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0936 seconds using 2.73MB