2

on my website you can upload images and I intend to allow at least 16k resolution for each upload. When displaying the image on the website I want to use a thumbnail image.

To create the thumbnail image I'm using the php code below:

<?php
    $imageUploadFile = $_FILES["passimagefile"]["tmp_name"];

    $src = imagecreatefromjpeg($imageUploadFile);   
    list( $width, $height ) = getimagesize( $imageUploadFile ); 
    $tmp = imagecreatetruecolor( $width / $height * 700, 700 ); 
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width / $height * 700, 700, $width, $height);           
    imagejpeg($tmp, $taget_thumbnail, 75);
?>

This works perfectly fine for most smaller (1k-4k) images. The thumbnail is correctly being generated. However, when I go and try to do it with an image that is like 6k or 8k the result is suddenly wrong.

The thumbnail image is being generated however it is completely black.

Is there a way to fix this?

Miger
  • 1,175
  • 1
  • 12
  • 33

2 Answers2

1

There is many quirks in php-gd, often requiring a lot of trials and errors!

You might had reach an internal limit, thus try to generate your image, but resize it down before exporting, examples:

$tmp = imagescale($tmp, 1920, 1080);
$tmp = imagecrop($tmp,  ['x' => 0, 'y' => 0, 'width' => 1920, 'height' => 1080]);

// ...

imagejpeg($tmp, $taget_thumbnail, 75);

If it doesn't works, you have to consider joining many images, as tiles.


For advanced stuffs, in Linux, you can try to use the shell (from php) and imagemagick instead. http://www.imagemagick.org/Usage/text/

NVRM
  • 11,480
  • 1
  • 88
  • 87
  • Even better, try something with libvips. https://github.com/libvips/php-vips I've never used it with PHP, but it's way faster than Imagemagick in general. – Brad May 22 '20 at 23:55
  • @Brad Thank you, looks nice. – NVRM May 22 '20 at 23:59
  • no libvips or imagemagick since my hosting service won't install anything.. at least for now.. thanks the suggestion I'll try this out as soon as possible.. otherwise I'll have to find a workaround.. – Miger May 27 '20 at 08:59
0

Finally I have found the problem after talking to the customer service of my hosting platform and it turns out that my upload limit over smtp is 20MB and therefore it doesn't work for larger files.

So I guess @NVRM's answer is the most correct and fitting.

The solution by the way would be to get a VPS Hosting instead, there I wouldn't have an upload limit..

Miger
  • 1,175
  • 1
  • 12
  • 33