Gem genereret img
Har fundet noget php kode på nettet der kan lave et png billede ud fra en txt fil.Men det er den uploaded txt fil der bliver gemt, og ikk png billedet
Har prøvet mig frem for at få den til at gemme det genereret billede frem for at generer billedet ud fra txt filen, hvergang det skal vises.
Den her generer png billed
[code]
<?php
/*
Txt to img render
*/
if( isset($_REQUEST['file']) )
{
$uploadname = $_REQUEST['file'];
//What is the maximum filesize to be viewed? In bytes - Default: 40KB
$max_size=500960;
//Which filetypes are allowed?
$allow[0] = "txt";
$extentie = substr($uploadname, -3);
for ($i = 0; $i < count($allow); $i++)
{
if ($extentie == $allow[$i])
{
$extentie_check = "ok";
$i = count($allow) + 5;
}
}
if ($extentie_check)
{
//check size here
$file_size = filesize($_REQUEST['file']);
if($file_size >= $max_size){
echo "ERROR!";
}
else {
// Load the txt file...
$lines = file( $_REQUEST['file'] );
// Get the longest line....
$longestLine = 0;
$numberLines = 0;
foreach ($lines as $line_num => $line)
{
$line = rtrim($line);
$tempLineLength = strlen($line); // Avoid calling strlen twice...
if( $tempLineLength > $longestLine )
$longestLine = $tempLineLength;
}
// The number of line....
$numberLines = count($lines);
//Set up the Vars...
$fontWidth = 8;
$fontHeight = 12;
$border = 1; // Not one pixel, but 1*FontWidth and 1*FontHeight
$imgWidth = ($fontWidth * $longestLine) + ( 2 * $border * $fontWidth );
$imgHeight = ($fontHeight * $numberLines) + ( 2 * $border * $fontHeight );
$currentX = 0;
$currentY = 0;
// Start GD
$img = imagecreatetruecolor( $imgWidth, $imgHeight );
if ($_REQUEST['color']==1) $charMap = imagecreatefrompng( "txt_2.png" );
else $charMap = imagecreatefrompng( "txt_2.png" );
for($j = 0; $j < $numberLines + 2 * $border; $j++ )
{
// Avoid calling strlen strlen($line) times :-)
$currentLineLength = strlen($line);
for( $i = 0; $i < $longestLine + 2 * $border; $i++ )
{
// int imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h)
imagecopy( $img, $charMap, $currentX, $currentY, ($fontWidth * 12), ($fontHeight * 0), $fontWidth, $fontHeight );
// Move along one char to the right
$currentX = $currentX + $fontWidth;
}
// Move down a line, and reset the x location
$currentX = 0;
$currentY = $currentY + $fontHeight;
}
$currentX = $border * $fontWidth;
$currentY = $border * $fontHeight;
// Main loop....
foreach ($lines as $line_num => $line)
{
// Avoid calling strlen strlen($line) times :-)
$currentLineLength = strlen($line);
for( $i = 0; $i < $currentLineLength; $i++ )
{
// Get the Locatino of the char in the charmap image
$charYOffSet = 0;
$charXOffSet = ord($line[$i]); // - 20; // Becuase my charmap skips the first 20 chars :-)
while( $charXOffSet >= 20 )
{
$charYOffSet++;
$charXOffSet = $charXOffSet - 20;
}
// int imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h)
imagecopy( $img, $charMap, $currentX, $currentY, ($fontWidth * $charXOffSet), ($fontHeight * $charYOffSet), $fontWidth, $fontHeight );
// Move along one char to the right
$currentX = $currentX + $fontWidth;
}
// Move down a line, and reset the x location
$currentX = ($border * $fontWidth);
$currentY = $currentY + $fontHeight;
}
// Display the PNG file...
header ("Content-type: image/png");
imagepng( $img );
imagedestroy($img);
}
}
else
{
echo "ERROR!";
}
}
else
{
echo "ERROR!";
}
?>
[/code]
Og den her gemmer txt filen på serveren.
[code]
<?php
/*
Txt to render uploader
*/
global $_FILES;
$filename = $_FILES['userfile']['name'];
//What is the maximum filesize to be uploaded? In bytes - Default: 40KB
$max_size=40960;
//Which filetypes are allowed?
$allow[0] = "txt";
if ($_POST["newname"])
$uploadname = $_POST['newname'];
else
$uploadname = $_FILES['userfile']['name'];
$extentie = substr($uploadname, -3);
for ($i = 0; $i < count($allow); $i++)
{
if ($extentie == $allow[$i])
{
$extentie_check = "ok";
$i = count($allow) + 5;
}
}
if ($extentie_check)
{
$file_size = filesize($_FILES['userfile']['tmp_name']);
if($file_size >= $max_size){
echo "ERROR!";
}
else {
$time = time();
$unique = $time . $uploadname;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
move_uploaded_file($_FILES['userfile']['tmp_name'], "upload/" . $unique);
$location = "upload/" . $unique;
echo "txt File Uploaded to: <a href='txt2Png.php?file=$location'>$unique</a><br />";
echo "<img src='txt2png.php?file=$location' />";
}
}
}
else
{
echo "ERROR!";
}
?>
[/code]
Det jeg gerne ville var at der blev generert et billede som så blev gemt på serveren, og ikk txt filen som bliver gemt.
Og at det er billedet der så bliver linket til :)
Håber at der er en haj der kan hjælpe mig, for jeg ar gået helt kold :'(
Hansen87