解决FPDF报错:FPDF error: Not a JPEG file / FPDF error: Not a PNG file
最近有个项目需要用到FPDF,但是输出的时候报错:
FPDF error: Not a JPEG file: http://***/data/attachment/forum/201603/19/105428nzhz6z9eh6qyyiw8.jpg
找到报错的代码:
用到了getimagesize函数来做判断,那就简单试下:
<?php /** * User: Administrator * Date: 2016/3/22 11:54 */ header("Content-type: text/html; charset=utf-8"); $a = getimagesize('105428nzhz6z9eh6qyyiw8.jpg'); var_dump($a);
结果如下:
array (size=6) 0 => int 282 1 => int 500 2 => int 3 3 => string 'width="282" height="500"' (length=24) 'bits' => int 8 'mime' => string 'image/png' (length=9)
发现了比较奇怪的现象:后缀为jpg,但是mime的结果却是image/png!!!!
发现问题就比较好解决了,由于我的图片是其他地方用到的,那就是复制个105428nzhz6z9eh6qyyiw8.jpg到105428nzhz6z9eh6qyyiw8.png即可,试了下,完美解决!
附上代码:
$imgurl=$imgtemp['attachment']; $imgtype = substr($imgurl,strrpos($imgurl,'.')+1); $a = getimagesize('../data/attachment/forum/'.$imgurl); $mime =str_replace('jpeg','jpg', str_replace('image/','',$a['mime'])); if($imgtype != $mime){ $imgurl_new = '/data/attachment/forum/'.str_replace($imgtype,$mime,$imgurl); if(!is_dir($imgurl_new)){ $jpg = '/data/attachment/forum/'.$imgurl; $topng = copy('..'.$jpg,'..'.$imgurl_new); $imgurl = str_replace('/data/attachment/forum/','',$imgurl_new); } } $mypath="http://$baseurl/data/attachment/forum/".$imgurl; $pdf->Image(''.$mypath.'');