Php 生成8000+的缩略图;图像
不久前,我和我的一些朋友建立了我们自己的文件托管/共享服务,我们在内部(私下)使用。这不是一个公共服务器,也不是专门为我们设计的 整个项目都基于以下规格Php 生成8000+的缩略图;图像,php,gd,Php,Gd,不久前,我和我的一些朋友建立了我们自己的文件托管/共享服务,我们在内部(私下)使用。这不是一个公共服务器,也不是专门为我们设计的 整个项目都基于以下规格 处理器:AMD Phenom II X4(型号:955)@3.20 Ghz 内存:8 GB 操作系统:64位Windows Vista家庭高级版 硬盘驱动器:x2 1 TB SATA 处理这些文件需要一点时间,更不用说php.ini必须重写很多,允许脚本占用近1GB的内存,并将执行时间延长到400秒 问题:那么规范足以处理负载,但有没有更好
- 处理器:AMD Phenom II X4(型号:955)@3.20 Ghz
- 内存:8 GB
- 操作系统:64位Windows Vista家庭高级版
- 硬盘驱动器:x2 1 TB SATA
那么规范足以处理负载,但有没有更好的方法来优化下面提供的代码,使其能够在8000多个映像中运行,而不会严重滞后于系统?更不用说在浏览了500多个文件后停止了。 下面是实际脚本的代码
function make_thumb($src,$dest,$desired_width) {
/* read the source image */
$de_width = 100;
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest,75);
}
function get_files($images_dir,$exts = array('jpg','png','gif','jpeg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
/* function: returns a file's extension */
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
$images_dir = 'data/'.$key.'/photos/';
$thumbs_dir = 'data/'.$key.'/photos/f/';
$thumbs_width = 100;
$images_per_row = 6;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
fm_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<a href="',$images_dir.$file,'" rel="gallery"><img src="',$thumbnail_image,'" class="photo-link"/></a>';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}
函数make_thumb($src、$dest、$desired_width){
/*读取源图像*/
$de_宽度=100;
$source_image=imagecreatefromjpeg($src);
$width=imagesx($source\u image);
$height=imagesy($source\u image);
/*找到此缩略图相对于所需宽度的“所需高度”*/
$desired_height=地板($height*($desired_width/$width));
/*创建一个新的“虚拟”映像*/
$virtual\u image=ImageCreateTureColor($required\u width,$required\u height);
/*以调整过的大小复制源图像*/
imagecopyresampled($virtual_image,$source_image,0,0,0,$desired_width,$desired_height,$width,$height);
/*创建指向其目标的物理缩略图图像*/
imagejpeg($virtual_image,$dest,75);
}
函数get_files($images_dir,$exts=array('jpg','png','gif','jpeg')){
$files=array();
如果($handle=opendir($images\u dir)){
while(false!=($file=readdir($handle))){
$extension=strtolower(获取文件扩展名($file));
if($extension&&in_数组($extension,$exts)){
$files[]=$file;
}
}
closedir($handle);
}
返回$files;
}
/*函数:返回文件的扩展名*/
函数get\u file\u扩展名($file\u name){
返回substr(strrchr($file_name,“.”),1);
}
$images_dir='data/'。$key./photos/';
$thumbs_dir='data/'。$key./photos/f/';
$thumbs_width=100;
$images\u/行=6;
/**生成照片库**/
$image\u files=获取文件($images\u dir);
如果(计数($image_文件)){
$index=0;
foreach($index=>$file形式的图像文件){
$index++;
$thumbnail\u image=$thumbs\u dir.$file;
如果(!file_存在($thumbnail_image)){
$extension=get_file_extension($thumbnail_image);
如果($延期){
fm_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
回声';
如果($index%$images_per_row==0){echo';}
}
回声';
}
否则{
echo“此库中没有图像。”;
}
您可以使用可以安装在服务器上的和ImageProcessor,或使用任何编译语言编写,它将比php更快,并且您可以根据所使用的应用程序限制资源使用
您可以使用shell\u exec('console command here')
执行此过程
sry关于这个问题,请参考问题我会尝试安装ImageMagick,并通过PHP从命令行调用它。如果它是一个内部服务器,为什么您会如此关心延迟?另外,它可能会在500张图像后停止,因为400秒的时间不足以处理您的请求?为什么不先把时间限制设为无限制,看看实际需要多长时间?你看过imagedestroy吗?用于释放中间的内存images@afuzzyllama我不在乎,因为那台机器没有被任何人使用,它专门用来存储它,但是它的延迟可能有点烦人。由于客户端也会延迟,因此每个用户的文件都会通过web面板显示#ontrack事实上不我没有想到,嗯,忘了图像破坏