Pergunta

I'm trying to upload an image into an application I'm creating. The .png image occupies about 190KB in the file system and the resolution is 2000px wide and 1667 px in height. I have set the memory limit for PHP for 32MB. But when I try to upload this .png image which I believe a lot of the people using my application will do, I'm getting the following error

Allowed memory size of 33554432 bytes exhausted (tried to allocate 13336000 bytes)

Now when I calculated these into say MB I understood that it read

Allowed memory size of 32MB exhausted ( tried to allocate 12.71MB )

So what I don't understand is how come a file which shows a size of 190KB on the file system end up taking up so much memory space? Is it something about how .png files are handled in memory which I don't know about?

Here is the code

private function optimise_image($source_path, $destination_path){
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_path);
    //Using this source path, we'll create a memory instance of the image for processing
    $source_image_ratio = $source_image_width/$source_image_height;
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_path);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_path);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_path);
            break;
    }

The error is occuring in imagecreatefrompng(). I load the image here to reduce it's resolution and to set it's quality to optimise storage and bandwidth on viewing.

Upon investigating a bit further, I found that the file is being uploaded and the size of the file on the server is again 190KB. So the piece of code you're seeing is where it's trying to pick up the file again for processing. Before this there's no where in the process where the file is being loaded into memory once and then left there without being destroyed ( which I thought was what would have been happening )

If there is any more information you may require, do ask I and I shall try and answer accordingly.

Thanks in advance :)

Foi útil?

Solução

image-width x image-height x 4 bytes (32bit) per pixel is needed just for the imagecreatefrompng / imagecreatefromjpeg

So that's roughly 12MB in your case

That leaves just 20MB for the GD lib and other PHP processes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top