web-dev-qa-db-fra.com

Ajouter un filigrane aux images avec php

J'ai un site Web où les utilisateurs peuvent télécharger des images ...

Je dois ajouter mon logo (filigrane) aux images une fois qu'elles sont téléchargées.

Comment faire?

Et il est important que le filigrane se trouve dans un coin où il sera visible, par exemple, j'ai vu des sites Web qui génèrent un filigrane à la volée et mettent la marque partout où l'arrière-plan de l'image principale est "de la même couleur" de sorte que le filigrane dépasse si vous savez ce que je veux dire.

Quelqu'un a un bon tutoriel ou un article à ce sujet? Ou connaissez-vous une fonction en php dont j'aurais besoin pour trouver la position du filigrane?

40
pesar

A bon exemple dans le manuel PHP:

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
49
XUE Can

utiliser cette fonction
le type d'image de filigrane doit être "png"

 function watermark_image($target, $wtrmrk_file, $newcopy) {
    $watermark = imagecreatefrompng($wtrmrk_file);
    imagealphablending($watermark, false);
    imagesavealpha($watermark, true);
    $img = imagecreatefromjpeg($target);
    $img_w = imagesx($img);
    $img_h = imagesy($img);
    $wtrmrk_w = imagesx($watermark);
    $wtrmrk_h = imagesy($watermark);
    $dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
    $dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
    imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
    imagejpeg($img, $newcopy, 100);
    imagedestroy($img);
    imagedestroy($watermark);
}

watermark_image('image_name.jpg','watermark.png', 'new_image_name.jpg');
14
iraqi_love4ever

Bon exemple d'image en filigrane et positionné au centre

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
12
Sailee Shahir

J'ai trouvé une bien meilleure solution qui ajoute un filigrane dinamicalement via .htaccess, vous pouvez trouver le tutoriel ici:

Ajouter un filigrane aux images via htaccess

Après avoir téléchargé le fichier .htaccess personnalisé, le chiffrement watermark.php et votre image watermark.png, toutes les images du dossier et de ses sous-dossiers afficheront le filigrane, mais vous conserverez toujours le fichier d'origine sur le serveur.

J'espère que cela aide quelqu'un de la même manière que cela m'a aidé.

3
Alberto

Cela peut être fait en utilisant une bibliothèque de manipulation d'images telle que Gd ou ImageMagick . Voici un tutoriel qui explique comment le faire avec Gd:

http://articles.sitepoint.com/article/watermark-images-php

2
Jimmy Cuadra

ImageMagick fonctionne bien pour cela. Je l'ai déjà fait. Cependant, toute l'entreprise est un peu pénible. Surtout si vous voulez des modes de mélange sophistiqués et similaires.

2
Gabriel Hurley
// Load the stamp and the photo to apply the watermark to

$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpg');
$save_watermark_photo_address = 'watermark_photo.jpg';

// Set the margins for the stamp and get the height/width of the stamp image

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
// header('Content-type: image/png');

imagejpeg($im, $save_watermark_photo_address, 80); 
imagedestroy($im);
2
Baz Love