2014-06-27 11 views
12

Come posso aggiungere un bordo attorno a un'immagine png? Ogni volta che provo ad aggiungere un bordo usando la funzione borderImage disponibile in imagick, perde la trasparenza se si tratta di un'immagine png.Aggiungi bordo intorno all'immagine png usando imagick PHP

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

Questa è l'immagine originale:

enter image description here

e questo è dopo l'aggiunta di un bordo:

enter image description here

Colore bordo viene applicato anche sullo sfondo. Voglio farlo usando imagick Come posso applicare un bordo a un'immagine trasparente senza perdere la trasparenza?

+0

Vuoi dire _solo_ applicare la linea di confine all'esterno dell'immagine in modo che non fornisca lo sfondo? –

risposta

15

Se si vuole raggiungere risultato come questo:

Result Image

allora è questo. Puoi anche dare il padding tra il bordo e l'immagine, se vuoi!

/** 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; 

ho avuto questo metodo da here. Fondamentalmente creiamo un rettangolo con riempimento trasparente e bordo formattato usando ImagickDraw::rectangle, quindi inseriamo l'immagine all'interno del rettangolo usando Imagick::compositeImage.

Ecco il risultato se si imposta $borderPadding-10:

Alternative Result Image

Questo è tutto! Spero che lo aiuti :)