在 PHP 中调整 PNG 图像大小

问题描述 投票:0回答:5

调整 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;
    }
}

我已经放入了标题,但由于某种原因我对 png 图像做了一些错误的事情。

php resize png gd
5个回答
16
投票

imagepng($image_p, null, 100)
中的最后一个参数应介于 0 和 9 之间。


8
投票

试试这个:

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

0
投票

看看这是否有效

#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;

}

0
投票

这是我用于 JPG/PNG 调整大小的代码。

$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);
    } elseif ($type == "image/png") { //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!";
    }
}

0
投票

原图

图片链接:https://shop.mmtcpamp.com/uploads/product/gold-coin-tola-2.png

函数 createBanner($backgroundWidth, $backgroundHeight, $insideImageWidth, $insideImageHeight, $backgroundColor, $insideImagePath) {

   $banner = imagecreatetruecolor($backgroundWidth, $backgroundHeight);


// Convert hex to RGB for the background color
list($r, $g, $b) = sscanf($backgroundColor, "#%02x%02x%02x");


// Allocate the background color (white)
$bgColor = imagecolorallocate($banner, $r, $g, $b);


// Fill the banner with the background color (white)
imagefill($banner, 0, 0, $bgColor);


// Load the inside image with transparency support
$insideImage = imagecreatefrompng($insideImagePath);


// Enable alpha blending and save full alpha channel information
imagesavealpha($insideImage, true);


imagealphablending($insideImage, false);

// Resize the inside image to fit within the specified dimensions
$resizedInsideImage = imagecreatetruecolor($insideImageWidth, $insideImageHeight);


// Fill the resized inside image with the background color (white)
imagefill($resizedInsideImage, 0, 0, $bgColor);

imagecopyresampled(
    $resizedInsideImage,
    $insideImage,
    0,
    0,
    0,
    0,
    $insideImageWidth,
    $insideImageHeight,
    imagesx($insideImage),
    imagesy($insideImage)
);


// Capture the output of the image without saving to a file
ob_start();

imagepng($resizedInsideImage);
$imageData = ob_get_contents();

ob_end_clean();

// Clean up resources
imagedestroy($banner);

imagedestroy($insideImage);

imagedestroy($resizedInsideImage);

// echo $imageData;
// die;

return $imageData;

}

// 文件路径的用法示例

$背景宽度= 580;

$背景高度= 346;

$insideImageWidth = 280;

$insideImageHeight = 280;

$backgroundColor = "#ffffff"; // 白色背景

$insideImagePath = 'https://shop.mmtcpamp.com/uploads/product/gold-coin-tola-2.png'; // 替换为你的图片路径

//调用函数直接在HTML中输出图像

createBanner($backgroundWidth, $backgroundHeight, $insideImageWidth, $insideImageHeight, $backgroundColor, $insideImagePath);

// 现在$imageData包含了图像数据,你可以根据需要使用它。

//直接以HTML形式输出图像

##调整图像大小

回声'';

© www.soinside.com 2019 - 2024. All rights reserved.