1

I want to resize the image file after upload with the help of imagecreatefromjpeg function but this function is unable to access the file from the folder as it's throwing the error i.e., **

imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in D:\xampp\htdocs\resize\index.php

** but file is uploaded & I wrote the following code.

<form method="post" enctype="multipart/form-data">
    <input type="file" name="f1">
    <input type="submit" name="btn" value="Upload">
</form>

<?php
ini_set("memory_limit","256M");
if(isset($_POST['btn'])) 
{

    if(move_uploaded_file($_FILES['f1']['tmp_name'], "images/".$_FILES['f1']['name'])) 
    {          


        $filename = "images/".$_FILES['f1']['name'];


        $original_info = getimagesize($filename);
         $original_w = $original_info[0];
        $original_h = $original_info[1];
        echo "<img src =$filename>";
        if( ini_get('allow_url_fopen') ) {
          // it's enabled, so do something        

            $original_img = imagecreatefromjpeg($filename);         
            $thumb_w = 100;
            $thumb_h = 60;
            $thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
            $thumb_filename = "new.jpg";
            imagecopyresampled($thumb_img, $original_img,
                               0, 0,
                               0, 0,
                               $thumb_w, $thumb_h,
                               $original_w, $original_h);
            imagejpeg($thumb_img, $thumb_filename);
            imagedestroy($thumb_img);
            imagedestroy($original_img); 
        }       
    }   
}
?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 3
    Possible duplicate of [Warning: imagejpeg() \[function:imagejpeg\]: gd-jpeg: JPEG library reports unrecoverable error](https://stackoverflow.com/questions/10611883/warning-imagejpeg-functionimagejpeg-gd-jpeg-jpeg-library-reports-unrecov) – jeprubio May 29 '18 at 06:36
  • I'm sure someone else will answer your specific question but I've found (doing the same thing) that most servers nowadays include the ImageMagick library which makes it a whole lot easier. It's worth a look. – Mike May 29 '18 at 06:38
  • I hve seen that, but not useful for me –  May 29 '18 at 06:39
  • are you sure you are uploading a `jpeg` image? as the function name says `imagecreatefromjpeg` it only accepts jpeg I guess. – Sahith Vibudhi May 29 '18 at 06:54
  • yes................ –  May 29 '18 at 07:07

1 Answers1

0

You must make sure of the type of file.

<?php
    $type = exif_imagetype($filename);
    // types 1=>gif, 2=>jpg, 3=>png, 6=>bmp
    switch ($type) {
        case 1 : $img = imageCreateFromGif ( $src );break;
        case 2 : $img = imageCreateFromJpeg( $src );break;
        case 3 : $img = imageCreateFromPng ( $src );break;
        case 6 : $img = imageCreateFromBmp ( $src );break;
    }
    // or if you cannot use exif_imagetype
    // you can use getImageSize
    $type = getImageSize($filename);
    switch ($type) {
        case 'image/gif'  : $img = imageCreateFromGif ( $filename ); break;
        case 'image/jpeg' : $img = imageCreateFromJpeg( $filename ); break;
        case 'image/png'  : $img = imageCreateFromPng ( $filename ); break;
        case 'image/bmp'  : $img = imageCreateFromBmp ( $filename ); break;
    }
//// than you can create new file with resize or crop...

?>