1

我有功能,通过这个功能,我试图在我的服务器中创建一些图像

           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(),然后创建了 thumb,但它的效果不如 gd 库。所以请帮我比现在更快地完成这个功能。

4

1 回答 1

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 都保留相同的数据。检查这是否可行:

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

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

[编辑2]

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

于 2014-12-02T08:31:12.413 回答