I have a function to create an image which works fine except for sometimes it gives an error on some apparently corrupt jpeg image when the Google bot comes around.
Here is the function:
- Code: Select all
static function printImage($filename, $width = '', $height = '', $watermark = '', $bottomWatermark = '') {
global $gLibPath;
if (!$filename) {
return;
}
// Check for the display width and height
// Note that trying to create an image with a width or a height of 2
// creates an image that contains errors
if ($width < 2 || $height < 2) {
return;
}
// Get the image type
$type = LibImage::getImageType($filename);
// Create the image
if (($type == "jpeg" || $type == "jpg") && (imagetypes() & IMG_JPEG)) {
// Fix the JPEG if it is invalid
/*
See for a while if this function call is the reason for this error message:
Error message: imagecreatefromjpeg():
'/home/web/europasprak.com/account/data/elearning/lesson/image/ChristopheCorm.jpg' is not a valid JPEG file
Filename: /home/engine/lib/image.php Line: 169
*/
LibImage::checkAndFixJPEG($filename, true);
$copy = imagecreatefromjpeg($filename);
} else if ($type == "gif" && (imagetypes() & IMG_GIF)) {
$copy = imagecreatefromgif($filename);
} else if ($type == "png" && (imagetypes() & IMG_PNG)) {
$copy = imagecreatefrompng($filename);
} else if ($type == "wbmp" && (imagetypes() & IMG_WBMP)) {
$copy = imagecreatefromwbmp($filename);
} else {
return;
}
// If the copy failed
if (!$copy) {
// Then create a blank image
$outputImage = imagecreate($width, $height);
$backgroundColor = imagecolorallocate($outputImage, 255, 255, 255);
imagefilledrectangle($outputImage, 0, 0, $width, $height, $backgroundColor);
$type = "png";
} else {
// Get the actual image width and height
$actualWidth = imagesx($copy);
$actualHeight = imagesy($copy);
$outputImage = imagecreatetruecolor($width, $height);
imagecopyresampled($outputImage, $copy, 0, 0, 0, 0, $width, $height, $actualWidth, $actualHeight);
imagedestroy($copy);
}
// Header indicating the image type
header("Content-type:image/$type");
// Create the image
if ($type == "jpeg" && (imagetypes() & IMG_JPEG)) {
imagejpeg($outputImage);
} elseif ($type == "gif" && (imagetypes() & IMG_GIF)) {
imagegif($outputImage);
} elseif ($type == "png" && (imagetypes() & IMG_PNG)) {
imagepng($outputImage);
} elseif ($type == "wbmp" && (imagetypes() & IMG_WBMP)) {
image2wbmp($outputImage);
}
imagedestroy($outputImage);
}
The error is at the line:
$copy = imagecreatefromjpeg($filename);
Here is the error message:
Error message: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 711 extraneous bytes before marker 0xd9
Filename: /home/learnintouch/engine/lib/image.php
Line: 188
Error type: 2
Client: Googlebot-Image/1.0
I wonder what to do to work around this potential error.
Kind Regards,

