假设用户上传.txt或.php文件,我想为它生成一个.png缩略图。有没有一种简单的方法,这不需要我打开文件并将其内容写入新的.png?我有ImageMagick和FFmpeg可用,必须有一种方法来利用它,但我一直在寻找很多,但没有运气。
提前致谢。
您可以使用PHP类qazxsw poi将文件转换为图像。它很适合txt文件。
imagick
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");