2

Possible Duplicate:
Save image from one format to another with php gd2

I am converting any image format to JPEG or JPG. Code for converting image to jpg is

function converttojpg($originalimg)
{
$filetype = @end(explode(".", $originalimg));
echo $originalimg . " asdasdfs";
if (strtolower($filetype) == 'jpg' or strtolower($filetype) == 'jpeg') 
    {
    $srcImg = imagecreatefromjpeg($originalimg);
    imagejpeg($srcImg,$originalimg,100);
    imagedestroy($srcImg);
    } 
if (strtolower($filetype) == 'png') 
    {
    $srcImg = imagecreatefrompng($originalimg);
    imagejpeg($srcImg,$originalimg,100);
    imagedestroy($srcImg);
    } 
if (strtolower($filetype) == 'gif') 
    {
    echo "executed";
    $srcImg = imagecreatefromgif($originalimg);
    imagejpeg($srcImg,$originalimg,100);
    imagedestroy($srcImg);
    }
}

After that i resize that converted image, whose code is

function resizeImage($originalImage,$toWidth,$toHeight)
{
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale)
    {
    $new_width = round($width * (1/$yscale));
    $new_height = round($height * (1/$yscale));
    }
else 
    {
    $new_width = round($width * (1/$xscale));
    $new_height = round($height * (1/$xscale));
    }
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp     = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height           );
return $imageResized;
}

and on Every image it reports errors and warning which are

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 33

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/corrupted_image_1.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 33

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 34

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 35

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 22

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/53.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 22

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 23
userphotos/MG_diesel-04.gif_thumb.png asdasdfsuserphotos/Porsche_911_996_Carrera_4S.jpg asdasdfs


Please help me in solving that problem. I have to complete it.

Community
  • 1
  • 1
Ahmed Khakwani
  • 410
  • 2
  • 8
  • 18

2 Answers2

0

Don't use extension to determine file type. To check if given file is an image and in format that allows GD to manipulate it, use getimagesize

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
-1
<?php
    $image = @imagecreatefromstring(file_get_contents($file_path));
        if ($image === false) {
            throw new Exception (sprintf('invalid image or file not found, file=%s', $file_path));
        }
?>

imagecreatefromstring detects image type and creates an image resource, otherwise exception is thrown.

Mikk
  • 2,209
  • 3
  • 32
  • 44
  • Using @ to suppress errors is never a good idea, especially if you are trying t work out why a script is not working as expected. – vascowhite Mar 18 '12 at 19:37