I have function and with this funqction I`m trying to create some images in my server
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;
}
as you understand Im getting image link and then chacking if file exists and I
m creating file if it not exists.
but my server slow down.
it is using 2GB RAM.
what can I do to accelerate my server?
I tryed file_put_content() first and then creating thumb but it not working as well as gd library. so please help me to do this function quicker than is.
One thing to note (not really an answer to your problem): When using GD2 functions, don't trust file extension. Someone could save JPEG with name "trollpic.gif" and cause your imagecreatefromgif to throw an error.
Use exif data instead: http://php.net/manual/en/function.exif-imagetype.php
Also - you could try imegemagick as an alternative to GD2 if that's possible (it's not on some cheaper hosting services).
[EDIT]
Looks like $getfile and $original both keep the same data. Check if this will work:
It's not the best you can do to optimize your code, but at least it's a start. I'd recommend setting some limit to how many files can be processed in one execution of the script and queueing it - that's the best you could do if you're trying to process a lot of data, not necessarily image related.
[EDIT2]
Also - unset variables when they're no longer needed. When you've done everything to an image and saved it in a file - destroy the resource. It won't remove image file, just drop its data from memory. http://php.net/manual/en/function.imagedestroy.php