Innym sposobem, aby uzyskać podobny efekt jest wklejenie pliku PNG na nowego obrazu z unikalnym kolorem tła, aby tymczasowo usunąć przejrzystości i ustawić przezroczyste zamiast tego kolor obrazu png zamiast czarnego koła. Następnie, po umieszczeniu go nad obrazem jpeg, ustawiasz nowy przezroczysty kolor na kolor maski.
// 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);
Jeśli resampling lub półprzezroczyste piksele stają się w dół, zamiast używać PNG jako maska, możesz wyłączyć mieszanie i narysuj kształt na $mask
przejrzystego obrazu zamiast.
// 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);
Uwaga: Powyższy kod jest nieprzetestowany, ale powinien mieć nadzieję, że zrobi to, czego potrzebujesz.
Dziękuję, to jest naprawdę pomocne – Matt
Wiem, że ten post był przez jakiś czas zamknięty, ale jak wyglądało środowisko uruchomieniowe, kiedy prowadziliście ten skrypt? Zajmuje to około 20 sekund ... Moje obrazy źródłowe/maski to 250 x 170 pikseli ... czy to, co dostajecie? –
Zignorować, nie jestem pewien, co robiłem źle, ale teraz działa świetnie: P Dzięki chłopaki! –