我有一个我无法解决的问题。我有一个复制小图像文件的脚本。问题是对于一个图像变体,它大约需要 1.5 秒。有什么可以更快的获得吗?我正在使用 PHP CLI,我的硬盘是 WD VelociRaptor 10K RPM。源文件夹包含大约 200K 文件
这是我想要更快的代码部分:
$startCopyVariant = time();
$result = array('uploadedImagesUrls'=>array(), 'errMsg'=>'');
// lazyload class instances
$productOptionImages = &lloader()->getImagesByName("productOption");
// validate image
$imgSizeInfo = @getimagesize($srcImageInfo['tmp_name']);
if (empty($imgSizeInfo)) { $result['errMsg'] = 'Invalid image '.$srcImageInfo['name'].', type '.$srcImageInfo['type']; return $result; }
$ext = pathinfo($srcImageInfo['name'], PATHINFO_EXTENSION);
$variantFileName = 'opt_'.$optionId."_variant.".$ext;
$mainDestFileName = $variantFileName;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'], "big");
copy($srcFileName, $productOptionImages->getImagePath().$variantFileName);
$variantFileName = $variantFileName = 'opt_'.$optionId."_variant_sma.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'], "small");
copy($srcFileName, $productOptionImages->getImagePath().$variantFileName);
$variantFileName = 'opt_'.$optionId."_variant_thu.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'], "thumbnail");
copy($srcFileName, $productOptionImages->getImagePath().$variantFileName);
$variantFileName = 'opt_'.$optionId."_variant_tin.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'], "tiny");
copy($srcFileName, $productOptionImages->getImagePath().$variantFileName);
$endCopyVariant = time();
$elapsedTime = $endCopyVariant - $startCopyVariant;
print_r("Variant copy time: (".$srcImageInfo['name']."): ".sprintf('%02d:%02d:%02d', ($elapsedTime/3600),($elapsedTime/60%60), $elapsedTime%60), 0);
谢谢。
编辑:这是 getCropsizeFileName 所做的:
private function getCropSizeFileName($srcFileName, $size) {
global $sourceCropBasePath;
$ext = pathinfo($srcFileName, PATHINFO_EXTENSION);
$destFileNamePrefix = basename($srcFileName, ".".$ext);
return $sourceCropBasePath.$destFileNamePrefix."_".$size.".".$ext;
}
每个复制行的计时器结果为:
Variant copy time1: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:02
Variant copy time2: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:01
Variant copy time3: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:02
Variant copy time4: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:01