logo
21.01.2020 13:48
1
Ahoj,

muzete mi prosim poradit ohledne spravneho postupu pri PHP uploadu obrazku, aby nedoslo k vycerpani memory limitu? Hosting je u Forpsi. Od tech jsem se dozvedel vypocet, ktery ukazuje narocnost na pamet.

výška x šířka x 3(barevné kanály) x 1.65

Uzivatelum jsem povolil uploadovat soubor (obrazek) max. 5MB. Obrazek se uploaduje a ulozi se, to je fajn. Jenze ja chci s tim souborem dale pracovat. Chci vytvorit minuatury, nebo ho otocit, apod. A tam to narazi na Memory limit, ktery je ale pry 128MB. Vypocet viz. vyse mi ukazuje cca 90MB, ale je mi jasne, ze tam jeste neco bezi.

Kdyz chci obrazek zmensit, vlastne se k tomu ani nedostanu. Poradi nekdo zpusob, jak to resit? Treba mam nejakou spatnou metodiku. Ale nahrani a ulozeni je v poradku. Problem je s tim nactenim pred zmensenim, apod.

Jednoducha ukazka:

Kód:
<?php

                    $filename = 'upload/images/file_123465.jpg';                    
                    if (file_exists($filename) === TRUE)
                    {
                        $image_info = getimagesize($filename);                
                        $width = $image_info[0];
                        $height = $image_info[1];

                        if ($image_info[0] > 2500)
                        {
                            $new_width = ceil($width / 2);
                            $new_height = ceil($height / 2);

                            $bg_color = imagecreatetruecolor($width, $height);                            
                            $source = imagecreatefromjpeg($filename);
// a tady to proste spadne
                            imagecopyresampled(
                                $bg_color, $source,
                                0, 0, 0, 0,
                                $new_width, $new_height,
                                $width, $height);

                            imagejpeg($bg_color, $filename, 75);
                        }
}
Na Forpsi to maji vysvetleno: https://support.forpsi.com/kb/a2770/...-knihovny.aspx

Co se právě děje na Webtrhu?

21.01.2020 15:01
2
Kód:
ini_set('memory_limit', '-1');

 if ( $width > 2500 ...
22.01.2020 08:17
3
Původně odeslal zezmen
Kód:
ini_set('memory_limit', '-1');

 if ( $width > 2500 ...
Jste si jisty, ze to bude stacit?
22.01.2020 09:47
4
Nevím jak u Forpsi, ale normálně mi vše proběhne v pořádku.
Otestujte následující kód:
Kód:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('memory_limit', '-1');

  function getRequiredMemory($_ImagePath)
    {
        $_ImageInfo = getimagesize($_ImagePath);
        echo 'Require Memory to edit <br>';
        echo 'bites: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65))."<br>";
        echo 'Kb: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65) / 1024)."<br>";
        echo 'Mb: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65) / 1048576)."<br>";
    }
    
    getRequiredMemory('./upload/images/file_123465.jpg');
    

          $filename = 'upload/images/file_123465.jpg';                    
          if (file_exists($filename) !== False)
             {
                $image_info = getimagesize($filename);                
                $width = $image_info[0];
                $height = $image_info[1];

                if ($width > 2500)
                    {
                      $new_width = ceil($width / 2);
                      $new_height = ceil($height / 2);

                      $bg_color = imagecreatetruecolor($width, $height);                            
                      $source = imagecreatefromjpeg($filename);

                      imagecopyresampled(
                                $bg_color, $source,
                                0, 0, 0, 0,
                                $new_width, $new_height,
                                $width, $height);

                      if (!imagejpeg($bg_color, $filename.'-new.jpg', 75)) 
                           { echo 'error <br>'; } 
                           else 
                           { echo 'original image: <br><img src="'.$filename.'" style="max-width:250px;"><br>';
                             echo 'resized image: <br><img src="'.$filename.'-new.jpg'.'" style="max-width:250px;"><br>'; }
                            
                      ImageDestroy($bg_color);
                      
                    }
            }
22.01.2020 09:47
5
Nebude, protoze kazdej spravne nastavenej hosting ti samozrejme nedovoli zmenit memory_limit a uz vubec ne na unlimited... reseni to vicemene nema, zpracovani obrazku je proste pametove narocny, to neni prace na levny hostingy. Osobne bych to asi resil na strane klienta pres javascript a canvas a posilal tudiz uz z klienta zmenseny verze...
22.01.2020 10:04
6
Případně s výpisem zpotřebované paměti:
Kód:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('memory_limit', '-1');

  function getRequiredMemory($_ImagePath)
    {
        $_ImageInfo = getimagesize($_ImagePath);
        echo 'Require Memory to edit <br>';
        echo 'bites: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65))."<br>";
        echo 'Kb: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65) / 1024)."<br>";
        echo 'Mb: '.round((($_ImageInfo[0] * $_ImageInfo[1] * $_ImageInfo['bits'] * $_ImageInfo['channels'] / 8 + Pow(2, 16)) * 1.65) / 1048576)."<br>";
    }
    
    getRequiredMemory('./upload/images/file_123465.jpg');
    

          $filename = 'upload/images/file_123465.jpg';                    
          if (file_exists($filename) !== False)
             {
                echo 'memory start image info: '.memory_get_usage().'<br>';
                $image_info = getimagesize($filename);                
                $width = $image_info[0];
                $height = $image_info[1];
                echo 'memory width and height info: '.memory_get_usage().'<br>';
                if ($width > 2500)
                    {
                      $new_width = ceil($width / 2);
                      $new_height = ceil($height / 2);

                      echo 'memory start imagetruecolor: '.memory_get_usage().'<br>';
                      $bg_color = imagecreatetruecolor($width, $height);
                      echo 'memory end imagetruecolor: '.memory_get_usage().'<br>';
                      echo 'memory start imagecreatefromjpeg: '.memory_get_usage().'<br>';                            
                      $source = imagecreatefromjpeg($filename);
                      echo 'memory end imagecreatefromjpeg: '.memory_get_usage().'<br>';

                      echo 'memory start imagecopyresampled: '.memory_get_usage().'<br>';
                      imagecopyresampled(
                                $bg_color, $source,
                                0, 0, 0, 0,
                                $new_width, $new_height,
                                $width, $height);
                      echo 'memory end imagecopyresampled: '.memory_get_usage().'<br>';

                      echo 'memory start imagejpeg: '.memory_get_usage().'<br>';
                      if (!imagejpeg($bg_color, $filename.'-new.jpg', 75)) 
                           { echo 'error <br>'; } 
                           else 
                           { echo 'original image: <br><img src="'.$filename.'" style="max-width:250px;"><br>';
                             echo 'resized image: <br><img src="'.$filename.'-new.jpg'.'" style="max-width:250px;"><br>'; }
                      echo 'memory end imagejpeg: '.memory_get_usage().'<br>'; 
                           
                      ImageDestroy($bg_color);
                      
                    }
            }