2014-12-02 68 views
1

我有功能以及与此funqction我真的想在我的服务器图像生成减慢servre

  foreach($value[0] as $imagekey => $imageval) { 
       $imgname = $gancxadeba . '_' . $imagekey; 
       $saveaddr = dirname(dirname($_SERVER['PHP_SELF'])).'/www/classifieds_images/'; 
       $as = '.JPG'; 
       $originalname = $imgname . $as; 
       if(!file_exists($saveaddr.$originalname)) { 
        if (preg_match('/\.(jpg)$/', $imageval)) { 
         $getfile = imagecreatefromjpeg($imageval); 
        } elseif (preg_match('/\.(JPG)$/', $imageval)) { 
         $getfile = imagecreatefromjpeg($imageval); 
        } elseif (preg_match('/\.(png)$/', $imageval)) { 
         $getfile = imagecreatefrompng($imageval); 
        } else { 
         $getfile = imagecreatefromgif($imageval); 
        } 
        list($width, $height) = getimagesize($imageval); 
        $newWidth = 90; 
        $newHeight = 120; 
        $original = imagecreatetruecolor($width, $height); 
        imagecopyresampled($original, $getfile, 0, 0, 0, 0, $width, $height, $width, $height); 
        imagejpeg($original, "../www/classifieds_images/$originalname"); 
        echo 'განცხადება: ' . $gancxadeba . ' ორიგინალი სურათი: ' . $imgname . ' created!' . PHP_EOL; 

        $thumbname = $imgname . '_THUMB' . $as; 
        if (!file_exists($saveaddr . $thumbname)) { 
         $thumb = imagecreatetruecolor($newWidth, $newHeight); 
         imagecopyresampled($thumb, $getfile, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
         imagejpeg($thumb, "../www/classifieds_images/$thumbname"); 
         echo 'განცხადება: ' . $gancxadeba . ' თამბი სურათი: ' . $imgname . ' created!' . PHP_EOL; 
        } 
       } 
       $image[$imagekey] = $imgname; 
      } 

建立部分图片,你明白我m getting image link and then chacking if file exists and I米创建文件,如果不存在的话。 但我的服务器变慢了。 它使用2GB RAM。 我可以做些什么来加速我的服务器?

我尝试file_put_content()第一次,然后创建拇指 但它不像gd库一样工作。 所以请帮助我做这个功能比现在更快。

+0

它不会创建减慢速度的文件。但使用图像库,你有没有尝试过最有名的? – 2014-12-02 08:25:26

回答

2

有一点需要注意(不是你的问题的答案): 使用GD2函数时,不要相信文件扩展名。有人可以用名称“trollpic.gif”保存JPEG并导致imagecreatefromgif引发错误。

使用EXIF数据,而不是: http://php.net/manual/en/function.exif-imagetype.php

而且 - 你可以尝试imegemagick作为替代GD2如果可能的话(它不是一些便宜的托管服务)。

[编辑]

$original = imagecreatetruecolor($width, $height); 
imagecopyresampled($original, $getfile, 0, 0, 0, 0, $width, $height, $width, $height); 
imagejpeg($original, "../www/classifieds_images/$originalname"); 

貌似$ GETFILE和$原来都保持相同的数据。 检查是否这将工作:

$original = imagecreatetruecolor($width, $height); 
imagejpeg($getfile, "../www/classifieds_images/$originalname"); 

这不是你可以做优化你的代码是最好的,但至少它是一个开始。 我建议设置一些限制,以便在脚本的一次执行中对多少个文件进行处理并对其进行排队 - 如果您尝试处理大量数据(不一定与图像相关),那么这是最好的选择。

[EDIT2]

而且 - 当他们不再需要尚未设定的变量。 当你完成了所有的图像并保存在一个文件中时 - 销毁资源。它不会删除图像文件,只是从内存中删除它的数据。 http://php.net/manual/en/function.imagedestroy.php

+0

是的。它现在比过去更快。但它使用2GB RAM。我能做些什么来最小化使用RAM? – shalvasoft 2014-12-02 09:05:13