代码之家  ›  专栏  ›  技术社区  ›  saperlipopette

使用PHP裁剪png图像,删除空白透明度

  •  6
  • saperlipopette  · 技术社区  · 7 年前

    现在我想修改PNG图片的大小。下面是我想要调整大小的PNG示例: enter image description here

    虚线代表PNG的边界,背景是透明的,我只在一个大空间的中间丢失了一颗星。我想裁剪这颗星,得到一个简单的恒星正方形(即使新背景变为空白,也没关系)。

    <?php
    
    $file = "./crop.png";
    
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    
    $image;
    
    switch ($ext){
    case 'png':
        $image = imagecreatefrompng($file);
        break;
    
    case 'jpeg':
    case 'jpg':
        $image = imagecreatefromjpeg($file);
        break;
    
    case 'gif':
        $image = imagecreatefromgif($file);
        break;
    }
    
    $cropped = imagecropauto($image, IMG_CROP_DEFAULT);
    
        if ($cropped !== false) { // in case a new image resource was returned
            echo "=> Cropping needed\n";
            imagedestroy($image);    // we destroy the original image
            $image = $cropped;       // and assign the cropped image to $im
        }
    
        imagepng($image, "./cropped.png");
        imagedestroy($image);
    
    2 回复  |  直到 7 年前
        1
  •  7
  •   Christos Lytras    7 年前

    如果你阅读并遵循 imagecropauto 这正是你想要的,它修剪图像的alpha通道。

    $im = imagecreatefrompng("./star-with-alpha.png");
    $cropped = imagecropauto($im, IMG_CROP_DEFAULT);
    
    if ($cropped !== false) { // in case a new image resource was returned
        imagedestroy($im);    // we destroy the original image
        $im = $cropped;       // and assign the cropped image to $im
    }
    
    imagepng($im, "./star-with-alpha-crop.png");
    imagedestroy($im);
    

    <body>
    
    <img src="star-with-alpha.png">
    
    <?php 
    
    $im = imagecreatefrompng("./star-with-alpha.png");
    $cropped = imagecropauto($im, IMG_CROP_DEFAULT);
    
    if ($cropped !== false) { // in case a new image resource was returned
        imagedestroy($im);    // we destroy the original image
        $im = $cropped;       // and assign the cropped image to $im
    }
    
    imagepng($im, "./star-with-alpha-crop.png");
    imagedestroy($im);
    
    ?>
    
    <img src="star-with-alpha-crop.png">
    
    </body>
    

    http://zikro.gr/dbg/php/crop-png/

    Cropped image demo

        2
  •  1
  •   informer    4 年前

    [这是ubuntu 12的] 唯一的问题是 imagecropauto 由于现在大多数服务器都使用ubuntu/debain,这个功能没有用。 下面是我编写的示例代码,它正是这样做的:

        //Add background transmparent
        $background = 'none';
        $image = new Imagick($path);
        $image->trimImage(0);
       
        //add transparent border
        //border add start
        /** Set border format **/
        $borderWidth = 20;
        $borderColor = 'none';
        $borderPadding = 10;
    
    
        $imageWidth = $image->getImageWidth() + ( 2 * ( $borderWidth + 
        $borderPadding ) );
        $imageHeight = $image->getImageHeight() + ( 2 * ( $borderWidth + 
        $borderPadding ) );
    

        $imageWithBorder = new Imagick();
        // Set image canvas
        $imageWithBorder->newImage( $imageWidth, $imageHeight, new ImagickPixel( 
        'none' ));
        // Create ImagickDraw object to draw border
        $border = new ImagickDraw();
        // Set fill color to transparent
        $border->setFillColor( 'none' );
        // Set border format
        $border->setStrokeColor( new ImagickPixel( $borderColor ) );
        $border->setStrokeWidth( $borderWidth );
        $border->setStrokeAntialias( false );
    

    绘制边框

        $border->rectangle(
            $borderWidth / 2 - 1,
            $borderWidth / 2 - 1,
            $imageWidth - ( ($borderWidth / 2) ),
            $imageHeight - ( ($borderWidth / 2) )
        );
        // Apply drawed border to final image
        $imageWithBorder->drawImage( $border );
        $imageWithBorder->setImageFormat('png');
    

    保存图像

        // Put source image to final image
        $imageWithBorder->compositeImage(
                $image, Imagick::COMPOSITE_DEFAULT,
                $borderWidth + $borderPadding,
                $borderWidth + $borderPadding
        );
        
        $imageWithBorder->writeImage($path);
    

        $imageWithBorder->scaleImage(FINAL_WIDTH, FINAL_HEIGHT, true);
        $imageWithBorder->setImageBackgroundColor($background);
        $w = $imageWithBorder->getImageWidth();
        $h = $imageWithBorder->getImageHeight();
        $imageWithBorder->extentImage(FINAL_WIDTH, FINAL_HEIGHT, ($w  - 
        FINAL_WIDTH) / 2, ($h - FINAL_HEIGHT)/ 2);
        $imageWithBorder->writeImage($path);
    

    希望有帮助。 干杯