我编写了一个小函数来获取一个URL,调整图像的大小并将其存储在本地,但是当需要创建文件夹时,脚本运行大约需要.85秒,而调整大小则需要.64秒。我目前支持jpeg和png,如下所示。
我想知道是否有一个更快的方法或者我正在做的事情需要花费很长时间,因为我现在的时间对我来说是无法接受的,我真的想让这个更快地执行。
非常感谢您的任何想法/想法。
谢谢您!
function getTime() {
$timer = explode( ' ', microtime() );
$timer = $timer[1] + $timer[0];
return $timer;
}
function createThumb($thumb, $ids){
$start = getTime();
// File and new size
$filename = $thumb;
// Get new dimensions
$img1 = getimagesize($filename);
if ($img1[0] > $img1[1]) {
$percentage = ('72' / $img1[0]);
} else {
$percentage = ('72' / $img1[1]);
}
$new_width = $img1[0] * $percentage;
$new_height = $img1[1] * $percentage;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
if($img1['mime']=='image/png'){
$image = imagecreatefrompng($filename);
imagealphablending($image_p, false);
imagesavealpha($image_p,true);
$transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
imagefilledrectangle($image_p, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]);
}
else {
$image = imagecreatefromjpeg($filename);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]);
$imgPath = '/foo/bar/location/'.$ids;
$imgName ='';
//category, product, support
if(!is_dir($imgPath)) {
mkdir($imgPath, 0777);
chmod($imgPath, 0777);
}
if(!is_file($imgPath."/index.html")){
$ourFileName = $imgPath."/index.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle,'<html><body>401</body></html>');
fclose($ourFileHandle);
}
// Output
if($img1['mime']=='image/png'){
$name = rand(1, 156406571337);
$imgName = date("y_m_d_h_m_s").$name.'.png';
imagepng($image_p, $imgPath.'/'.$imgName);
} else {
$name = rand(1, 156406571337);
$imgName = date("y_m_d_h_m_s").$name.'.jpg';
imagejpeg($image_p, $imgPath.'/'.$imgName, 100);
}
$end = getTime();
echo '<strong>createImage</strong>: '.round($end - $start,4).' seconden<br />';
exit;
return $imgName;
}