我的&x27;。巴布亚新几内亚';在php中,图像不适用于调整图像大小代码

我的&x27;。巴布亚新几内亚';在php中,图像不适用于调整图像大小代码,php,Php,当我将一个jpg文件传递给imgsize.php?w=100&h=100&img=powered\u by.jpg时,它工作得很好 但是我将一个png文件传递给imgsize.php?w=100&h=100&img=mengo.png它不工作 我的imgsize.php文件代码是 $extension = pathinfo($_GET['img']); $extension = $extension[extension]; if ($extension == "jpg" || $extensio

当我将一个jpg文件传递给
imgsize.php?w=100&h=100&img=powered\u 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文件时,它会显示空白图像

我会将缩略图的代码留给您,并将其保存到目标文件夹中

    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 );
    }
}

正如您在if-else条件中检查扩展以生成标题一样,您必须检查是否创建$img2 like

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图像如果创建或存储不正确,通常会产生问题。损坏的图像也可能导致此问题。希望这对您有用。

查看函数名,然后认为png不是jpeg,但手册中可能有与png相同的函数。抱歉,Ernesto我不需要保存图像,但我需要动态调整它们的大小-thanks@Toms只需修改doment,当您执行copy()时,只需返回图像