Hello,

I've coded an image resizing function. It works very well for most image types but I've found that there seems to be a problem with progressive jpeg. If I resize my image for a thumbnail size, 50 px of width the result is only a small black square. I've tried with other image types again to be sure everything was right and it's still working fine.

Did you face that problem?

Your help would be greatly appreciated! Thanks in advance :-)

Dani AI

Generated

Good catch by — the black thumbnail was not a mysterious GD bug but a colorspace issue. PHP’s GD (via libjpeg) expects RGB/JFIF JPEGs and commonly produces black or garbled output when fed CMYK JPEGs exported from print workflows. Progressive vs baseline JPEGs are usually not the root cause; CMYK files are. As recommended, checking your PHP/GD/libjpeg versions is sensible, but the quickest fix is to detect and convert CMYK images before resizing.

Safer detection and better resizing:

  • Don’t trust the client MIME in $_FILES['…']['type']; use finfo or getimagesize()/exif_imagetype() to confirm the file is actually an image.
  • Use imagecopyresampled() (higher quality) instead of imagecopyresized() and free images with imagedestroy() when done.
  • Preserve PNG/GIF transparency with imagealphablending()/imagesavealpha() and choose appropriate output functions/quality for each format.

Example snippets (replace the older checks and imagecopyresized() in the thread):

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime  = finfo_file($finfo, $_FILES['Image']['tmp_name']);
finfo_close($finfo);
imagecopyresampled($output, $src, 0,0,0,0, $new_w, $new_h, $orig_w, $orig_h);

If you must handle CMYK reliably or need color-accurate conversions, use ImageMagick (command line or PHP Imagick). A simple command-line conversion is:

convert input.jpg -colorspace sRGB output.jpg

That converts CMYK to sRGB before resizing with GD or ImageMagick.

Extra troubleshooting tips: check PHP error logs and memory_limit (large images can exhaust memory and produce blank files), validate getimagesize() returns data before proceeding, and return a clear error to users if an unsupported colorspace is detected. Credit to for the file-extension caution and to — server-side getimagesize() (or finfo) is the right place to check image dimensions and type before accepting an upload.

Recommended Answers

All 10 Replies

I've not personally come across that problem but I would suggest starting by looking at the version of PHP, and the version of the GD Library you have installed.

phpinfo();

You should find the info in there then check with the php.net website that its fairly up to date, as a starting point.

Member Avatar for Member #120589

What are you using to resize? Include your code.

Thanks for replying, here's what my code looks like

$upload = $_FILES["Image"]["tmp_name"];
$details_type = explode("/", $_FILES["Image"]["type"]);

if($details_type[1] == "jpg" || $details_type[1] == "jpeg" || $details_type[1] == "pjpeg")
{
$tmp = imagecreatefromjpeg($upload);
}
elseif($details_type[1] == "png")
{
$tmp = imagecreatefrompng($upload);
}
elseif($details_type[1] == "gif")
{
$tmp = imagecreatefromgif($upload);
}
list($width,$height) = getimagesize($upload);

$new_width=50;
$new_height =($height/$width)*$new_width;
$output= imagecreatetruecolor($new_width,$new_height);
imagecopyresized($output, $tmp, 0,0,0,0,$new_width, $new_height, $width, $height);
$img_name = $path."thumb_".$_FILES["Image"]["name"];
imagejpeg($output, $img_name, 100);
Member Avatar for Member #120589

if everything works fine with other extensions, I'm not sure I know what's going on.

However for extension, I'd use this:

$ext = pathinfo($_FILES['Image']['tmp_name'], PATHINFO_EXTENSION);

THat should work no matter what the location of the 'temporary' directory

Does imagejpeg() work no matter what the input extension?
I very rarely use this. Anybody else?

not used gd library for a few years but I'd suggest changing your code and try saving as a png instead to debug/see if you get the same problems. even if png isn't something you want to use it might help you narrow down the problem.

Thanks both for the tips, Kevin I'll try with png tonight.

I've found exactly why it wasn't working. The image I tried to upload was in CMYK, I've converted it with photoshop and now it works perfectly. Internet Explorer doesn't support CMYK but Firefox and Chrome do.

IE can't do something firefox and chrome and safari and opera can do??? shocking ;)

I want a code to check the size of the image before you update to the website. Who can help me

does most things i need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.