当我将jpg文件传递给imgsize.php?w=100&h=100&img=powered_by.jpg
时,它的工作正常
但我将一个png文件传递给imgsize.php?w=100&h=100&img=mengo.png
它无法正常工作
我的imgsize.php文件代码是
$extension = pathinfo($_GET['img']);
$extension = $extension[extension];
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
header("Content-type: image/jpeg");
}
if ($extension == "png") {
header("Content-type: image/png");
}
if ($extension == "gif") {
header("Content-type: image/gif");
}
$img = $_GET['img'];
$nwidth = $_GET['w'];
$nheight = $_GET['h'];
$img2 = imagecreatefromjpeg($_GET['img']);
$width = imagesx($img2);
$height = imagesy($img2);
$ratio = $width / $height;
$new_nwidth = $nwidth;
$new_nheight = floor($height * ($nwidth / $width));
$im = imagecreatefromjpeg($img) or $im = imagecreatefrompng($img) or $im = imagecreatefromgif($img) or $im = false;
if (!$im) {
} else {
$thumb = imagecreatetruecolor($new_nwidth, $new_nheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $new_nwidth, $new_nheight, $transparent);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $new_nwidth, $new_nheight, $width, $height);
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
imagejpeg($thumb, null, 100);
}
if ($extension == "png") {
imagepng($thumb, null, 9);
}
if ($extension == "gif") {
imagegif($thumb, null, 100);
}
}
任何解决方案吗?当我将它传递出一个png文件时,它显示空白图像
答案 0 :(得分:2)
正如您在if else条件中检查扩展名生成标题一样,您必须检查创建$ img2
的方式if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
$img2 = imagecreatefromjpeg($_GET['img']);
}
if ($extension == "png") {
$img2 = imagecreatefrompng($_GET['img']);
}
if ($extension == "gif") {
$img2 = imagecreatefromgif($_GET['img']);
}
我想为您的信息添加的另一件事是检查图像mime类型而不是检查扩展名。为此你可以使用getimagesize函数,它返回一个数组,你可以通过$ retun_array ['mime']检查mime类型。
如果没有正确创建或存储,PNG图像通常会产生问题。损坏的图像也可能导致此问题。希望这对你有用。
答案 1 :(得分:0)
我留下了我的缩略图代码,并将它们保存到目标文件夹
function thumbail($forcedwidth, $forcedheight, $sourcefile, $destfile){
$fw = $forcedwidth;
$fh = $forcedheight;
$is = getimagesize( $sourcefile );
$ancho = $is[0];
$alto = $is[1];
if($ancho > $forcedwidth) {
$ancho2 = $forcedwidth;
$por = (100 * $forcedwidth)/$ancho;
$alto2 = ($alto * $por)/100;
$t = 1;
}else{
$ancho2 = $ancho;
$alto2 = $alto;
$t = 2;
}
if($alto > $forcedheight) {
$alto2 = $forcedheight;
$por = (100 * $forcedheight)/$alto;
$ancho2 = ($ancho * $por)/100;
$t = 1;
}else{
$ancho2 = $ancho;
$alto2 = $alto;
}
if ($t == 1){
$img_src = imagecreatefromjpeg( $sourcefile );
$img_dst = imagecreatetruecolor( $ancho2, $alto2 );
imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $ancho2, $alto2, $is[0], $is[1] );
if(!imagejpeg($img_dst, $destfile, 90 )){
exit();
}
}else if ($t == 2){
copy( $sourcefile, $destfile );
}
}