简体   繁体   English

我的'.png'图片无法用于调整php中​​的图像代码

[英]my '.png' image not work for resize image code in php

when i pass a jpg file to imgsize.php?w=100&h=100&img=powered_by.jpg then its work well but i pass a png file to imgsize.php?w=100&h=100&img=mengo.png it's not working 当我将jpg文件传递给imgsize.php?w=100&h=100&img=powered_by.jpg然后它工作得很好但我将png文件传递给imgsize.php?w=100&h=100&img=mengo.png它不能正常工作

my imgsize.php file code is 我的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);
    }
}

any solution for it ? 任何解决方案吗? its display blank image when i pass it out a png file 当我将它传递给png文件时,它显示空白图像

As you have checked for the extension in if else condition to generate header the same way you would have to check for creating $img2 like 正如您在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']);
}

One another thing i would like to add for your info is that check for image mime type rather that checking extension. 我要为您的信息添加的另一件事是检查图像mime类型而不是检查扩展名。 for that you can user getimagesize function, which return an array and you can check for mime type by $retun_array['mime']. 为此你可以使用getimagesize函数,它返回一个数组,你可以通过$ retun_array ['mime']检查mime类型。

PNG images often creates problem if not created or stored properly. 如果未正确创建或存储,PNG图像通常会产生问题。 Corrupt images may cause this problem too. 损坏的图像也可能导致此问题。 Hope this may work for you. 希望这可能对你有用。

I leave you my code for thumbnail images and save them into a destination folder 我留下了我的缩略图图像代码并将它们保存到目标文件夹中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM