PHP 图像调整大小功能无法正常工作

发布于 2024-12-07 05:30:06 字数 2328 浏览 1 评论 0原文

我想调整 PNG 图像的透明度,请帮忙。这是代码:

function createThumb($upfile, $dstfile, $max_width, $max_height){
   $size = getimagesize($upfile);
   $width = $size[0];
   $height = $size[1];
   $x_ratio = $max_width / $width;
   $y_ratio = $max_height / $height;
   if( ($width <= $max_width) && ($height <= $max_height)) {
           $tn_width = $width;
           $tn_height = $height;
   } elseif (($x_ratio * $height) < $max_height) {
           $tn_height = ceil($x_ratio * $height);
           $tn_width = $max_width;
   } else {
           $tn_width = ceil($y_ratio * $width);
           $tn_height = $max_height;
   }
   if($size['mime'] == "image/jpeg"){
           $src = ImageCreateFromJpeg($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imageinterlace( $dst, true);
           ImageJpeg($dst, $dstfile, 100);
   } else if ($size['mime'] == "image/png"){
        $src = ImageCreateFrompng($upfile);
        $dst = ImageCreateTrueColor($tn_width, $tn_height);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

      // integer representation of the color black (rgb: 0,0,0)
     $background = imagecolorallocate($dst, 255, 255, 0);
     // removing the black from the placeholder
     imagecolortransparent($dst, $background);

     // turning off alpha blending (to ensure alpha channel information
     // is preserved, rather than removed (blending with the rest of the
     // image in the form of black))
     imagealphablending($dst, false);

     // turning on alpha channel information saving (to ensure the full range
     // of transparency is preserved)
     imagesavealpha($dst, true);


              Imagepng($dst, $dstfile);

      } else {

           $src = ImageCreateFromGif($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imagegif($dst, $dstfile);
   }
}

图像源:

在此处输入图像描述

当前结果(我不想要)是:

在此处输入图像描述

如何调整图像大小并保持背景颜色的透明度?需要帮助,请。谢谢。

I want to resize an image PNG with transparence plz help. Here is the code :

function createThumb($upfile, $dstfile, $max_width, $max_height){
   $size = getimagesize($upfile);
   $width = $size[0];
   $height = $size[1];
   $x_ratio = $max_width / $width;
   $y_ratio = $max_height / $height;
   if( ($width <= $max_width) && ($height <= $max_height)) {
           $tn_width = $width;
           $tn_height = $height;
   } elseif (($x_ratio * $height) < $max_height) {
           $tn_height = ceil($x_ratio * $height);
           $tn_width = $max_width;
   } else {
           $tn_width = ceil($y_ratio * $width);
           $tn_height = $max_height;
   }
   if($size['mime'] == "image/jpeg"){
           $src = ImageCreateFromJpeg($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imageinterlace( $dst, true);
           ImageJpeg($dst, $dstfile, 100);
   } else if ($size['mime'] == "image/png"){
        $src = ImageCreateFrompng($upfile);
        $dst = ImageCreateTrueColor($tn_width, $tn_height);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

      // integer representation of the color black (rgb: 0,0,0)
     $background = imagecolorallocate($dst, 255, 255, 0);
     // removing the black from the placeholder
     imagecolortransparent($dst, $background);

     // turning off alpha blending (to ensure alpha channel information
     // is preserved, rather than removed (blending with the rest of the
     // image in the form of black))
     imagealphablending($dst, false);

     // turning on alpha channel information saving (to ensure the full range
     // of transparency is preserved)
     imagesavealpha($dst, true);


              Imagepng($dst, $dstfile);

      } else {

           $src = ImageCreateFromGif($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imagegif($dst, $dstfile);
   }
}

The image source :

enter image description here

The current result (that I don't want) is :

enter image description here

How can I resize the image and maintain the transparency of the background color? Need help, please. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2024-12-14 05:30:06

imagecopyresampled 位于错误的位置。这应该在您设置背景颜色之后调用:

$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);

// use imagecolorallocatealpha to set BG as transparent:
$background = imagecolorallocatealpha($dst, 255, 255, 255, 127);

// turning off alpha blending as it's not needed
imagealphablending($dst, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);

// Do this here!
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

Imagepng($dst, $dstfile);

imagecopyresampled is in the wrong place. This should be called after you have set the background colour:

$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);

// use imagecolorallocatealpha to set BG as transparent:
$background = imagecolorallocatealpha($dst, 255, 255, 255, 127);

// turning off alpha blending as it's not needed
imagealphablending($dst, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);

// Do this here!
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

Imagepng($dst, $dstfile);
挽清梦 2024-12-14 05:30:06
<?php
function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {
    if(file_exists($newname))
        @unlink($newname);
    if(!file_exists($name))
        return false;
    $arr = split("\.",$name);
    $ext = $arr[count($arr)-1];

    if($ext=="jpeg" || $ext=="jpg"){
        $img = @imagecreatefromjpeg($name);
    } elseif($ext=="png"){
        $img = @imagecreatefrompng($name);
    } elseif($ext=="gif") {
        $img = @imagecreatefromgif($name);
    }
    if(!$img)
        return false;
    $old_x = imageSX($img);
    $old_y = imageSY($img);
    if($old_x < $new_w && $old_y < $new_h) {
        $thumb_w = $old_x;
        $thumb_h = $old_y;
    } elseif ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = floor(($old_y*($new_h/$old_x)));
    } elseif ($old_x < $old_y) {
        $thumb_w = floor($old_x*($new_w/$old_y));
        $thumb_h = $new_h;
    } elseif ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;
    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);

    if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }

    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
    if($border) {
        $black = imagecolorallocate($new_img, 0, 0, 0);
        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
    }
    if($base64) {
        ob_start();
        imagepng($new_img);
        $img = ob_get_contents();
        ob_end_clean();
        $return = base64_encode($img);
    } else {
        if($ext=="jpeg" || $ext=="jpg"){
            imagejpeg($new_img, $newname);
            $return = true;
        } elseif($ext=="png"){
            imagepng($new_img, $newname);
            $return = true;
        } elseif($ext=="gif") {
            imagegif($new_img, $newname);
            $return = true;
        }
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return $return;
}
//example useage
createthumb("1.png", "2.png", 64,64,true, true, false);
?>

希望这可以帮助...
我不应该因为这个代码而受到赞扬...只是在网络上发现它...享受

<?php
function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {
    if(file_exists($newname))
        @unlink($newname);
    if(!file_exists($name))
        return false;
    $arr = split("\.",$name);
    $ext = $arr[count($arr)-1];

    if($ext=="jpeg" || $ext=="jpg"){
        $img = @imagecreatefromjpeg($name);
    } elseif($ext=="png"){
        $img = @imagecreatefrompng($name);
    } elseif($ext=="gif") {
        $img = @imagecreatefromgif($name);
    }
    if(!$img)
        return false;
    $old_x = imageSX($img);
    $old_y = imageSY($img);
    if($old_x < $new_w && $old_y < $new_h) {
        $thumb_w = $old_x;
        $thumb_h = $old_y;
    } elseif ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = floor(($old_y*($new_h/$old_x)));
    } elseif ($old_x < $old_y) {
        $thumb_w = floor($old_x*($new_w/$old_y));
        $thumb_h = $new_h;
    } elseif ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;
    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);

    if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }

    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
    if($border) {
        $black = imagecolorallocate($new_img, 0, 0, 0);
        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
    }
    if($base64) {
        ob_start();
        imagepng($new_img);
        $img = ob_get_contents();
        ob_end_clean();
        $return = base64_encode($img);
    } else {
        if($ext=="jpeg" || $ext=="jpg"){
            imagejpeg($new_img, $newname);
            $return = true;
        } elseif($ext=="png"){
            imagepng($new_img, $newname);
            $return = true;
        } elseif($ext=="gif") {
            imagegif($new_img, $newname);
            $return = true;
        }
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return $return;
}
//example useage
createthumb("1.png", "2.png", 64,64,true, true, false);
?>

Hope this can help...
I dont deserve credit for this code... just found it op net... enjoy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文