Witam,
Posiadam skrypt napisany za pomocą pakietu ImageMagick, który konwertuje tekst i obraz gif wprowadzony przez użytkownika w jeden obiekt gif i umieszcza go w serwisie. Wszystko działa poprawnie lecz przy dodwaniu większych gifów mechanizm działa bardzo długo, aż w końcu dostaję error jednak obiekt zostaje dodany:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@.pl and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2 Server at .pl Port 80


W odpowiedzi od hostingodawcy dostałem informację, że najprawdopodobniej mam żle zoptymalizowany skrypt.

Oto Logi błedów:
[Thu Nov 01 11:33:23 2012] [error] [client 78.30.108] Script timed out
before returning headers: dodaj.php, referer:
http://--.pl/dodaj.php?co=gif
[Thu Nov 01 11:33:56 2012] [error] [client 78.30.108] File does not
exist: --.pl/public_html/500.shtml,
referer: http:/--.pl/dodaj.php?co=gif



  1. if(isset($_POST['submit_gif']) and $_GET['co']=='gif') {
  2.  
  3. if(isset($_FILES["file"]["type"]))
  4. {
  5. if ((($_FILES["file"]["type"] == "image/gif")))
  6. {
  7. if ($_FILES["file"]["error"] > 0)
  8. {
  9. echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  10. die();
  11. }
  12. else
  13. {
  14.  
  15. $fname = $_FILES["file"]["name"].time().rand(1000,9999);
  16. move_uploaded_file($_FILES["file"]["tmp_name"],
  17. $fname);
  18. }
  19. }
  20. else
  21. {
  22. echo "typ pliku się nie zgadza (tylko gify)";
  23. die();
  24. }
  25. }
  26. else
  27. {
  28. die();
  29. }
  30. $file = $fname;
  31. list($width, $height, $type, $attr) = getimagesize($file);
  32.  
  33. //wewnętrzny gif///////////
  34. $max_width = 400;
  35. $max_height = 400;
  36. ///////////////////////////
  37. exec("convert $file -coalesce $file");
  38. exec("convert -size $width"."x"."$height $file -resize $max_width"."x"."$max_height $file");
  39.  
  40. $b = 'b_'.time().rand(1111,9999);
  41. $text_f = 't_'.time().rand(1111,9999);
  42. $anim = 'a_'.time().rand(1111,9999);
  43. $res_img = 'res_'.time().rand(1111,9999);
  44. ///////////kolory tła:////////////
  45. $bg = array(
  46. array('207','219','236'),
  47. array('214','255','194'),
  48. array('253','255','194'),
  49. array('255','233','194'),
  50. array('248','149','255'),
  51. array('255','194','194')
  52. );
  53. /*
  54. Niebieski: 207 219 236
  55. Zielony: 214 255 194
  56. Żółty: 253 255 194
  57. Pomarańcz: 255 233 194
  58. Fiolet: 248 149 255
  59. Czerwony: 255 194 194 */
  60. $rgb = $_POST['rgb'];
  61. $r2 = $bg[$rgb][0];
  62. $g2 = $bg[$rgb][1];
  63. $b2 = $bg[$rgb][2];
  64. //kolor tekstu://///////////////
  65. $r = 67;
  66. $g = 67;
  67. $b = 67;
  68. /////////rozmiar tekstu////////
  69. $size = 30;
  70. //plik czcionki////////////////
  71. $font = 'Century Gothic Bold.ttf';
  72. ////////////////
  73. $watermark= 'serwis.pl'; //watermark na dole
  74. $watermark_size = 15; //rozmiar watermarkowej czcionki
  75. ///////////////
  76.  
  77. $text = strip_tags($_POST['text']);
  78. $lines = explode('|', wordwrap($text, 30, '|'));
  79. list($width, $height, $type, $attr) = getimagesize($file);
  80. /////odstęp na dole (pod obrazkiem)/////////
  81. $space = 25;
  82. ////////////////////////////////////////////
  83. $new_h = $height+$space;
  84. /////szerokość wyniku/////////
  85. $new_w = 520;
  86. /////////////////////////////
  87. $image = imagecreatetruecolor($new_w, $new_h+25);
  88. $color = imagecolorallocate($image, $r, $g, $b);
  89. $color_bg = imagecolorallocate($image, $r2, $g2, $b2);
  90.  
  91. $y = (($new_w-$width)/2);
  92.  
  93. ////odstęp od górnej krawędzi całości////
  94. $h = 50;
  95. /////////////////////////////////////////
  96.  
  97. imagefilledrectangle($image,0,0, $new_w,$new_h+25,$color_bg);
  98. foreach ($lines as $line)
  99. {
  100. list($left,,$right,$up,,$down) = imageftbbox( $size, 0, $font, $line);
  101. $text_w = $right - $left;
  102. while($text_w>$new_w)
  103. {
  104. list($left,,$right,$up,,$down) = imageftbbox( $size, 0, $font, $line);
  105. $text_w = $right - $left;
  106. $size--;
  107. }
  108. }
  109. reset($lines);
  110. $size-2;
  111. foreach ($lines as $line)
  112. {
  113. list($left,,$right,$up,,$down) = imageftbbox( $size, 0, $font, $line);
  114. $text_w = $right - $left;
  115.  
  116. $text_h = $up - $down;
  117. $center = ($new_w-$text_w)/2;
  118. imagettftext($image, $size, 0, $center, $h, $color, $font, $line);
  119. $h += $text_h+15;
  120. }
  121. $image2 = imagecreatetruecolor($new_w, $new_h+$h-15);
  122. imagefilledrectangle($image2,0,0, $new_w,$new_h+$h-15,$color_bg);
  123. imagecopymerge($image2, $image, 0, 0, 0, 0, $new_w, $new_h+25, 100);
  124.  
  125. list($left,$down,$right) = imageftbbox( $watermark_size, 0, $font, $watermark);
  126. $text_w = $right - $left;
  127.  
  128. $right_align = $new_w-$text_w-10;
  129. imagettftext($image2, $watermark_size, 0, $right_align, $new_h+$h-20, $color, $font, $watermark);
  130. imagejpeg($image2, $text_f.'.jpg');
  131. imagedestroy($image);
  132. imagedestroy($image2);
  133.  
  134. $cmd = "convert ".$file." -matte -bordercolor none -border ".$y."x".$h." ".$b.".gif";
  135. exec($cmd);
  136. $cmd = "convert ".$text_f.".jpg ".$b.".gif -loop 0 ".$anim.".gif";
  137. exec($cmd);
  138. $cmd = "convert ".$anim.".gif -coalesce -delete 0 -deconstruct -loop 0 r_".$res_img.".gif";
  139. exec($cmd);
  140. ///kasowanie plików pomocniczych///
  141. unlink($text_f.'.jpg');
  142. unlink($b.".gif");
  143. unlink($file);
  144. unlink($anim.".gif");
  145. $uploaddir = 'img/upload/';
  146. $tytul=htmlspecialchars($_POST['tytul']);
  147. $zrodlo=htmlspecialchars($_POST['zrodlo']);
  148. $autor = $user->userInfo("id");
  149. $data=date('d-m-Y H:i');
  150. $wykonaj = mysql_query("INSERT INTO `shity` (`title`, `img`, `source`, `author`, `data`, `type`) VALUES ('$text', '".$uploaddir."r_".$res_img.".gif', '$zrodlo', '$autor', '$data', 'obrazek')");
  151. exec("mv r_".$res_img.".gif ".$uploaddir."r_".$res_img.".gif");
  152.  
  153. }
  154. elseif ($_GET['co']=='gif') {


Co zrobić aby mechanizm działał szybciej i żeby nie pokazywał się error?