2013-04-02 103 views
0

假設用戶上傳.txt或.php文件,並且想爲其生成.png縮略圖。有沒有一種簡單的方法,不需要我打開文件並將其內容寫入新的.png中?我有ImageMagick和FFmpeg可用,必須有一種方法來利用這一點,但我一直在尋找很多,但沒有運氣。生成文本文件的縮略圖

在此先感謝。

+0

我見過很多關於如何將各種圖像格式轉換爲縮略圖的內容。但是我一直無法找到任何關於轉換.txt文件的信息,但我確信它可以完成。縮略圖對於文本文件來說不會很有意義。 – dmikester1

+0

確實如此,但它可以給出文本長度和密度的概念。 – Sophivorus

+0

我發現這個:http://stackoverflow.com/questions/3826379/image-magick-converting-text-to-image-is-there-a-way-to-center-the-text-to-t – dmikester1

回答

0

您可以使用PHP的imagick類文件轉換爲圖像。 它會很好地爲txt文件工作。

try 
{ 
    $im = new imagick("inputfile.txt[0]"); 
    if ($im) 
    { 
     $im->setImageAlphaChannel(imagick::ALPHACHANNEL_DEACTIVATE); 
     $im->setImageFormat('jpg'); 
     $im->setImageCompressionQuality(85); 
     file_put_contents("outfile.jpg",$im->getImageBlob()); 
    } 
} catch(Exception $e) 
{ 
    echo "cannot process"; 
} 

// When imagick is unable to read the file, it may wrongly 
// set internal server error 500 status code. 
// I do not understand why that happens, because the script 
// continues normally. Anyway, lets overwrite it here for all cases. 
header("HTTP/1.1 200 OK"); 
2

您可以使用ffmpeg

ffmpeg -video_size 640x480 -chars_per_frame 60000 -i in.txt -frames:v 1 out.png 

enter image description here

但是,它也有一些注意事項:

  • 默認情況下,它呈現每幀6000個字,所以它可能不要畫出所有的文字。您可以使用-chars_per_frame和/或-framerate輸入選項對此進行更改。默認幀速率爲25.

  • 文本不會自動換行,因此您必須爲文本添加換行符以適合您的輸出視頻大小。