Potential Memory leak or am I just using too large files?

Hi there,

I’m doing a little project for a charity and they’re using pretty large high res images, like really large, some are as large as 11 MB. The error below is from an image which is 8MB large.

Now, I’ve been using the focus plugin to handle these images, but if their size is going to lead to issues for the site is is recommended I maybe implement an actual cropper to reduce file sizes?

I’ve also noticed that speed’s been effected quite badly by loading images anyway, so I need to do something on that front. If someone has dealt with this before please let me know what the best course of action is thank you!

Both removing and reducing the file size (by a manual crop) doesn’t error. This has happened a few times.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 22464 bytes) in 
C:\xampp\htdocs\Projects\1-Kirby\Project-Ladder\Ladder-V0.15\kirby\toolkit\vendors\abeautifulsite\SimpleImage.php on line 300

The error is pointing to the code below:

         // Perform crop
    $new = imagecreatetruecolor($crop_width, $crop_height);
    imagealphablending($new, false);
    imagesavealpha($new, true);
    imagecopyresampled($new, $this->image, 0, 0, $x1, $y1, $crop_width, $crop_height, $crop_width, $crop_height);

Would reducing file size on upload be an option for you? There are several threads on the forum with solutions to do just that.

It’s looking like that’s the best course of action right now.

In order to get the site live I’ll crop the large images down, but if this is going to cause an error every time then I need a better solution because I don’t know who’s going to upload an image a year from now.

I can’t rely on people doing the right thing. Maybe I could add some rules/checks in. Image files 3-4 MB and smaller, or resolution restrictions? With maybe a suggestion of cropping the image and then trying to upload again.

You can restrict the size of uploaded files in the file settings: https://getkirby.com/docs/panel/blueprints/file-settings#file-limits

Awesome!

Is my problem purely to do with file size though?

Thanks :stuck_out_tongue:

When does that error message appear? Do you try to create thumbs from those large images?

They’re hero/banner images and it’s only appearing on a couple of pages. Like every page has one and each image is of equal size when cropped and the upload sizes vary between 5 MB and 11 MB

The server runs out of memory, so it seems it can’t handle that. What is the memory_limit setting in your php.ini?

Would shrinking the image via a hook on image upload result in the same memory issue?

You could try to switch to imagemagick. You are probably still using GD Lib, which is the default library to handle image resizing. Imagemagick is better at handling large images.

You can switch to imagemagick, if it’s installed on your server like this:

// in site/config/config.php
c::set('thumbs.driver', 'im');

The limit set in my php,ini file is 128MB for my local version I don’t know what’s on my test server though. Must be the same because the error is occurring on both my local and my live test server.

So I tried it and it’s no longer producing an error but it’s no longer cropping the image with the focus plugin (see below).

Maybe Imagemagick isn’t installed locally? I know it’s installed by my host but I haven’t installed it locally I’m just using what was installed with XAMP.

<?php if($image = $page->coverImage()->toFile()): ?>  	
	<div>
		<?php  echo $image->focusCrop(2500, 550);  ?>
	</div>
<?php endif ?>

You can run phpinfo() in your template to see if ImageMagick is installed

@Thiousi Kirby is using imagemagick from the command-line, rather than PHP’s Imagick-extensions. so imagemagicks binary convert has to installed on the server.

@JIrving In order to make it work, you probably need to set the correct path to convert in config.php in addition to changing the driver. See: https://getkirby.com/docs/cheatsheet/options/thumbs-bin