我需要使用exif_imagetype()
来识别图像的文件类型。
这是我的代码:
$imgSrc = "image.jpg";
if (exif_imagetype($imgSrc) == IMAGETYPE_GIF) {
$filetype = 'The picture is GIF';
}
if (exif_imagetype($imgSrc) == IMAGETYPE_JPEG) {
$filetype = 'The picture is JPEG';
}
if (exif_imagetype($imgSrc) == IMAGETYPE_PNG) {
$filetype = 'The picture is PNG';
}
echo $filetype;
我遇到的问题是$filetype
不仅回显了我想要的字符串,而且回显了(我认为是)exif_imagetype()
部分输出:
The picture is JPEG����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG
v80), default quality ��C $.' ",#(7),01444'9=82
<.342��C 2!!22222222222222222222222222222222222222222222222222��
正如你所看到它确实回应了我的字符串,但我不知道为什么其余部分在输出中。
有人能告诉我为什么会这样吗? 提前谢谢。
编辑:在下面添加完整的php页面
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//check file type + saving the image into memory
if (exif_imagetype($imgSrc) == IMAGETYPE_GIF) {
$filetype = 'The picture is GIF';
$myImage = imagecreatefromgif($imgSrc);
}
if (exif_imagetype($imgSrc) == IMAGETYPE_JPEG) {
$filetype = 'The picture is JPEG';
$myImage = imagecreatefromjpeg($imgSrc);
}
if (exif_imagetype($imgSrc) == IMAGETYPE_PNG) {
$filetype ='The picture is PNG';
$myImage = imagecreatefrompng($imgSrc);
}
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
//read EXIF header from uploaded file
$exif = exif_read_data($imgSrc);
//fix the Orientation if EXIF data exist
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$myImage = imagerotate($myImage,90,0);
break;
case 3:
$myImage = imagerotate($myImage,180,0);
break;
case 6:
$myImage = imagerotate($myImage,-90,0);
break;
}
}
// copying the part into thumbnail
$thumbSize = 200;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
?>