使用imagick PHP在png图像周围添加边框 - php

如何在png图像周围添加边框?每当我尝试使用borderImage中提供的imagick函数添加边框时,如果它是png图像,它将失去其透明度。

<?php

$image = new Imagick();
$image->readImage('tux.png');

$image->BorderImage(new ImagickPixel("red") , 5,5);

// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image;

这是原始图像:

这是在添加边框之后:

边框颜色也应用于背景。我想使用imagick做到这一点
如何在透明图像上应用边框而不损失透明性?

参考方案

如果要获得这样的结果:

那么就是这个。如果需要,您甚至可以在边框和图像之间进行填充!

/** Set source image location. You can use URL here **/
$imageLocation = 'tux.png';

/** Set border format **/
$borderWidth = 10;

// You can use color name, hex code, rgb() or rgba()
$borderColor = 'rgba(255, 0, 0, 1)';

// Padding between image and border. Set to 0 to give none
$borderPadding = 0;


/** Core program **/

// Create Imagick object for source image
$imageSource = new Imagick( $imageLocation );

// Get image width and height, and automatically set it wider than
// source image dimension to give space for border (and padding if set)
$imageWidth = $imageSource->getImageWidth() + ( 2 * ( $borderWidth + $borderPadding ) );
$imageHeight = $imageSource->getImageHeight() + ( 2 * ( $borderWidth + $borderPadding ) );

// Create Imagick object for final image with border
$image = new Imagick();

// Set image canvas
$image->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 );

// Draw border
$border->rectangle(
    $borderWidth / 2 - 1,
    $borderWidth / 2 - 1,
    $imageWidth - ( ($borderWidth / 2) ),
    $imageHeight - ( ($borderWidth / 2) )
);

// Apply drawed border to final image
$image->drawImage( $border );

$image->setImageFormat('png');

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

// Prepare image and publish!
header("Content-type: image/png");
echo $image;

我从here获得此方法。基本上,我们只是使用ImagickDraw::rectangle制作具有透明填充和带边框的矩形,然后使用Imagick::compositeImage将图像放入矩形内。

如果将$borderPadding设置为10,则结果如下:

而已!希望能帮助到你 :)

Laravel Image Intervention避免旋转 - php

我正在上载由iPhone相机垂直拍摄的iPhone图像,其尺寸为2448x3264,并且当我创建600x360的拇指时,该尺寸非常高(?),它会自动旋转为水平。我尝试了什么但没有成功:更改拇指尺寸使用fit功能使用resize功能使用crop功能使用upsize和aspectRatio方法仅设置height并在width上使用null仅设置width并在he…

寻找PHP图片库…圆角和调整大小 - php

on-topic 我只是想寻找一个好的PHP图像库,所以想要显示带有圆角的图像,进行一些调整大小,并在运行中或上载时模糊其他图片。 参考方案 我建议看看ImageMagick。PHP中的库也有出色的包装器:http://www.imagemagick.org/script/api.php#php

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友

获取图片扩展名 - php

我要上传图片扩展名。据我所知,最好的方法是getimagesize()函数。但是此函数的mime在图像具有image/jpeg或.jpg扩展名时返回.JPEG。如何获得确切的延期? 参考方案 您可以对image_type_to_extension返回的图像类型使用getimagesize函数:$info = getimagesize($path); $ext…