PHP Conference Japan 2024

imagecopy

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecopy複製影像的一部分

說明

imagecopy(
    GdImage $dst_image,
    GdImage $src_image,
    int $dst_x,
    int $dst_y,
    int $src_x,
    int $src_y,
    int $src_width,
    int $src_height
): bool

src_image 的一部分複製到 dst_image,起始位置為 x, y 座標 src_xsrc_y,寬度為 src_width,高度為 src_height。定義的部分將複製到 x, y 座標 dst_xdst_y

參數

dst_image

目標影像資源。

src_image

來源影像資源。

dst_x

目標點的 x 座標。

dst_y

目標點的 y 座標。

src_x

來源點的 x 座標。

src_y

來源點的 y 座標。

src_width

來源寬度。

src_height

來源高度。

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.0.0 dst_imagesrc_image 現在需要 GdImage 實例;先前需要的是 資源

範例

範例 #1 裁剪 PHP.net 標誌

<?php
// 建立影像實例
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);

// 複製
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

// 輸出並從記憶體中釋放
header('Content-Type: image/gif');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);
?>

以上範例將輸出類似以下的內容

Output of example : Cropping the PHP.net logo

另請參閱

新增註釋

使用者提供的註釋 16 則註釋

5
john at mccarthy dot net
17 年前
這是那個酷炫波浪函數的升級版:將影像大小加倍,使其波動,然後再重新取樣縮小。這可以產生更漂亮、抗鋸齒的波浪。

// 如此簡單又好用!
function wave_region($img, $x, $y, $width, $height,$amplitude = 4.5,$period = 30)
{
// 建立影像大小兩倍的副本
$mult = 2;
$img2 = imagecreatetruecolor($width * $mult, $height * $mult);
imagecopyresampled ($img2,$img,0,0,$x,$y,$width * $mult,$height * $mult,$width, $height);

// 讓它波動
for ($i = 0;$i < ($width * $mult);$i += 2)
{
imagecopy($img2,$img2,
$x + $i - 2,$y + sin($i / $period) * $amplitude, // 目標位置 (dest)
$x + $i,$y, // 來源位置 (src)
2,($height * $mult));
}

// 再次縮小取樣
imagecopyresampled ($img,$img2,$x,$y,0,0,$width, $height,$width * $mult,$height * $mult);
imagedestroy($img2);
}

用於完整影像:
wave_region ($oImage,0,0,imagesx($oImage),imagesy($oImage));
4
Jeff
17 年前
我遇到一個問題,頁面上可以上傳任何圖片,然後我需要將其作為具有透明度的真彩色圖片處理。問題出在具有透明度的調色板圖片(例如 GIF 圖片)上,當我使用 imagecopy 將圖片轉換為真彩色時,透明部分會變成黑色(無論實際代表透明的是什麼顏色)。

要將圖片轉換為具有透明度的真彩色圖片,以下程式碼可以運作(假設 $img 是您的圖片資源)

<?php
//Convert $img to truecolor
$w = imagesx($img);
$h = imagesy($img);
if (!
imageistruecolor($img)) {
$original_transparency = imagecolortransparent($img);
//we have a transparent color
if ($original_transparency >= 0) {
//get the actual transparent color
$rgb = imagecolorsforindex($img, $original_transparency);
$original_transparency = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue'];
//change the transparent color to black, since transparent goes to black anyways (no way to remove transparency in GIF)
imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0));
}
//create truecolor image and transfer
$truecolor = imagecreatetruecolor($w, $h);
imagealphablending($img, false);
imagesavealpha($img, true);
imagecopy($truecolor, $img, 0, 0, 0, 0, $w, $h);
imagedestroy($img);
$img = $truecolor;
//remake transparency (if there was transparency)
if ($original_transparency >= 0) {
imagealphablending($img, false);
imagesavealpha($img, true);
for (
$x = 0; $x < $w; $x++)
for (
$y = 0; $y < $h; $y++)
if (
imagecolorat($img, $x, $y) == $original_transparency)
imagesetpixel($img, $x, $y, 127 << 24);
}
}
?>

現在 $img 是一個真彩色圖片資源
5
petr dot biza at gmail dot com
14 年前
有一個函式可以裁剪圖片的空白邊緣。

<?php
/**
* $image image cursor (from imagecreatetruecolor)
* $backgound image curosr (from imagecolorallocate)
* $paddng int
*/
function imageCrop($image, $background = false, $padding = 0) {
if(
$background)
$background = imagecolorallocate($image, 255, 255, 255);

$top = imageSY($image);
$left = imageSX($image);
$bottom = 0;
$right = 0;

for (
$x = 0 ; $x < imagesx($image) ; $x++) {
for (
$y = 0 ; $y < imagesy($image) ; $y++) {

// if there match
if(imagecolorat($image, $x, $y) != $background) {

if(
$x < $left)
$left = $x;
if(
$x > $right)
$right = $x;
if(
$y > $bottom)
$bottom = $y;
if(
$y < $top)
$top = $y;
}
}
}

$right++;
$bottom++;

// create new image with padding
$img = imagecreatetruecolor($right-$left+$padding*2,$bottom-$top+$padding*2);
// fill the background
imagefill($img, 0, 0, $background);
// copy
imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top);

// destroy old image cursor
imagedestroy($image);
return
$img;
}
?>
3
latin4567 at gmail dot com
15 年前
我建立了一個執行標準 9 切片縮放技術的 PHP 函式。這對於縮圖陰影縮放以及任何涉及面板設計都非常有用。歡迎參考及使用

注意:我的 9 切片程式碼沒有指定邊距,而是使用居中矩形的概念... 輸入提供圖片(作為資源)、矩形的 x 和 y 座標,以及矩形的寬度和高度。

$src_im 參數應為圖片資源。此程式碼是用於 9 切片半透明 PNG 圖片,並且僅使用半透明 PNG 圖片進行過測試,但它應該適用於其他圖片類型(可能需要一些修改)

因此,如果您的來源圖片為 400 x 400,所有邊都需要 24 像素的邊距,而目標大小為 800 x 500,則應使用以下參數

<?php
$im
= NineSlice($im, 24, 24, 352, 352, 800, 500)
?>

<?php
/* 9-Slice Image Slicing Script by Sam Kelly (DuroSoft: http://www.durosoft.com) */
function NineSlice($src_im, $rect_x, $rect_y, $rect_w, $rect_h, $target_w, $target_h)
{
$src_w = imagesx($src_im);
$src_h = imagesy($src_im);

$im = CreateBlankPNG($target_w, $target_h);
imagealphablending($im,true);

$left_w = $rect_x;
$right_w = $src_w - ($rect_x + $rect_w);

$left_src_y = ceil($rect_h / 2) - 1 + $rect_y;
$right_src_y = $left_src_y;

$left_src_x = 0;
$right_src_x = $left_w + $rect_w;

$top_src_x = ceil($rect_w / 2) - 1 + $rect_x;
$bottom_src_x = $top_src_x;
$bottom_src_y = $rect_y + $rect_h;
$bottom_h = $src_h - $bottom_src_y;

$left_tile = CreateBlankPNG($left_w, 1);
imagecopy($left_tile, $src_im, 0, 0, 0, $left_src_y, $left_w, 1);

$right_tile = CreateBlankPNG($right_w, 1);
imagecopy($right_tile, $src_im, 0, 0, $right_src_x, $right_src_y, $right_w, 1);

$top_tile = CreateBlankPNG(1, $rect_y);
imagecopy($top_tile, $src_im, 0, 0, $top_src_x, 0, 1, $rect_y);

$bottom_tile = CreateBlankPNG(1, $bottom_h);
imagecopy($bottom_tile, $src_im, 0, 0, $bottom_src_x, $bottom_src_y, 1, $bottom_h);

$inner_tile = CreateBlankPNG(4, 4);
imagecopy($inner_tile, $src_im, 0, 0, ceil($src_w / 2) - 1, ceil($src_h / 2) - 1, 4, 4);

imagecopy($im, $src_im, 0, 0, 0, 0, $left_w, $rect_y);
imagecopy($im, $src_im, 0, $target_h - $bottom_h, 0, $bottom_src_y, $rect_x, $bottom_h);
imagecopy($im, $src_im, $target_w - $right_w, 0, $right_src_x, 0, $right_w, $rect_y);
imagecopy($im, $src_im, $target_w - $right_w, $target_h - $bottom_h, $src_w - $right_w, $bottom_src_y, $right_w, $bottom_h);

imagesettile($im, $top_tile);
imagefilledrectangle($im, $left_w, 0, $target_w - $right_w - 1, $rect_y, IMG_COLOR_TILED);

imagesettile($im, $left_tile);
imagefilledrectangle($im, 0, $rect_y, $left_w, $target_h - $bottom_h - 1, IMG_COLOR_TILED);


$right_side = CreateBlankPNG($right_w, $target_h - $rect_y - $bottom_h);
imagesettile($right_side, $right_tile);
imagefilledrectangle($right_side, 0, 0, $right_w, $target_h - $rect_y - $bottom_h, IMG_COLOR_TILED);
imagecopy($im, $right_side, $target_w - $right_w, $rect_y, 0, 0, $right_w, $target_h - $rect_y - $bottom_h);

$bottom_side = CreateBlankPNG($target_w - $right_w - $left_w, $bottom_h);
imagesettile($bottom_side, $bottom_tile);
imagefilledrectangle($bottom_side, 0, 0, $target_w - $right_w - $left_w, $bottom_h, IMG_COLOR_TILED);
imagecopy($im, $bottom_side, $right_w, $target_h - $bottom_h, 0, 0, $target_w - $right_w - $left_w, $bottom_h);

imagedestroy($left_tile);
imagedestroy($right_tile);
imagedestroy($top_tile);
imagedestroy($bottom_tile);
imagedestroy($inner_tile);
imagedestroy($right_side);
imagedestroy($bottom_side);

return
$im;
}

function
CreateBlankPNG($w, $h)
{
$im = imagecreatetruecolor($w, $h);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
return
$im;
}
?>
2
ragnar_40k at hotmail dot com
19 年前
這是一個用於在圖片上製作孔洞的函式

// 設定圖片一部分的 Alpha 通道(目前忽略畫布的 Alpha)。
// $img_canvas - 32 位元真彩色圖片,具有 Alpha 通道
// $img_mask - 8 位元灰階圖片(白色部分將在畫布中遮罩為透明)。
// 這取決於目前的像素格式
// (高位元組) -> (Alpha 通道) {紅色} {綠色} {藍色} <- (低位元組)
function mask($img_canvas, $img_mask, $dst_x, $dst_y)
{
$old_blendmode = imagealphablending($img_canvas, FALSE);

$width = imagesx($img_mask);
$heigth = imagesy($img_mask);

$mask_x = 0;
$x = $dst_y;
while ($mask_x<$width)
{
$mask_y = 0;
$y = $dst_y;
while ($mask_y<$heigth)
{
imagesetpixel($img_canvas, $x, $y,
(((imagecolorat($img_mask, $mask_x, $mask_y) >> 1) << 24) | (imagecolorat($img_canvas, $x, $y) & 0x00FFFFFF)); // 將遮罩像素的 alpha 值左移 24 位,並與畫布像素的 RGB 值合併

++$mask_y; // 遮罩 y 座標遞增
++$y; // 畫布 y 座標遞增
}
++$mask_x; // 遮罩 x 座標遞增
++$x; // 畫布 x 座標遞增
}

imagealphablending($img_canvas, $old_blendmode); // 恢復畫布的 alpha 混合模式
}
2
johnny at netvor dot sk // johnny at netvor dot sk 的留言
19 年前
simple image combination srcipt, ie. if u want to create one huuuge signature from more small ones // 簡單的圖片組合腳本,例如,如果您想從多個小簽名創建一個巨大的簽名

<?php
// config --
$src = array ("http://www.google.com/images/logo_sm.gif", "http://sk2.php.net/images/php.gif");
$under = 0; // combine images underneath or not?
// -- end of config

$imgBuf = array ();
$maxW=0; $maxH=0;
foreach (
$src as $link)
{
switch(
substr ($link,strrpos ($link,".")+1))
{
case
'png':
$iTmp = imagecreatefrompng($link);
break;
case
'gif':
$iTmp = imagecreatefromgif($link);
break;
case
'jpeg':
case
'jpg':
$iTmp = imagecreatefromjpeg($link);
break;
}

if (
$under)
{
$maxW=(imagesx($iTmp)>$maxW)?imagesx($iTmp):$maxW;
$maxH+=imagesy($iTmp);
}
else
{
$maxW+=imagesx($iTmp);
$maxH=(imagesy($iTmp)>$maxH)?imagesy($iTmp):$maxH;
}

array_push ($imgBuf,$iTmp);
}

$iOut = imagecreate ($maxW,$maxH) ;

$pos=0;
foreach (
$imgBuf as $img)
{
if (
$under)
imagecopy ($iOut,$img,0,$pos,0,0,imagesx($img),imagesy($img));
else
imagecopy ($iOut,$img,$pos,0,0,0,imagesx($img),imagesy($img));
$pos+= $under ? imagesy($img) : imagesx($img);
imagedestroy ($img);
}

imagegif($iOut);
?>
4
webmaster at plumage dot nl // webmaster at plumage dot nl 的留言
15 年前
I have a few remarks om the mirror-function // 我對鏡像函數有一些註解
The cases horizontal and vertical are switched. // 水平和垂直的情況被切換了。
1 = vertical and 2 = horizontal. // 1 = 垂直,2 = 水平。
When I used it there appeared a black lining of 1 pixel on the side or on the top of the picture. // 當我使用它時,圖片的側面或頂部出現了 1 像素的黑色線條。
To remove it the function becomes as follows // 為了移除它,函數變成如下所示

<?php
function ImageFlip ( $imgsrc, $mode )
{

$width = imagesx ( $imgsrc );
$height = imagesy ( $imgsrc );

$src_x = 0;
$src_y = 0;
$src_width = $width;
$src_height = $height;

switch (
$mode )
{

case
'1': //垂直翻轉
$src_y = $height -1;
$src_height = -$height;
break;

case
'2': //水平翻轉
$src_x = $width -1;
$src_width = -$width;
break;

case
'3': //垂直水平翻轉
$src_x = $width -1;
$src_y = $height -1;
$src_width = -$width;
$src_height = -$height;
break;

default:
return
$imgsrc;

}

$imgdest = imagecreatetruecolor ( $width, $height );

if (
imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
{
return
$imgdest;
}

return
$imgsrc;

}
?> // PHP程式碼:圖片翻轉函數
3
xafford
16 年前
關於 Borszczuk 先前的文章以及鏡像圖片的函式

使用 imagecopyresampled 有更好(且更快)的方法來完成這項任務。

<?php

define
( 'IMAGE_FLIP_HORIZONTAL', 1 );
define ( 'IMAGE_FLIP_VERTICAL', 2 );
define ( 'IMAGE_FLIP_BOTH', 3 );

function
ImageFlip ( $imgsrc, $mode )
{

$width = imagesx ( $imgsrc );
$height = imagesy ( $imgsrc );

$src_x = 0;
$src_y = 0;
$src_width = $width;
$src_height = $height;

switch ( (int)
$mode )
{

case
IMAGE_FLIP_HORIZONTAL:
$src_y = $height;
$src_height = -$height;
break;

case
IMAGE_FLIP_VERTICAL:
$src_x = $width;
$src_width = -$width;
break;

case
IMAGE_FLIP_BOTH:
$src_x = $width;
$src_y = $height;
$src_width = -$width;
$src_height = -$height;
break;

default:
return
$imgsrc;

}

$imgdest = imagecreatetruecolor ( $width, $height );

if (
imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height ) )
{
return
$imgdest;
}

return
$imgsrc;

}

?>
2
Chad Allard
15 年前
關於這裡討論的 image_flip 函式,別忘了支援透明度。



$imgdest = imagecreatetruecolor($width, $height);

之後

imagealphablending($imgdest, false);
imagesavealpha($imgdest, true);

在另一篇文章中提到您不需要使用 imagesavealpha 函式,但我發現如果沒有它,背景透明度會使背景畫布變成黑色。
1
John Conde
15 年前
簡單基本的圖片裁切

<?php
// 原始圖片
$filename = 'someimage.jpg';

// 取得原始圖片的尺寸
list($current_width, $current_height) = getimagesize($filename);

// 原始圖片上開始裁切圖片的 x 和 y 座標
$left = 50;
$top = 50;

// 這將是圖片的最終大小(例如,我們將向左和向下裁切多少像素)
$crop_width = 200;
$crop_height = 200;

// 重新取樣圖片
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename, 100);
?>
0
etienne at escott dot info
15 年前
這是基於 designerkamal at gmail dot com 的 Skew 函式。

這是一個用於在 PHP 中傾斜圖像並具有反鋸齒功能的函式。它適用於 Alpha PNG 圖像。

警告:您傾斜的圖像越大,處理所需的時間就越長。它比沒有反鋸齒功能的時間長約 3 倍。

<?php
// $img: handle of an image
// $skew_val: level of skew to apply (0 being none, 1 being 45°)
function imageskewantialiased($img, $skew_val)
{
$width = imagesx($img);
$height = imagesy($img);
$height2 = $height + ($width * $skew_val);

// See below for definition of imagecreatealpha
$imgdest = imagecreatealpha($width, $height2);

// Process the image
for($x = 0, $level = 0; $x < $width - 1; $x++)
{
$floor = floor($level);

// To go faster, some lines are being copied at once
if ($level == $floor)
imagecopy($imgdest, $img, $x, $level, $x, 0, 1, $height - 1);
else
{
$temp = $level - $floor;

// The first pixel of the line
// We get the color then apply a fade on it depending on the level
$color1 = imagecolorsforindex($img, imagecolorat($img, $x, 0));
$alpha = $color1['alpha'] + ($temp * 127);
if (
$alpha < 127)
{
$color = imagecolorallocatealpha($imgdest, $color1['red'], $color1['green'], $color1['blue'], $alpha);
imagesetpixel($imgdest, $x, $floor, $color);
}

// The rest of the line
for($y = 1; $y < $height - 1; $y++)
{
// Merge this pixel and the upper one
$color2 = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$alpha = ($color1['alpha'] * $temp) + ($color2['alpha'] * (1 - $temp));
if (
$alpha < 127)
{
$red = ($color1['red'] * $temp) + ($color2['red'] * (1 - $temp));
$green = ($color1['green'] * $temp) + ($color2['green'] * (1 - $temp));
$blue = ($color1['blue'] * $temp) + ($color2['blue'] * (1 - $temp));
$color = imagecolorallocatealpha($imgdest, $red, $green, $blue, $alpha);
imagesetpixel($imgdest, $x, $floor + $y, $color);
}

$color1 = $color2;
}

// The last pixel of the line
$color1 = imagecolorsforindex($img, imagecolorat($img, $x, $height - 1));
$alpha = $color1['alpha'] + ((1 - $temp) * 127);
if (
$alpha < 127)
{
$color = imagecolorallocatealpha($imgdest, $color1['red'], $color1['green'], $color1['blue'], $alpha);
imagesetpixel($imgdest, $x, $floor + $height - 1, $color);
}
}

// The line is finished, the next line will be lower
$level += $skew_val;
}

// Finished processing, return the skewed image
return $imgdest;
}

// Creates a new image of the size specified with a blank background (transparent)
function imagecreatealpha($width, $height)
{
// Create a normal image and apply required settings
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);

// Apply the transparent background
$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
for (
$x = 0; $x < $width; $x++)
{
for (
$y = 0; $y < $height; $y++)
{
imagesetpixel($img, $x, $y, $trans);
}
}

return
$img;
}

// Here's an example of how to use it
imagepng(imageskewantialiased(imagecreatefrompng('test.png'), 0.15), 'skew.png');
?>
0
matrebatre
16 年前
我遇到了與 Jeff 相同的問題,但他的解決方案對我無效。我製作了這個腳本,它似乎有效。

<?php

$img
= imagecreatefromgif('in.gif');
$w = imagesx($img);
$h = imagesy($img);

$trans = imagecolortransparent($img);
if(
$trans >= 0) {

$rgb = imagecolorsforindex($img, $trans);

$oldimg = $img;
$img = imagecreatetruecolor($w,$h);
$color = imagecolorallocate($img,$rgb['red'],$rgb['green'],$rgb['blue']);
imagefilledrectangle($img,0,0,$w,$h,$color);
imagecopy($img,$oldimg,0,0,0,0,$w,$h);

}

imagegif($img,'out.gif');

?>
0
C. Jansen
18 年前
在回覆一個支援論壇的貼文時,我注意到 imagecopy() 的一個奇怪之處。第一段程式碼(應該)會建立一個影像物件,在該影像中配置一個顏色資源,用配置的顏色填滿背景,然後將另一個裁剪過的影像複製到它上面。

<?php
// 建立新的影像資源
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );

//將背景填滿白色
imagefill( $temp, 0, 0, $white );

//將影像複製到新的資源中
imagecopy($temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height);
?>

但這樣會產生黑色的背景。我注意到移除 imagefill() 呼叫會產生相同的結果。解決方案是在 imagecopy() 之後呼叫 imagefill()。以線性思考來說,我原本猜想這樣會用白色覆蓋先前複製的影像,但它並沒有。我猜 GD 使用了圖層系統?這樣對嗎?

<?php
// 建立新的影像資源
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );

// 將影像複製到新的資源中
imagecopy( $temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height );

//將背景填滿白色(不知道為什麼必須按照這個順序)
imagefill( $temp, 0, 0, $white );
?>

我使用的是內建 GD (2.0.28) 的 php 5.1.4
0
designerkamal at gmail dot com
18 年前
在 PHP 中扭曲影像...
<?php
function Skew($src, $dest, $skew_val)
{
$imgsrc = imagecreatefromgif($src);
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$imgdest = imagecreatetruecolor($width, $height+($height*$skew_val));
$trans = imagecolorallocate($imgdest,0,0,0);
$temp=0;
for(
$x=0 ; $x<$width ; $x++)
{
for(
$y=0 ; $y<$height ; $y++)
{
imagecopy($imgdest, $imgsrc, $x, $y+$temp, $x, $y, 1, 1);
imagecolortransparent($imgdest,$trans);

}
$temp+=$skew_val;
}
imagepng($imgdest, $dest);
imagedestroy($imgsrc);
imagedestroy($imgdest);
}
Skew("img.gif", "img2.png","1");
print
"<img src='img.gif'>";
print
"<br><br>";
print
"<img src='img2.png'>";
?>
-2
Cory Gagliardi
16 年前
以下是一些簡單的程式碼,用於調整上傳圖片的大小,並在圖片的右下角插入浮水印(來自 24 位元 PNG)。在這個例子中,浮水印是一個對角線帶,上面寫著「已售出」。驗證上傳圖片類型是否正確的程式碼已被省略。

<?PHP
//載入並調整圖片大小
$uploaded = imagecreatefromjpeg($_FILES['file']['tmp_name']);
$image = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
imagecopyresampled($image, $uploaded, 0, 0, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, imagesx($uploaded), imagesy($uploaded));
imagealphablending($image,true); //允許我們在 $image 上應用 24 位元浮水印

//載入「已售出」浮水印
$sold_band = imagecreatefrompng('../images/sold_band.png');
imagealphablending($sold_band,true);

//應用浮水印並儲存
$image = image_overlap($image, $sold_band);
imagecopy($image,$sold_band,IMAGE_WIDTH - SOLD_WIDTH,IMAGE_HEIGHT - SOLD_HEIGHT,0,0,SOLD_WIDTH,SOLD_HEIGHT);
$success = imagejpeg($image,'../images/sold/'.$id.'.jpg',85);

imagedestroy($image);
imagedestroy($uploaded);
imagedestroy($sold_band);
?>
-2
admin at cvxdes dot com
18 年前
我用這段程式碼來為圖片加上浮水印。這是我寫的函式。

<?php
function watermark($url,$logo){
$bwidth = imagesx($url);
$bheight = imagesy($url);
$lwidth = imagesx($logo);
$lheight = imagesy($logo);
$src_x = $bwidth - ($lwidth + 5);
$src_y = $bheight - ($lheight + 5);
ImageAlphaBlending($url, true);
ImageCopy($url,$logo,$src_x,$src_y,0,0,$lwidth,$lheight);
}
?>

使用方法
<?php
//$current_image 將會是你想要加上浮水印的圖片。 需確認它是使用 imagecreatefrom*** 建立的圖片資源才能正常運作。
watermark($current_image,$watermark_image);
?>

希望這對某些人有幫助。
To Top