Mit GD in PHP, wie kann ich ein transparentes PNG-Wasserzeichen im PNG-und GIF-Dateien ? (JPG-Dateien funktionieren)

Habe ich ein Bild (nennen wir es original Bild) auf, das will ich Wasserzeichen ein anderes Bild (nennen wir es logo).

Die logo ist ein transparentes PNG in der Erwägung, dass die original Bild werden können png -, jpg-oder gif-Format.

Ich habe den folgenden code:

function watermarkImage($originalFileContents, $originalWidth, $originalHeight) {
    $logoImage = imagecreatefrompng('logo.png');
    imagealphablending($logoImage, true);

    $logoWidth  = imagesx($logoImage);  
    $logoHeight = imagesy($logoImage);

    $originalImage = imagecreatefromstring($originalFileContents);

    $destX = $originalWidth  - $logoWidth;
    $destY = $originalHeight - $logoHeight;

    imagecopy(
        //source
        $originalImage,
        //destination
        $logoImage,
        //destination x and y
        $destX, $destY,
        //source x and y
        0, 0,
        //width and height of the area of the source to copy
        $logoWidth, $logoHeight
    );
    imagepng($originalImage);
}

Dieser code funktioniert gut (gut = halten die Transparenz der logo) nur, wenn der original Bild ist eine JPG-Datei.

Wenn die original-Datei ist ein GIF-oder PNG, das logo hat einen festen weißen hintergrund, also die Transparenz funktioniert nicht.

Warum ? Was muss ich ändern damit es funktioniert ?

Dank

UPDATE:

Hier ist meine version recodiert:

function generate_watermarked_image($originalFileContents, $originalWidth, $originalHeight, $paddingFromBottomRight = 0) {
    $watermarkFileLocation = 'watermark.png';
    $watermarkImage = imagecreatefrompng($watermarkFileLocation);
    $watermarkWidth = imagesx($watermarkImage);  
    $watermarkHeight = imagesy($watermarkImage);

    $originalImage = imagecreatefromstring($originalFileContents);

    $destX = $originalWidth - $watermarkWidth - $paddingFromBottomRight;  
    $destY = $originalHeight - $watermarkHeight - $paddingFromBottomRight;

    //creating a cut resource
    $cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);

    //copying that section of the background to the cut
    imagecopy($cut, $originalImage, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);

    //placing the watermark now
    imagecopy($cut, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);

    //merging both of the images
    imagecopymerge($originalImage, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);
}
InformationsquelleAutor Doron | 2010-12-14
Schreibe einen Kommentar