简体   繁体   English

在 PHP 中调整 PNG 图像的大小

[英]Resize PNG image in PHP

I'm getting a no image display when resizing PNG however the following code works for JPEG.我在调整 PNG 大小时没有图像显示,但以下代码适用于 JPEG。

list($width_orig, $height_orig) = getimagesize( $fileName );

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);

if( $type )){
    switch( $type ){
        case 'image/jpeg':
            $image = imagecreatefromjpeg($fileName);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagejpeg($image_p, null, 100);
        break;

        case 'image/png':
            imagealphablending( $image_p, false );
            imagesavealpha( $image_p, true );
            $image = imagecreatefrompng( $fileName );
            imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagepng($image_p, null, 100);
        break;
    }
}

I've put the headers in but for some reason I'm doing something wrong for png images.我已经放入了标题,但由于某种原因,我对 png 图像做错了。

imagepng($image_p, null, 100)最后一个参数imagepng($image_p, null, 100)应该在0到9之间。

try this: 试试这个:

$image = imagecreatefrompng ( $filename );
$new_image = imagecreatetruecolor ( $width, $height ); // new wigth and height
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
imagecopyresampled ( $new_image, $image, 0, 0, 0, 0, $width, $height, imagesx ( $image ), imagesy ( $image ) );
$image = $new_image;

// saving
imagealphablending($image , false);
imagesavealpha($image , true);
imagepng ( $image, $filename );

see if this works 看看这是否有效

#upload de image
public function upImagem($imagem, $dir, $res, $id, $tam){

    $arquivo = $imagem;
    $arq_nome = $arquivo['name'];
    $ext = $arquivo['type'];

    $nome=$this->RenImg($arquivo, $dir, $id, $tam);
    $imagem = $arquivo['tmp_name'];


    if($ext=='image/jpeg'){
    $img = imagecreatefromjpeg($imagem);
    }
    elseif($ext=='image/png'){
    $img = imagecreatefrompng($imagem);
    }
    elseif($ext=='image/gif'){
    $img = imagecreatefromgif($imagem);
    }


    if(($ext=='image/png') or ($ext=='image/gif')){
    list($x, $y) = getimagesize($arquivo['tmp_name']);
    }
    elseif($ext=='image/jpeg'){
    $x = imagesx($img);//original height
    $y = imagesy($img);//original width
    }

    $altura  = $res[1];
    $largura = $res[0];


    $nova  = imagecreatetruecolor($largura,$altura);
    $preto = imagecolorallocate($nova, 0, 0, 0);

    if(($ext=='image/png') or ($ext=='image/gif')){
    imagealphablending($nova , false);
    imagesavealpha($nova , true);
    }

    if($ext=='image/png'){
    imagecolortransparent ($nova, $preto);
    imagecopymerge($img, $nova, 0, 0, 0, 0, imagesx($nova), imagesy($nova), 100);
    imagecopyresized($nova,$img,0,0,0,0,$largura,$altura, $x, $y );
    }
    else {
    imagecopyresampled($nova,$img,0,0,0,0,$largura,$altura,$x,$y);
    }


    if($ext=='image/jpeg'){
    imagejpeg($nova,$nome,99);
    }

    elseif($ext=='image/gif'){
    imagealphablending($nova , false);
    imagesavealpha($nova , true);
    imagegif($nova,$nome,99);
    }

    elseif($ext=='image/png'){
    imagealphablending($nova , false);
    imagesavealpha($nova , true);
    imagepng($nova,$nome);
    }

    imagedestroy($img);
    imagedestroy($nova);

}



#renames the image
public function RenImg($arq,$dir,$id,$tam){

    $arq_nome = $arq['name'];
    $arq_nome2=str_replace('.jpg','',$arq['name']);//renomeia o arquivo
    $arq_nome2=str_replace('.png','',$arq_nome2);//renomeia o arquivo
    $arq_nome2=str_replace('.gif','',$arq_nome2);//renomeia o arquivo
    //$new_name = md5($arq_nome);
    $ext = $this->getExt($arq_nome);
    $nome = $dir.$id.$tam.'.jpg';//.'.'.$ext

    return $nome;

}


#capture the file extension
public function getExt($arq){

    $ext = pathinfo($arq, PATHINFO_EXTENSION);

    return $ext;

}

This is a code i use for JPG/PNG rezise.这是我用于 JPG/PNG rezise 的代码。

$imagename="default";
if (isset ($_FILES['arquivo'])){              
          $imagename = $imagename.".jpg";
          $source = $_FILES['arquivo']['tmp_name'];
          $target = "images/tmp/".$imagename;
          $type=$_FILES["arquivo"]["type"];
          
          //JPG or JPEG
          if($type=="image/jpeg" || $type=="image/jpg"){
          move_uploaded_file($source, $target);
          $imagepath = $imagename;
          $save = "images/" . $imagepath; //Path to save the image
          $file = "images/tmp/" . $imagepath; //path to orginal size image
          list($width, $height) = getimagesize($file) ;
          $modwidth = 1920;
          $diff = $width / $modwidth; // Use $modheight = $idff to mantain aspect ratio
          $modheight = 1080;   
          $tn = imagecreatetruecolor($modwidth, $modheight) ;
          $image = imagecreatefromjpeg($file) ;
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
          echo "<center><b><h5>Image was updated!</h5></b>";
          imagejpeg($tn, $save, 100) ;
          }
          //PNG
          elseif($type=="image/png"){ 
          move_uploaded_file($source, $target);
          $imagepath = $imagename;
          $save = "images/" . $imagepath; //Path to save the image
          $file = "images/tmp/" . $imagepath; //path to orginal size image
          list($width, $height) = getimagesize($file) ;
          $modwidth = 1920;
          $diff = $width / $modwidth; // Use $modheight = $idff to mantain aspect ratio
          $modheight = 1080;   
          $tn = imagecreatetruecolor($modwidth, $modheight) ;
          imagealphablending( $tn, false );
          imagesavealpha( $tn, true );
          $image = imagecreatefrompng($file) ;
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
          echo "Image was updated!";      
          imagepng($tn, $save, 9) ;       
          }
        else{
            echo "Error!";
            }
      }

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

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