49

ユーザーが画像をアップロードできるウェブサイトを持っています...

画像がアップロードされたら、画像にロゴ (透かし) を追加する必要があります。

どうすればそうできますか?

また、透かしが見える隅にあることが重要です。たとえば、その場で透かしを生成し、メイン画像の背景が「同じ色」の場合はどこにでもマークを付ける Web サイトを見たことがあります。私が何を意味するか知っていれば、透かしが突き出ています。

これに関する良いチュートリアルや記事を持っている人はいますか? または、透かしの位置を見つけるために必要なphpの関数を知っていますか?

4

7 に答える 7

57

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);
于 2010-02-10T07:41:59.720 に答える
22

この関数を使用する
透かし画像のタイプは「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');
于 2014-11-14T20:08:38.900 に答える
12

透かし画像の良い例と中央に配置

<?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);
?>
于 2012-12-20T15:19:44.167 に答える
3

.htaccess を介して透かしを動的に追加するはるかに優れたソリューションを見つけました。ここでチュートリアルを見つけることができます。

htaccess で画像に透かしを追加する

カスタム .htaccess ファイル、watermark.php scrypt、および watermark.png 画像をアップロードすると、フォルダーとそのサブフォルダー内のすべての画像に透かしが表示されますが、元のファイルはサーバーに保持されます。

それが私を助けたのと同じように誰かを助けることを願っています。

于 2012-10-11T16:13:06.220 に答える
2

これは、 GDImageMagickなどの画像操作ライブラリを使用して実行できます。GDを使用してそれを行う方法を説明するチュートリアルは次のとおりです。

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

于 2010-02-10T07:44:30.880 に答える
2

ImageMagickはそのためにうまく機能します。以前やったことがあります。ただし、ビジネス全体は少し苦痛です。特に派手なブレンドモードなどが必要な場合。

于 2010-02-10T07:51:56.477 に答える
2
// 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);
于 2014-01-24T06:28:42.447 に答える