Problem with resizing corrupted images using PHP image functions
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.
<?php
// 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;
else
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;
switch($ext){
case 'jpg':
$image = imagecreatefromjpeg($src);
break;
case 'jpeg':
$image = imagecreatefromjpeg($src);
break;
case 'gif':
$image = imagecreatefromgif($src);
break;
case 'png':
$image = imagecreatefrompng($src);
break;
}
// 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);
if(file_exists($dest)){
@unlink($dest);
}
// Output
switch($ext){
case 'jpg':
imagejpeg($image_p, $dest, 100);
break;
case 'jpeg':
imagejpeg($image_p, $dest, 100);
break;
case 'gif':
imagegif($image_p, $dest);
break;
case 'png':
imagepng($image_p, $dest);
break;
}
imagedestroy($image_p);
unlink($tmpFile);
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:
<?php
require_once('phpthumb/phpthumb.class.php');
$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()){
if($phpThumb->RenderToFile($output_filename)){
// 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!
Related posts:
16 Comments to “Problem with resizing corrupted images using PHP image functions”
Leave a Reply



My Elance Profile
Hi, Congratulations to the site owner for this marvelous work you’ve done. It has lots of useful and interesting data.
[...] Problem with resizing corrupted images using PHP image functions [...]
have a great week,
Great! ) thanks
=)
;-)
damn ))
great )
yep
world of warcraft news
world of warcraft news
world of warcraft gold
wow news
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, =-],
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-(((,
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,
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,
[...] [...]