PHP 对 png 图像进行缩放,支持透明背景

1 篇文章 0 订阅
1 篇文章 0 订阅
function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
    {
        if ( $height <= 0 && $width <= 0 ) {
            return false;
        }
        $info = getimagesize($file);
        $image = '';

        $final_width = 0;
        $final_height = 0;
        list($width_old, $height_old) = $info;

        if ($proportional) {
            if ($width == 0) $factor = $height/$height_old;
            elseif ($height == 0) $factor = $width/$width_old;
            else $factor = min ( $width / $width_old, $height / $height_old);  
            $final_width = round ($width_old * $factor);
            $final_height = round ($height_old * $factor);

        }
        else {       
            $final_width = ( $width <= 0 ) ? $width_old : $width;
            $final_height = ( $height <= 0 ) ? $height_old : $height;
        }

        switch ($info[2] ) {
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif($file);
            break;
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg($file);
            break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng($file);
            break;
            default:
                return false;
        }

        $image_resized = imagecreatetruecolor( $final_width, $final_height );

        if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {

            $trnprt_indx = imagecolortransparent($image);

            // If we have a specific transparent color
            if ($trnprt_indx >= 0) {

                // Get the original image's transparent color's RGB values
                $trnprt_color = imagecolorsforindex($image, $trnprt_indx);

                // Allocate the same color in the new image resource
                $trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

                // Completely fill the background of the new image with allocated color.
                imagefill($image_resized, 0, 0, $trnprt_indx);

                // Set the background color for new image to transparent
                imagecolortransparent($image_resized, $trnprt_indx);
            }
            // Always make a transparent background color for PNGs that don't have one allocated already
            elseif ($info[2] == IMAGETYPE_PNG) {

                // Turn off transparency blending (temporarily)
                imagealphablending($image_resized, false);

                // Create a new transparent color for image
                $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);

                // Completely fill the background of the new image with allocated color.
                imagefill($image_resized, 0, 0, $color);

                // Restore transparency blending
                imagesavealpha($image_resized, true);
            }
        }

        imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

        if ( $delete_original ) {
            if ( $use_linux_commands )
                exec('rm '.$file);
            else
                @unlink($file);
        }

        switch ( strtolower($output) ) {
            case 'browser':
                $mime = image_type_to_mime_type($info[2]);
                header("Content-type: $mime");
                $output = NULL;
            break;
            case 'file':
                $output = $file;
            break;
            case 'return':
                return $image_resized;
            break;
            default:
            break;
        }

        switch ($info[2] ) {
            case IMAGETYPE_GIF:
                imagegif($image_resized, $output);
            break;
            case IMAGETYPE_JPEG:
                imagejpeg($image_resized, $output);
            break;
            case IMAGETYPE_PNG:
                imagepng($image_resized, $output);
            break;
            default:
                return false;
        }

        return true;
    }

源文出自

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Medpy是一个基于Python的医学图像处理库,它提供了一些常见的医学图像处理和分割算法,其中就包括了图割(Graph Cut)算法。下面简单介绍一下如何使用Medpy的Graph Cut算法对彩色PNG图像进行分割。 1. 准备工作 首先需要安装Medpy库,并且准备一张彩色PNG图像。可以使用PIL或者OpenCV等库读取PNG图像。 ```python from PIL import Image # 读取彩色PNG图像 img = Image.open('example.png') ``` 2. 图像准备 Medpy的Graph Cut算法接受的输入图像是灰度图像,所以需要先将彩色图像转换为灰度图像。转换之后,可以将灰度值归一化为0到1之间的浮点数,以方便Graph Cut算法使用。 ```python import numpy as np # 将彩色图像转换为灰度图像 gray_img = img.convert(mode='L') # 归一化灰度值 gray_array = np.array(gray_img) gray_array = gray_array / np.max(gray_array) ``` 3. 分割 准备好输入图像之后,就可以调用Medpy的Graph Cut算法进行分割了。Graph Cut算法需要指定前景和背景的种子点,可以手动选择或者使用自动种子选择算法。这里以手动选择种子点的方式进行演示。 ```python from medpy.graphcut import graphcut # 分割种子点的定义 seed1 = (10, 10) seed2 = (200, 200) # 进行Graph Cut分割 roi = graphcut(gray_array, [seed1, seed2], algorithm='expansion') ``` 分割结果roi是一个与输入图像大小相同的布尔数组,表示每个像素是否属于前景。可以使用PIL库的Image.fromarray函数将其转换为图像。 ```python # 将分割结果转换为图像 result = Image.fromarray(roi.astype(np.uint8)*255) # 保存结果图像 result.save('result.png') ``` 至此,就完成了使用Medpy的Graph Cut算法对彩色PNG图像进行分割的操作。需要注意的一点是,如果输入图像非常大,Graph Cut算法可能运行时间较长,可以通过缩小图像尺寸、降低灰度图像精度等方式来加速运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值