I have this code:
$image = imagecreatefromjpeg($filename);
$size = getimagesize($filename, $info);
var_dump($image);
var_dump($size);
And I receive this output:
Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error
Warning: imagecreatefromjpeg(): 'my_image.jpg' is not a valid JPEG file
bool(false)
array(7) {
[0]=>
int(22)
[1]=>
int(30)
[2]=>
int(2)
[3]=>
string(22) "width="22" height="30""
["bits"]=>
int(8)
["channels"]=>
int(3)
["mime"]=>
string(10) "image/jpeg"
}
I got warn, that image is not a valid JPEG file and don't load that file, but getimagesize says that is JPEG file (mime type). What is happening and how can I fix this?
I tried this, but it didn't help me:
Interestingly, on the local server everything works good (with the same image of course).
Create Function isPNG
<?php
function isPNG($file){
return preg_match('/'.quotemeta('PNG').'/i', file_get_contents($file));
}
?>
Edit your code
<?php
if(preg_match('/\.(png)$/i', $filename) || isPNG($filename)){
$image = imagecreatefrompng($filename);
}else{
$image = imagecreatefromjpeg($filename);
}
$size = getimagesize($filename, $info);
var_dump($image);
var_dump($size);
?>
Good luck!