2010-09-15 6 views
0

Je dois créer un filigrane l'appliquer sur une image et l'enregistrer avec un nom différent. Le script actuel fonctionne plutôt bien mais le seul problème est que j'ai besoin d'augmenter la taille du "texte d'exemple" et de changer l'arrière-plan du noir au blanc. J'ai essayé différents scénarios, j'ai changé l'opacité mais je ne peux toujours pas changer la couleur d'arrière-plan.php, gd, créer un filigrane, changer la taille du texte filigrane et la couleur de fond, imagecreatefromjpeg

function watermark($imag_path, $photo_id) { 
    // Load the stamp and the photo to apply the watermark to 
    $im = imagecreatefromjpeg("$imag_path"); 
    echo "imag_path is $imag_path and photoid is $photo_id"; 
    // First we create our stamp image manually from GD 
    $stamp = imagecreatetruecolor(490, 20); 

    //$im = imagecreatefromjpeg("$photo_id"); 
    imagestring($stamp, 5, 20, 2, 'sample text', 0xff0000); 

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

    // Merge the stamp onto our photo with an opacity (transparency) of 100% 
    imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 100); 
    $new_photo_id = $photo_id . "sample.JPG"; 
    // Save the image to file and free memory 
    imagejpeg($im, "tmp/$new_photo_id"); 
    imagedestroy($im); 
} 

Répondre

3

Pourquoi utiliser un tampon? J'utilise le code suivant sur un de mes sites:

$im = imagecreatefromjpeg($path); 

    function shadow_text($im, $size, $x, $y, $font, $text) 
    { 
    $black = imagecolorallocate($im, 0, 0, 0); 
    $white = imagecolorallocate($im, 255, 255, 255); 
    imagettftext($im, $size, 0, $x + 1, $y + 1, $black, $font, $text); 
    imagettftext($im, $size, 0, $x + 0, $y + 1, $black, $font, $text); 
    imagettftext($im, $size, 0, $x + 0, $y + 0, $white, $font, $text); 
    } 

    $font = '../fonts/verdana.ttf'; 
    $size = 11; 

    # calculate maximum height of a character 
    $bbox = imagettfbbox($size, 0, $font, 'ky'); 
    $x = 8; $y = 8 - $bbox[5]; 

    $text = 'text to be added'; 
    shadow_text($im, $size, $x, $y, $font, $text); 

    header("Content-Type: image/jpeg"); 
    imagejpeg($im, null, 90); 

Ce code fonctionne assez vite que nous l'utilisons pour ajouter des étiquettes dynamiques à la volée des photos de notre section photo comme ils sont téléchargés, plutôt que de les sauver sur le disque.

+0

Je dois enregistrer les images en filigrane sur le disque afin de pouvoir les télécharger plus tard sur un site Web. – Michael

+0

donc il suffit de supprimer la dernière ligne et de modifier la dernière ligne (remplacez 'null' par votre nom de fichier) – Alnitak

+0

merci beaucoup, vous avez fait ma journée! – Michael

Questions connexes