2012-08-31 132 views
1

我想添加一個水印到圖像,然後將圖像保存到另一個文件與PHP。這是迄今爲止的代碼,但由於某種原因,水印未出現在新目錄中的圖像上。 原始圖像保存在路徑$old_path中,我想在應用水印後將其保存到$new_pathphp水印和保存圖像

$old_path = "images_upload/".$name.".".$type; 
    $new_path = "images_main/".$name.".".$type; 
    ////////////////////water mark 
$main_image = imagecreatefromstring(file_get_contents($old_path)); 

// Load the logo image 
$logoImage = imagecreatefrompng("assets/watermark.png"); 
imagealphablending($logoImage, true); 

// Get dimensions 
$imageWidth=imagesx($main_image); 
$imageHeight=imagesy($main_image); 

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

// Paste the logo 
imagecopy(
    // source 
    $main_image, 
    // destination 
    $logoImage, 
    // destination x and y 
    $imageWidth-$logoWidth, $imageHeight-$logoHeight, 
    // source x and y 
    0, 0, 
    // width and height of the area of the source to copy 
    $logoWidth, $logoHeight); 
    //////////////////////// 

    rename($old_path, $new_path);// save image 

請告訴我我做錯了什麼。

回答

2

你從來沒有imagecopy結果寫下任何文件,只需將舊圖像重命名爲新路徑 - 用imagejpeg代替:

imagejpeg($logoImage, $new_path); 
+0

我不想將圖像回顯到瀏覽器。我想在應用水印後將它從$ old_path複製到$ new_path。 (所以$ new_path中的新圖像上會有水印) – user1481850

+1

@ user1481850你看過我提供的鏈接嗎? – moonwave99

+0

我測試過這個函數不接受新文件路徑的參數。現在應該做什麼來生成一個新的圖像文件? – gadget00

1

您不在任何地方輸出圖像。你需要輸出它。你現在正在做的是將你的舊文件重命名爲新的。

嘗試imagepng()或您的格式等效。 http://php.net/manual/en/function.imagepng.php

+0

我使用改名功能,在複製的影像。 – user1481850

+1

@ user1481850,我不認爲你明白。當你使用'imagecopy'時,操作只發生在內存中。除非您選擇這樣做,否則它們不會保存到文件中。閱讀我指出的文檔。 – Brad