如何用PHP制作缩略图

时间:2009-10-06 13:17:45

标签: php

我只是想知道如何制作存储在硬盘中的图像的缩略图并在html页面中使用它们,我还需要缩略图才能放大(到原来的大小),如果点击最好在div标签内同一页,如果有人能把我放在正确的方向,我将不胜感激

感谢

8 个答案:

答案 0 :(得分:5)

您需要启用GD扩展程序。以下代码将在JPEG,PNG和GIF文件的名为~tmb的子目录中创建缩略图文件:

$invalid = true;
if ($file != '.' and $file != '..') {
    if (filetype($path_abs.$file) == "file") {
        $ext = strtolower(substr($file,strrpos($file,'.')+1));
        if ($ext == 'jpg' || $ext == 'jpeg') {
            $origimg = @imagecreatefromjpeg($path_abs.$file);
        } elseif ($ext == 'png') {
            $origimg = @imagecreatefrompng($path_abs.$file);
        } elseif ($ext == 'gif') {
            $origimg = @imagecreatefromgif($path_abs.$file);
        }
        if ($origimg !== false) {
            $nheight = 0;
            $nwidth = 0;
            $use_orig = false;
            if ($width<=160 and $height<160) {
                $nwidth = $width;
                $nheight = $height;
                $use_orig = true;
                $invalid = false;
            } else {
                if ($width>$height and $width>0) {
                    $nheight = intval((160 / $width) * $height);
                    $nwidth = 160;
                } elseif ($height>0) {
                    $nwidth = intval((160 / $height) * $width);
                    $nheight = 160;
                } else {
                    $image = false;
                }
                if ($nheight > 0 and $nwidth > 0) {
                    $newimg = imagecreatetruecolor($nwidth, $nheight);
                    $bgc = imagecolorallocate ($newimg, 238, 238, 238);
                    imagefilledrectangle ($newimg, 0, 0, $nwidth, $nheight, $bgc);
                    if (@imagecopyresampled($newimg, $origimg, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)) {
                        $image = imagejpeg($newimg, $path_abs.'~tmb/'.$file);
                        $invalid = false;
                    } elseif (@imagecopyresized($newimg, $origimg, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)) {
                        $image = imagejpeg($newimg, $path_abs.'~tmb/'.$file);
                        $invalid = false;
                    }
                }
            }
        }
    }
}
if (!$invalid) {
    if ($use_orig) {
        echo '<img src="'.$file.'" alt="" />';
    } else {
        echo '<img src="~tmb/'.$file.'" alt="" />';
    }
} else {
    echo '<p>Error for file '.$file.'</p>';
}

在上面的代码中,它将它们的大小调整为160x160,但保持宽高比。

答案 1 :(得分:3)

gd库允许您操作图像。 您将找到一篇文章来生成缩略图here

如果您希望允许用户查看缩略图和原始大小,最好的方法是保留两个版本。 并显示其中一个。

答案 2 :(得分:1)

如此简单,如果您有任何疑问,请发送电子邮件至karthid@in.com

$ffmpeg = "ffmpeg Installed path"

$flvfile = "source video file with root path"

$png_path " "Destination video file with root path and file type"

exec("$ffmpeg -y -i $flvfile -vframes 1 -ss 00:01:60 -an -vcodec png -f rawvideo -s 110x90 $png_path");
一切都好......

答案 3 :(得分:0)

您可以使用PHP中的GD库来加载和调整图像大小以生成缩略图

http://us.php.net/manual/en/image.examples.php

答案 4 :(得分:0)

查找PECL扩展Imagick。它通常可以安装标准的包管理器。

http://se2.php.net/Imagick

您可以动态创建缩略图并使用.php文件(慢速)提供缩略图,也可以制作存储在服务器上的缩略图副本(首选)

答案 5 :(得分:0)

我找到的最好的方法是使用phpThumb类(http://phpthumb.sourceforge.net/)。

它拥有您需要的所有内容,包括缓存,过滤器,水印和其他很酷的东西。只需看看演示页面。

答案 6 :(得分:0)

maxImageUpload对于使用原始图像创建缩略图,普通图像非常有用。

您可以使用jQuery放大缩略图。

答案 7 :(得分:0)

根据我的经验,GD对于大型图像并不具有记忆效果,因此我强烈建议您使用Imagick。我使用Imagick库为thumbnail generation with php编写了代码片段。您可以修改它以保存图像,而不是使用http://php.net/manual/en/imagick.writeimage.php

回显它