imagecreatefromjpeg出错

时间:2013-12-14 19:45:11

标签: php

我有一个图像,我想要创建不同的大小 (缩略图,个人资料,例如小)

我的脚本是(部分内容......)

$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;
        }

当我调整配置文件的大小时,小我没有错误调整大小时缩略图我有这个错误

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

并创建替代图片

由于

1 个答案:

答案 0 :(得分:1)

 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);
  

试试这个