Severin Kacianka: Probleme mit dem Memory Limit bei der Thumbnailerstellung

Hallo liebes Forum,

ich versuche mich gerade an der Thumbnailerstellung von hochgeladenen (jpg-)Bildern und erschöpfe jedes Mal das Memory Limit. Als Input verwende ich im Moment ca. 1,4MB große Bilder.
Hier der relevante Code:

  
function createThumb($src,$dest,$new_w,$new_h){  
    $src = imagecreatefromjpeg($src);#Zeile 583  
    $old_x = imageSX($src);  
    $old_y = imageSY($src);  
    if ($old_x > $old_y) {  
        $thumb_w = $new_w;  
        $thumb_h = $old_y * ($new_h / $old_x);  
    }  
    if ($old_x < $old_y) {  
        $thumb_w = $old_x * ($new_w / $old_y);  
        $thumb_h = $new_h;  
    }  
    if ($old_x == $old_y) {  
        $thumb_w = $new_w;  
        $thumb_h = $new_h;  
    }  
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);  
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);  
    imagejpeg($dest_img,$dest);  
    imagedestroy($dst_img);  
    imagedestroy($src_img);  
}  

(Ist fast eine Kopie von http://www.icant.co.uk/articles/phpthumbnails/)
Der Aufruf sieht so aus:

  
<?php  
ini_set("memory_limit","20M");  
include $_SERVER['DOCUMENT_ROOT'].'/includes/datenbank_funktionen.inc.php';  
#echo (int)getWidth('gallerie/9/1.jpg',150);  
echo '<br>'.memory_get_usage();  
createThumb('gallerie/9/1.jpg','gallerie/9/thumb_1.jpg',112,150);  
?>  

Die Ausgabe sieht in etwa so aus:
298984
Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 1944 bytes) in /home/severin/ius/includes/datenbank_funktionen.inc.php on line 583

Die "Googlelösung" ist einfach das memory_limit so hoch wie möglich stellen, aber wenn 20MB nicht reichen, mache ich irgendeinen Fehler.

Was landet alles im Speicher? Bei 20MB Memory_Limit müsste ich doch locker zehn 1,4MB große Bilder unterbringen können?

Gruß und danke für eure Zeit,
Severin

--
They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.
-- Benjamin Franklin
  1. Hallo,

    ich habe das ganze jetzt mit einem 70KB Bild erfolgreich probiert (und dabei auch gleich ein paar Fehler gefunden).
    Korrigierte Version:

      
    function createThumb($src,$dest,$new_w,$new_h){  
        $src_img = imagecreatefromjpeg($src);  
        $old_x = imageSX($src_img);  
        $old_y = imageSY($src_img);  
        if ($old_x > $old_y) {  
            $thumb_w = $new_w;  
            $thumb_h = $old_y * ($new_h / $old_x);  
        }  
        if ($old_x < $old_y) {  
            $thumb_w = $old_x * ($new_w / $old_y);  
            $thumb_h = $new_h;  
        }  
        if ($old_x == $old_y) {  
            $thumb_w = $new_w;  
            $thumb_h = $new_h;  
        }  
        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);  
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);  
        imagejpeg($dst_img,$dest);  
        imagedestroy($dst_img);  
        imagedestroy($src_img);  
    }  
    
    

    Bleibt aber immer noch das Problem, wie ich das ganze mit großen Bildern hinbekomme.

    Gruß,
    Severin

    --
    They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.
    -- Benjamin Franklin
    1. Hallo ich,

      also mit einem memory_limt von 25MB funktioniert es auch mit dem großen Bild.
      Aber ist es vertretbar das memory_limit so hoch zu setzen?

      Gruß,
      Severin

      --
      They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.
      -- Benjamin Franklin
      1. Tach.

        Anfang des Monats gab es schon einmal einen Thread zu diesem Thema: Arbeitsspeicherverbrauch von imagecreatefromjpeg(). Vor allem Martins Ausführungen dürften dir dabei helfen.

        --
        Once is a mistake, twice is jazz.
        1. Hallo Blaubart,

          Anfang des Monats gab es schon einmal einen Thread zu diesem Thema: Arbeitsspeicherverbrauch von imagecreatefromjpeg(). Vor allem Martins Ausführungen dürften dir dabei helfen.

          Danke für den Verweis. Dann werde ich wohl einfach das Memory_Limit schätzen und beten :-)

          Gruß,
          Severin

          --
          They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.
          -- Benjamin Franklin