Автор Тема: Помощ за скрипт обработващ картинки  (Прочетена 953 пъти)

a_panov

  • Напреднали
  • *****
  • Публикации: 62
    • Профил
Имам в логовете тая грешка:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 10368 bytes) in /home/bg_cars/public_html/adds/add.php on line 310

PHP-то там ми е:
$image = imagecreatefromjpeg($file);
След това следва код с който обработвам качената снимка - ресайзвам я пропорционално и правя тъмбнейл.

Уродите ми подават 800К до 2М снимки които като се опитам да обработя РНР-то ми казва че прехвърлям лимита от 16 меги където да се разполагам. 800К jpeg на диска е 20-25 мегабайта в паметта.

Какво да правя когато ми подадат "големи" снимки?
Кода ми е:
Код
GeSHi (PHP):
  1. move_uploaded_file($HTTP_POST_FILES["$c"]["tmp_name"], "images/" . $id2 . "/" . $ii . ".png");
  2. smart_resize_image("images/" . $id2 . "/" . $ii . ".png", $width = 480, $height = 360, $proportional = true, $output = 'file', $delete_original = true, $use_linux_commands = false );

Код
GeSHi (PHP):
  1. function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
  2.  {
  3.    if ( $height <= 0 && $width <= 0 ) {
  4.      return false;
  5.    }
  6.  
  7.    $info = getimagesize($file);
  8.    $image = '';
  9.  
  10.    $final_width = 0;
  11.    $final_height = 0;
  12.    list($width_old, $height_old) = $info;
  13.  
  14.    if ($proportional) {
  15.      if ($width == 0) $factor = $height/$height_old;
  16.      elseif ($height == 0) $factor = $width/$width_old;
  17.      else $factor = min ( $width / $width_old, $height / $height_old);  
  18.  
  19.      $final_width = round ($width_old * $factor);
  20.      $final_height = round ($height_old * $factor);
  21.  
  22.    }
  23.    else {
  24.      $final_width = ( $width <= 0 ) ? $width_old : $width;
  25.      $final_height = ( $height <= 0 ) ? $height_old : $height;
  26.    }
  27.  
  28.    switch ( $info[2] ) {
  29.      case IMAGETYPE_GIF:
  30.        $image = imagecreatefromgif($file);
  31.      break;
  32.      case IMAGETYPE_JPEG:
  33.        $image = imagecreatefromjpeg($file);
  34.      break;
  35.      case IMAGETYPE_PNG:
  36.        $image = imagecreatefrompng($file);
  37.      break;
  38.      default:
  39.        return false;
  40.    }
  41.  
  42.    $image_resized = imagecreatetruecolor( $final_width, $final_height );
  43.  
  44.    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  45.      $trnprt_indx = imagecolortransparent($image);
  46.  
  47.      // If we have a specific transparent color
  48.      if ($trnprt_indx >= 0) {
  49.  
  50.        // Get the original image's transparent color's RGB values
  51.        $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
  52.  
  53.        // Allocate the same color in the new image resource
  54.        $trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  55.  
  56.        // Completely fill the background of the new image with allocated color.
  57.        imagefill($image_resized, 0, 0, $trnprt_indx);
  58.  
  59.        // Set the background color for new image to transparent
  60.        imagecolortransparent($image_resized, $trnprt_indx);
  61.  
  62.  
  63.      }
  64.      // Always make a transparent background color for PNGs that don't have one allocated already
  65.      elseif ($info[2] == IMAGETYPE_PNG) {
  66.  
  67.        // Turn off transparency blending (temporarily)
  68.        imagealphablending($image_resized, false);
  69.  
  70.        // Create a new transparent color for image
  71.        $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  72.  
  73.        // Completely fill the background of the new image with allocated color.
  74.        imagefill($image_resized, 0, 0, $color);
  75.  
  76.        // Restore transparency blending
  77.        imagesavealpha($image_resized, true);
  78.      }
  79.    }
  80.  
  81.    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
  82.  
  83.    if ( $delete_original ) {
  84.      if ( $use_linux_commands )
  85.        exec('rm '.$file);
  86.      else
  87.        @unlink($file);
  88.    }
  89.  
  90.    switch ( strtolower($output) ) {
  91.      case 'browser':
  92.        $mime = image_type_to_mime_type($info[2]);
  93.        header("Content-type: $mime");
  94.        $output = NULL;
  95.      break;
  96.      case 'file':
  97.        $output = $file;
  98.      break;
  99.      case 'return':
  100.        return $image_resized;
  101.      break;
  102.      default:
  103.      break;
  104.    }
  105.  
  106.    switch ( $info[2] ) {
  107.      case IMAGETYPE_GIF:
  108.        imagegif($image_resized, $output);
  109.      break;
  110.      case IMAGETYPE_JPEG:
  111.        imagejpeg($image_resized, $output);
  112.      break;
  113.      case IMAGETYPE_PNG:
  114.        imagepng($image_resized, $output);
  115.      break;
  116.      default:
  117.        return false;
  118.    }
  119.     return true;
  120.  }
  121.  
« Последна редакция: Sep 02, 2009, 08:40 от VladSun »
Активен

neter

  • Global Moderator
  • Напреднали
  • *****
  • Публикации: 3408
  • Distribution: Debian, SailfishOS, CentOS
  • Window Manager: LXDE, Lipstick
    • Профил
    • WWW
Re: Помощ за скрипт обработващ картинки
« Отговор #1 -: Sep 02, 2009, 00:06 »
Имаш три варианта:
1. Задай повече памет към memory_limit директивата в php.ini. Например
Цитат
memory_limit = 50M
Това ще се отрази на всички php приложения, в които не е специално указано друго;
2. Сложи подобен ред в началото на твоя php код (под <?php)
Код
GeSHi (PHP):
  1. ini_set('memory_limit', '48M');
3. Направи си .htaccess файл в папката, в която е твоят php код, и сложи подобен ред в него
Цитат
php_value memory_limit 48M
Активен

"Да си добре приспособен към болно общество не е признак за добро здраве" - Джиду Кришнамурти