图像创建减慢服务速度


image creating slow down 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 内存。我该怎么做来加速我的服务器?

我先尝试了file_put_content((,然后创建拇指但它不像GD库那样工作。所以请帮助我比现在更快地完成这个功能。

需要注意的一件事(不是你真正的问题的答案(:使用 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都保留相同的数据。检查这是否有效:

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

优化代码的最佳方法,但至少这是一个开始。我建议对脚本的一次执行中可以处理的文件数量设置一些限制,并将其排队 - 如果您尝试处理大量数据,不一定与图像相关,这是您可以做的最好的事情。

[编辑2]

另外 - 当不再需要变量时取消设置变量。当您对图像完成所有操作并将其保存在文件中时 - 销毁资源。它不会删除图像文件,只是从内存中删除其数据。http://php.net/manual/en/function.imagedestroy.php