daniel5455 Posted April 10, 2008 Share Posted April 10, 2008 I have a website that the customer can add a weekly ad (pdf) to the site as he wishes. My hosting company just sent me an email stating the following. Hi, Your resold account "csite" on reseller server Servername is running multiple instances of several php scripts and causing major load problem for the entire server. We have been monitoring this for the last couple of days. After carefully watching the server hardware consumption at the time when the load increases and server performance decreases, we have identified that your account is the one causing problems on the server. The php scripts are running under the directory /home/csite/public_html/wkly/ You can see some of those processes below and their cpu usage on the entire server: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 14599 csite 17 0 87200 53m 51m R 46 1.8 0:00.52 /usr/php4/bin/php /home/csite/public_html/wkly/getImage.php 14597 csite 17 0 91732 11m 4916 R 25 0.4 0:00.30 /usr/php4/bin/php /home/csite/public_html/wkly/getImage.php 14600 csite 17 0 91732 11m 4912 R 15 0.4 0:00.30 /usr/php4/bin/php /home/csite/public_html/wkly/getImage.php 14603 csite 17 0 93056 13m 4904 R 10 0.4 0:00.15 /usr/php4/bin/php /home/csite/public_html/wkly/getImage.php 14602 csite 17 0 91732 11m 4912 R 10 0.4 0:00.14 /usr/php4/bin/php /home/csite/public_html/wkly/getImage.php We had no choice but to disable web access to that folder to stabilize the server as these processes are over loading the server. Please optimize your code/script to avoid these heavy resource consuming apache & mysql usage. You can still access the folder "wkly" using cPanel and FTP. If you need web access to work on it, provide us your IP address by visiting this page: www.myipaddress.com and we can enable web access to you as well. If it is normal usage of the scripts/domain, I suggest you move that domain to a separate semi-dedicated account which has more resources available to run such high cpu-usage scripts. Here is the code for the getImage.php <?php // required param: img function string_to_number($string){ $string = preg_replace('/[\'"]/', '', $string); $string = preg_replace('/[^0-9]+/', '', $string); $string = trim($string, ''); $string = strtolower($string); return $string;} $editDirectory = $_SERVER[DOCUMENT_ROOT] . '/wkly/'; $imageName = $_GET["img"]; $width = string_to_number($_GET['w']); $quality = string_to_number($_GET['q']); if(empty($imageName) || !file_exists($editDirectory.$imageName) ){ exit; } if(empty($quality)){ $quality = 90; } // The file $filename = $editDirectory.$imageName; list($orig_width, $orig_height) = getimagesize($filename); if(empty($width)){ $width=$orig_width; $height=$orig_height; }else{ $resize_percent = $orig_width/$width; $height = ceil($orig_height / $resize_percent); } // Content type header('Content-type: image/jpeg'); // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height); // Output imagejpeg($image_p, null, $quality); ?> I dont know how to optimize this script. And i cant afford to move this to a dedicated server. Any Suggestions on this Thanks In Advanced. Quote Link to comment https://forums.phpfreaks.com/topic/100507-php-script-using-too-much-of-the-processor/ Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 should try and destroy the image after it is outputted imagedestroy($image_p); Usually a script will run like that if you end up with an infinite loop. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100507-php-script-using-too-much-of-the-processor/#findComment-513994 Share on other sites More sharing options...
daniel5455 Posted April 10, 2008 Author Share Posted April 10, 2008 i not that good with php scripts i had someone do this site and i can no longer get a hold of that person. Can you explain what that command does and were would i put it. thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/100507-php-script-using-too-much-of-the-processor/#findComment-513999 Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 You can put that just after imagejpeg(). Quote Link to comment https://forums.phpfreaks.com/topic/100507-php-script-using-too-much-of-the-processor/#findComment-514000 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 Could also be running because people are going to the page without an image file being set <?php // required param: img function string_to_number($string){ $string = preg_replace('/[\'"]/', '', $string); $string = preg_replace('/[^0-9]+/', '', $string); $string = trim($string, ''); $string = strtolower($string); return $string;} if(isset($_GET['img'])){ if(file_exists($_GET['img'])){ $editDirectory = $_SERVER[DOCUMENT_ROOT] . '/wkly/'; $imageName = $_GET["img"]; $width = string_to_number($_GET['w']); $quality = string_to_number($_GET['q']); if(empty($imageName) || !file_exists($editDirectory.$imageName) ){ exit; } if(empty($quality)){ $quality = 90; } // The file $filename = $editDirectory.$imageName; list($orig_width, $orig_height) = getimagesize($filename); if(empty($width)){ $width=$orig_width; $height=$orig_height; }else{ $resize_percent = $orig_width/$width; $height = ceil($orig_height / $resize_percent); } // Content type header('Content-type: image/jpeg'); // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$orig_width,$orig_height); // Output imagejpeg($image_p, null, $quality); imagedestroy($image_p); } else { echo "Image does not exist"; } } else { echo "No image defined"; } ?> Now it will check to see if the img is set and also check to see if it even exists also. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100507-php-script-using-too-much-of-the-processor/#findComment-514007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.