2011-08-15 92 views
1

我有一个jpegs文件夹和一个我希望用来管理它们的MySQL数据库。该数据库有一个表:'图像'和7个字段:'imgID','imgTitle','imgURL','imgDate','imgClass','imgFamily',&'imgGender'。主键是'imgID',索引键是'imgDate'。使用PHP从一个图像文件夹创建缩略图

我想要做的是创建一个PHP文件,它将通过我的图像文件夹(所有jpeg),并创建它们的缩略图,然后可以在显示链接到我的网页上的图像时使用它。由于我将来会在该文件夹中添加更多图像,因此防止代码创建已创建缩略图的图像的双倍将是一件好事。

我遇到的所有文献都建议使用gd图像库来做到这一点,但我愿意接受建议。

因为我是MySQL和PHP的新手,我希望有人可以帮我解决这个问题。我尝试过的所有东西都失败了。

图像所在的目录是new_arrivals_img /相对于网站根目录,并且缩略图应放置在new_arrivals_img /缩略图/相对于网站根目录。

现在我正在建设该网站,因此使用MAMP在本地托管它。我有一些问题想出我的图像的相对路径。有没有办法将new_arrivals_img /设置为根?

+1

使用'在命令行上convert'(Imagemagik的一部分),它可以做批量转换为你。用这种方法做起来要比尝试将脚本拼凑起来要容易得多:http://www.imagemagick.org/script/convert.php –

+0

有两个问题。我想知道在我的页面上创建链接时,php如何知道哪个缩略图属于哪个图像。这个解决方案是否有这个条件?或者,这将在php文件中完成,该文件将加载网站上的图像?其次,当我将更多图像添加到文件夹时会发生什么?它会创建重复的缩略图吗? – stefmikhail

+1

使用子目录。 '1.jpg'原始图像进入'thumbs/1.jpg'。 –

回答

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

+0

我该如何去分配php.ini中的内存?在使用MAMP作为本地主机的情况下是否发生了这种变化?最后,此代码是否允许在动态创建链接时使用缩略图和图像之间的某种连接? – stefmikhail

+1

MAMP中的php.ini文件在(我认为)应用程序/ MAMP/conf/php5 /我有MAMP,而不是MAMP PRO,所以如果你使用PRO,你必须检查手册。在你的php.ini文件中找到'memory_limit'并将其改为64M之类的东西,然后重新启动MAMP。此外,这根据您所需的宽度进行比例调整大小(示例调用中为100px) –

+0

嘿,它工作!现在,有一件事......质量真的不是很好。我开始认为用photoshop或类似的东西创建缩略图可能是一个更好的主意。最终我想显示缩略图作为使用php创建页面的较大图像的链接。使用上面的脚本通过Photoshop这个有什么好处吗?还是它有更多的文件名是相同的?我感谢您的帮助。 – stefmikhail

0

我根本不会使用gd,我会使用imagemagick。缩略图看起来会更好。

+0

真的吗?它有多大的不同? – stefmikhail

+0

然后让我问你。使用PHP来创建缩略图是一个好方法吗?它会允许它们将文件用作更大图像的链接吗?否则我可以在photoshop中创建缩略图。 – stefmikhail