作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在托管一个充满图像的网络服务器。这些是通过 php 以编程方式加载的。为了防止加载 100 个 10mb 文件,我制作了缩略图。如果我现在想上传新图片,我需要手动制作缩略图。我可以在将它上传到服务器后自动执行此操作,或者在 php 意识到拇指丢失后自动执行此操作吗?
如果您需要我目前拥有的代码:
function getThumb($file)
{
//Strip the file down to its name.
//$suffix = pathinfo($file,PATHINFO_EXTENSION); //Depricated.
//$filename = basename($file); //Depricated.
$filename = pathinfo($file,PATHINFO_FILENAME);
$thumbpath = "imgres/posts/thumb/";
$thumbnail = $thumbpath.$filename.".jpg";
return $thumbnail;
}
//Image loader
//Folder containing images.
$filedir = "imgres/posts/";
//Retrieve the images in the folder.
$images = glob($filedir."*.{jpg,JPG,png,PNG,gif,GIF}",GLOB_BRACE);
//Make sure the image array is not empty, or null.
if (!empty($images))
{
//Load the images into the website.
foreach ($images as $image)
{
if (pathinfo($image,PATINFO_EXTENSION) == "gif" || pathinfo($image, PATHINFO_EXTENSION))
{
echo '<a href="'.$image.'"><img src="'.$image.'"/></a>';
}
else
{
echo '<a href="'.$image.'"><img src="'.getThumb($image).'"/></a>';
}
}
}
else
{
//Write out an error message to warn the user.
echo "<p>No images were found in the server. This is most likely an error in the PHP code, incompatibility, or something went wrong with our storage solution. Contact admin! Information needed: Browser, OS, and has the site worked before?</p>";
}
最佳答案
我找到了 claviska 的一个叫做 simpleimage 的类。我的代码现在已经完成了。一切看起来像这样。
处理缩略图的代码:
function isThumb($thumbnail)
{
return file_exists($thumbnail);
}
function makeThumb($original, $destination)
{
$img = new abeautifulsite\SimpleImage($original);
$img->fit_to_width(256);
$img->save($destination);
}
?>
加载图片的代码:
<?php
//Image loader
//Folder containing images.
$filedir = "imgres/posts/";
//Retrieve the images in the folder.
$images = glob($filedir."*.{jpg,JPG,png,PNG,gif,GIF}",GLOB_BRACE);
//Make sure the image array is not empty, or null.
if (!empty($images))
{
//Load the images into the website.
foreach ($images as $image)
{
if (pathinfo($image,PATINFO_EXTENSION) == "gif" || pathinfo($image, PATHINFO_EXTENSION)=="GIF")
{
echo '<a href="'.$image.'"><img src="'.$image.'"/></a>';
}
else
{
echo '<a href="'.$image.'"><img src="'.getThumb($image).'"/></a>';
}
}
}
else
{
//Write out an error message to warn the user.
echo "<p>No images were found in the server. This is most likely an error in the PHP code, incompatibility, or something went wrong with our storage solution. Contact admin! Information needed: Browser, OS, and has the site worked before?</p>";
}
?>
关于php - 我怎样才能在网络服务器上自动生成缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097802/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!