I am writing a script that will download a file from user input, resize it to a thumbnail and add two new file names to the database.
However, I canβt understand for life how to get PHP to detect a MIME type image and then pass it to the header. Here is the code, I put comments to try to make it as clear as possible:
$picture = $_FILES['picture']['name']; $file = 'picture/'.$picture.''; $save = 'thumb/tn-'.$picture.''; $thumb = 'tn-'.$picture.''; list($width, $height) = getimagesize($file); $size = getimagesize($file); $fp = fopen($file, "r"); if ($size && $fp) { header("Content-type:".$size['mime']); fpassthru($fp); exit; } else { echo 'Error getting filetype.'; } $modwidth = 300; $modheight = 200; if (($width > 301) || ($height > 201)) { $tn = imagecreatetruecolor($modwidth, $modheight); if ($size['mime'] == "image/png") { $image = imagecreatefrompng($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagepng($tn, $save, 100); } if ($size['mime'] == "image/jpeg") { $image = imagecreatefromjpeg($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagejpeg($tn, $save, 100); } if ($size['mime'] == "image/gif") { $image = imagecreatefromgif($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagegif($tn, $save, 100); } } else { echo 'Your file is too small.'; }
So, here is the part that I donβt understand: the code works fine when I load .jpeg, but if it is PNG or GIF, it displays a page with the image saying 127.0. 0.1: 8888 'is not displayed because it contains errors.' I assume that it should not have the correct header type of the header, but maybe something else?
When I select .jpeg, it automatically loads into my image folder and generates a thumbnail in the Thumbnail folder as I want it.
But as soon as I try it with PNG and GIF, it fails. Should I miss something obvious?
Is this code good for what I'm trying to execute?
Thanks in advance,