问题描述
我们如何减小图像大小,即在不降低图像质量和比例(高度和宽度)的情况下使其达到100kb左右?如果您有任何示例代码,请帮助我。
解决方法
Laravel本身不支持图像优化,但是很容易集成外部库,例如 spatie 的 laravel-image-optimizer 。
您可以通过调用优化函数来使用此库。
void Main()
{
var doc1Reader = new StringReader("<WELL_SET xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <WellBore> <APINumber>42113</APINumber> <MetaData> <Data_Set>TX</Data_Set> </MetaData> <WBRoot> <RecordId>012</RecordId> </WBRoot> </WellBore> <WellBore> <APINumber>32145</APINumber> <MetaData> <Data_Set>DI</Data_Set> </MetaData> <WBRoot> <RecordId>033</RecordId> </WBRoot> </WellBore></WELL_SET>");
var doc1 = XDocument.Load(doc1Reader);
// just grab it when you are visiting that node
var list = doc1.Descendants("WellBore").Select(e => new KeyValuePair<string,string>(e.Element("APINumber").Value,e.XPathSelectElement("./MetaData/Data_Set").Value));
foreach (var i in list) {
Console.WriteLine($"{i.Key} {i.Value}");
}
// use a fancy XPath:
List<XElement> wellBoreApi = doc1.Descendants("WellBore").Elements("APINumber").ToList();
foreach (var apiItem in wellBoreApi)
{
// what this XPath does:
// extracts value of /Metadata/Data_Set of ALL WellBore elements that have a child element with inner text = apiItem.Value
XElement xnList = doc1.XPathSelectElement($"//WellBore[APINumber[text()=\"{apiItem.Value}\"]]/MetaData/Data_Set");
Console.WriteLine($"{apiItem.Value} {xnList.Value}");
}
}