生成文本文件的缩略图

问题描述 投票:0回答:4

假设用户上传.txt或.php文件,我想为它生成一个.png缩略图。有没有一种简单的方法,这不需要我打开文件并将其内容写入新的.png?我有ImageMagick和FFmpeg可用,必须有一种方法来利用它,但我一直在寻找很多,但没有运气。

提前致谢。

php ffmpeg imagemagick thumbnails
4个回答
2
投票

你总是可以使用php的imagettftext函数。

它将为您提供文本文件中的内容的表示。

http://php.net/manual/en/function.imagettftext.php


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。
  • 文本不会自动换行,因此您必须为文本添加换行符以适合您的输出视频大小。

0
投票

您可以使用PHP类qazxsw poi将文件转换为图像。它很适合txt文件。

imagick

0
投票
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");
© www.soinside.com 2019 - 2024. All rights reserved.