Barno
Barno

Reputation: 3331

Error on imagecreatefromjpeg

I have an image and i want create different size like (thumbnails, profiles, small for example)

my script is (part of this..)

$new_img = @imagecreatetruecolor($new_width, $new_height);
        switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
            case 'jpg':
            case 'jpeg':
                $src_img = @imagecreatefromjpeg($file_path);

            /* See if it failed */
            if(!$src_img)
            {
                /* Create a black image */
                $src_img  = imagecreatetruecolor(150, 30);
                $bgc = imagecolorallocate($src_img, 255, 255, 255);
                $tc  = imagecolorallocate($src_img, 0, 0, 0);

                imagefilledrectangle($src_img, 0, 0, 150, 30, $bgc);

                /* Output an error message */
                imagestring($src_img, 1, 5, 5, 'Error loading ' . $file_path, $tc);
            }
        ...more cases...

            default:
            $src_img = null;
        }

when i resize for profiles, small i haven't error when resize for thumbnails i have this error

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error:

and create alternative image

Thanks

Upvotes: 0

Views: 1358

Answers (1)

amarjit singh
amarjit singh

Reputation: 461

 switch(strtolower($_FILES['fileupload']['type']))
                        {
                            case 'image/jpeg':
                                $filename = imagecreatefromjpeg('imagepath/'.$post['fileupload']['name']);
                                break;
                            case 'image/png':
                                $filename = imagecreatefrompng('imagepath/'.$post['fileupload']['name']);
                                break;
                            case 'image/gif':
                                $filename = imagecreatefromgif('imagepath/'.$post['fileupload']['name']);
                                break;
                            default:
                                exit('Unsupported type: '.$_FILES['fileupload']['type']);
                        }

                        ob_start();
                        imagejpeg($filename);
                        // large image
                        $large = base64_encode(ob_get_contents()); // returns output

                        $mainimgWidth  = imagesx($filename);
                        $mainimgHeight = imagesy($filename);

                        $thumbWidth = intval($mainimgWidth / 4);
                        $thumbHeight = intval($mainimgHeight / 4);
                        $new = imagecreatetruecolor($thumbWidth, $thumbHeight);
                        $backgroundColor = imagecolorallocate($new, 255, 255, 255);
                        imagefill($new, 0, 0, $backgroundColor);
                        imagecopyresampled($new, $filename, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainimgWidth, $mainimgHeight);

                        /** Catch the imagedata */
                        ob_start();

                        imagejpeg($new);



                        $data = ob_get_clean();

                        // Destroy resources
                        imagedestroy($filename);
                        imagedestroy($new);

                        // Set new content-type and status code
                        $thumb = base64_encode($data);

TRY LIKE THIS

Upvotes: 1

Related Questions