php - image creating slow down servre -
i have function , funqction i`m trying create images in server
foreach($value[0] $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 understand im getting image link , chacking if file exists , i
m creating file if not exists. server slow down. using 2gb ram. can accelerate server?
i tryed file_put_content() first , creating thumb not working gd library. please me function quicker is.
one thing note (not answer problem): when using gd2 functions, don't trust file extension. save jpeg name "trollpic.gif" , cause imagecreatefromgif throw error.
use exif data instead: http://php.net/manual/en/function.exif-imagetype.php
also - try imegemagick alternative gd2 if that's possible (it's not on cheaper hosting services).
[edit]
$original = imagecreatetruecolor($width, $height); imagecopyresampled($original, $getfile, 0, 0, 0, 0, $width, $height, $width, $height); imagejpeg($original, "../www/classifieds_images/$originalname");
looks $getfile , $original both keep same data. check if work:
$original = imagecreatetruecolor($width, $height); imagejpeg($getfile, "../www/classifieds_images/$originalname");
it's not best can optimize code, @ least it's start. i'd recommend setting limit how many files can processed in 1 execution of script , queueing - that's best if you're trying process lot of data, not image related.
[edit2]
also - unset variables when they're no longer needed. when you've done image , saved in file - destroy resource. won't remove image file, drop data memory. http://php.net/manual/en/function.imagedestroy.php
Comments
Post a Comment