Search code examples
phpimage-uploadinglarge-filesmemory-limit

Is there a way to reduce php createimagefromjpeg memory usage?


I'm building a website where I should be able to upload really large images (from 2MB to 50MB). Of course I need to resize them, but createimagefromjpeg function uses A LOT of memory. Memory usage hits 800 megabytes for some of the images, but I'm using hostgator's shared plan and 256M memory is maximum there. Can I somehow reduce the memory this function uses?


Solution

  • According to the Hostgator forums ImageMagick should be installed on your server. I would recommend using a binary for something such as image resizing because of sheer efficiency. To do what you are looking to do, a command such as this will suffice (assuming you want the 2 MB you asked for):

    $SCALED_VALUE = (2097152/filesize($OLD_IMAGE))*100; #< Get the percentage of scaling that is required to go to 2MB
    shell_exec('convert '. $OLD_IMAGE . '-resize ' . $SCALED_VALUE . '% new_image.jpg'); #< Perform scaling
    

    To answer the question regarding php_memory_limit in the comments; this limit is within PHP and will not affect ImageMagick (or any other application other than PHP).