Una manera diferente para obtener un efecto similar es pegar el archivo PNG en una nueva imagen con un color de fondo único para eliminar temporalmente la transparencia, y establecer la transparencia color de la imagen png al color del círculo negro en su lugar. Luego, cuando lo coloca sobre la imagen jpeg, establece el nuevo color transparente en el color de la máscara.
// Load the Black Circle PNG image
$png = imagecreatefrompng('mask.png');
$width = imagesx($png);
$height = imagesy($png);
// Create a mask image
$mask = imagecreatetruecolor($width, $height);
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled($mask, $png, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($png);
// Set the black portion of the mask to transparent.
$black = imagecolorallocate($mask, 0, 0, 0);
imagecolortransparent($mask, $black);
// Load JPEG image.
$jpg = imagecreatefromjpeg('image.jpg');
$j_width = imagesx($jpg);
$j_height = imagesx($jpg);
// Enable alpha blending and copy the png image
imagealphablending($jpg, true);
imagecopyresampled($jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height);
imagedestroy($mask);
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate($jpg, 255, 0, 255);
imagecolortransparent($jpg, $magenta);
imagepng($jpg);
Si remuestreo o semi-transparentes píxeles que están consiguiendo abajo, en lugar de utilizar un png como una máscara, puede desactivar la mezcla y dibujar una forma transparente en la imagen $mask
lugar.
// Load JPEG Image.
$jpg = imagecreatefromjpeg('image.jpg');
$width = imagesx($jpg);
$height = imagesx($jpg);
// Create mask at same size with an opaque background.
$mask = imagecreatetruecolor($width, $height);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
// Disable alpha blending and draw a transparent shape onto the mask.
$transparent = imagecolorallocatealpha($mask, 255, 255, 255, 127);
imagealphablending($mask, false);
imagefilledellipse($mask, round($width/2), round($height/2), $width, $height, $transparent);
// Paste the mask onto the original image and set the new transparent colour.
imagealphablending($jpg, true);
imagecopyresampled($jpg, $mask, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($mask);
$magenta = imagecolorallocate($jpg, 255, 0, 255);
imagecolortransparent($jpg, $magenta);
// Output new image to browser as a png.
imagepng($jpg);
Nota: El código anterior no se ha probado, pero con suerte debe hacer lo que necesita.
Gracias, esto es realmente útil – Matt
Sé que esta publicación ha estado cerrada durante un tiempo, pero ¿cómo se veía el tiempo de ejecución cuando ustedes ejecutaron este script? Está tardando en promedio 20 segundos más o menos ... Mis imágenes de fuente/máscara son 250 x 170 píxeles ... ¿se trata de lo que ustedes están recibiendo? –
Sin tener en cuenta, no estoy seguro de lo que estaba haciendo mal, pero funciona muy bien ahora: P Gracias chicos! –