Image Image Image Image Image

© Copyright 2012 ABCoder | Email | RSS

Scroll to Top

To Top





By abcoder

Problem with resizing corrupted images using PHP image functions

On 05, Jun 2009 | In PHP | By abcoder

As a programmer we must always think about the exceptional situations. Generally I use my own function to resize uploaded images. It supports jpg, gif, png images with transparency which is “almost” okay.

// example use
// resizeImage("corrupted_image_1.jpg", "corrupted_image_resized.jpg", 100, 100, "jpg");
function chkImgExt($n){
	$tmp = explode('.', $n);
	$ext = strtolower(array_pop($tmp));
	if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png')
		return $ext;
		return false; 

function resizeImage($src, $dest, $w, $h, $ext){
	$real_path = dirname(__FILE__) . '/';
	$tmpFile = $real_path."tmp_images/".time().'TMP.'.$ext;
	copy($src, $tmpFile); // you may use move_uploaded_file() if the $src is a $_FILES referance
	@chmod($tmpFile, 0777);
	$src = $tmpFile;
	list($width, $height) = @getimagesize($src);
	$new_width = $w;
	$new_height = $h;

		case 'jpg':
			$image = imagecreatefromjpeg($src);
		case 'jpeg':
			$image = imagecreatefromjpeg($src);
		case 'gif':
			$image = imagecreatefromgif($src);
		case 'png':
			$image = imagecreatefrompng($src);

	// Resample
	$image_p = @imagecreatetruecolor($new_width, $new_height);
	if ( ($ext == 'gif') || ($ext == 'png') ) {
		$trnprt_indx = imagecolortransparent($image);
		// If we have a specific transparent color
		if ($trnprt_indx >= 0) {
			// Get the original image's transparent color's RGB values
			$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
			// Allocate the same color in the new image resource
			$trnprt_indx = imagecolorallocate($image_p, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
			// Completely fill the background of the new image with allocated color.
			imagefill($image_p, 0, 0, $trnprt_indx);
			// Set the background color for new image to transparent
			imagecolortransparent($image_p, $trnprt_indx);
		// Always make a transparent background color for PNGs that don't have one allocated already
		elseif ($ext == 'png'){
			// Turn off transparency blending (temporarily)
			imagealphablending($image_p, false);
			// Create a new transparent color for image
			$color = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
			// Completely fill the background of the new image with allocated color.
			imagefill($image_p, 0, 0, $color);

			// Restore transparency blending
			imagesavealpha($image_p, true);

	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

	// Output
		case 'jpg':
			imagejpeg($image_p, $dest, 100);
		case 'jpeg':
			imagejpeg($image_p, $dest, 100);
		case 'gif':
			imagegif($image_p, $dest);
		case 'png':
			imagepng($image_p, $dest);

	return true;

But when I tried resizing these 2 images (corrupted_image_1.jpg, corrupted_image_2.jpg) it failed! The error is:

gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9

I don’t know what does it mean!

First I thought the problem may be due to large file size or GD. I tried with larger file and it worked fine! Then I opened the corrupted images with photoshop and just save as jpg again, and yes it worked. It does not make good sense to me. How a general user will do that? I searched a lot on Google and got a lot of alternative image resizing codes and none of them worked. :(

Now what? Yes I used phpThumb long ago and to me (also most of you) it feels like using a lot of unnecessary codes just for simply resizing a silly image! I can’t believe phpThumb created the thumbnails of both of the corrupted images! yes, using GD! no imagemagick.

I have no idea how phpThumb do it? I never dare to look inside their codes :P
Here is the code using phpThumb to resize the images:

	$phpThumb = new phpThumb();
	$capture_raw_data = false; 	$phpThumb->resetObject();
	$phpThumb->setSourceFilename($targetFile); // your source image file
	$output_filename = $tpath; // output file path
	$phpThumb->setParameter('w', 100); // thumbnail width
	$phpThumb->setParameter('q', 100); // thumbnail quality
	$phpThumb->setParameter('config_output_format', 'jpeg'); // preferred thumbnail format 

	if ($phpThumb->GenerateThumbnail()){
			// success
		} else {
			$msg = "Error during resizing \n" . $phpThumb->fatalerror . '  ' . $phpThumb->debugmessages;
	} else {
		$msg = "Error with file\n" . $phpThumb->fatalerror . '  ' . $phpThumb->debugmessages;

If you are using your own function for image resizing please check with these 2 files (corrupted_image_1.jpg, corrupted_image_2.jpg). If you see it doesn’t work I would suggest to use phpThumb, it’s free. You should also make sure your web host has the latest php version on the server or go with a php hosting provider, that specializes in it.

Thanks a lot to phpThumb for their amazing work!

Tags | , , , , , , , , , , , ,


  1. well be good n stay outta trouble or be good at it… lol …. ttyl, britney spears bikini, pxaog, vote for the worst, ygotnh, job application for home depot, wuwd, tungkol sa alamat, 3954, culos y panochas, =-],

  2. I must commend you on the awesome job that you’ve done on your site., printable alphabet stencils, oidh, what do we call a change to the constitution, enroyb, free alphabetical order worksheets, =]], free tracking cell phone, lweh, best mate for capricorn, 8-(((,

  3. I think your site is great! Thanks for taking the time to make it., jada fire porn pics xxx, 98630, jada fire tit fuck, 449, jada fire tgp, :-[[, jada fire teacher renae cruz, 7501, jada fire teach renea cruz, 435,

  4. DUDE !!! What the Hell is up !! i need to add you to my topfriend, underground alien bases, %OO, xerox ducati race replica decals, 0996, tt silver replica, 632945, phone sex live call back, >:-[, cheri dress flower girl mon, :-[[[, apollo sun club, >:(, what is area 51, jctf, sexy live cams, ohj, gay anal sex tip, 8-]], david wells horoscopes, xjv,

  5. I’m obtaining an important minimal trouble I cant seem to be capable of subscribe your personal feed, I’m dealing with the search engines reader.

  6. Hello there! I really enjoy reading your site! If you keep making great posts I’ll return every single day to keep reading!

  7. I’m speechless. This is a excellent weblog and really enticing too. Great work! That’s no longer actually much originating from a novice publisher like me, nevertheless it’s all I may just say after diving into you. Nice grammar and vocabulary. Now not like other blogs. You truly understand what you?re talking about too. Such a lot that you helped me want to explore more. Your weblog has grow to be a stepping stone personally, my pal.

  8. Very Helpful

    Bharat Matrimony

  9. Nice


  10. excellent article about, however! i really don’t strive to be so negative however i do believe that your site would look a little better if you had a bit of gray in it :P No, you won’t have to agree… this really is merely my simple opinion. Many thanks for a good post anyhow! =) Sincerely, Resan