我有一个jpeg文件夹,以及一个我希望用来管理它们的MySQL数据库。数据库有一个表:'images'和7个字段:'imgID','imgTitle','imgURL','imgDate','imgClass','imgFamily',& 'imgGender'。主键是'imgID',索引键是'imgDate'。
我想要做的是创建一个PHP文件,它将遍历我的图像文件夹(所有jpeg),并创建它们的缩略图,然后可以在显示我网页上图像的链接时使用。由于我将来会在文件夹中添加更多图像,因此最好防止代码创建已经创建了缩略图的双倍图像。
我遇到的所有文献都建议使用gd图像库来执行此操作,但我愿意接受建议。
由于我是MySQL和PHP的新手,我希望有人能帮我解决这些问题。我尝试的一切都失败了。
图像所在的目录是new_arrivals_img /相对于站点根目录,缩略图应放在new_arrivals_img / thumbnails /相对于站点根目录。
现在我正在构建网站,因此使用MAMP在本地托管它。我在弄清楚我的图像的相对路径时遇到了一些问题。有没有办法将new_arrivals_img /设置为root?
答案 0 :(得分:4)
据说ImageMagick在记忆方面更胜一筹,但我总是拥有GD,而且它总是为我完成这项工作。确保在php.ini中分配足够的内存
然后使用这样的脚本:
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("new_arrivals_img/","new_arrivals_img/thumbnails/",100);
?>
http://www.webcheatsheet.com/php/create_thumbnail_images.php
答案 1 :(得分:0)
我根本不会使用gd,我会使用imagemagick。缩略图看起来会好很多。