您好,登錄后才能下訂單哦!
PHP中怎么實現生成圖片水印,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
文本水印
我們使用函數watermark_text()來生成文本水印,你必須先指定字體源文件、字體大小和字體文本,具體代碼如下:
$font_path = "GILSANUB.TTF"; // Font file $font_size = 30; // in pixcels $water_mark_text_2 = "phpfuns"; // Watermark Text function watermark_text($oldimage_name, $new_image_name) { global $font_path, $font_size, $water_mark_text_2; list($owidth,$oheight) = getimagesize($oldimage_name); $width = $height = 300; $image = imagecreatetruecolor($width, $height); $image_src = imagecreatefromjpeg($oldimage_name); imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight); $blue = imagecolorallocate($image, 79, 166, 185); imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2); imagejpeg($image, $new_image_name, 100); imagedestroy($image); unlink($oldimage_name); return true; }
可以在這里查看demo。
圖片水印
我們使用函數watermark_image()來生成圖片水印,你必須先水銀圖片的源文件。具體代碼如下:
$image_path = "phpfuns.png"; function watermark_image($oldimage_name, $new_image_name) /{ global $image_path; list($owidth,$oheight) = getimagesize($oldimage_name); $width = $height = 300; $im = imagecreatetruecolor($width, $height); $img_src = imagecreatefromjpeg($oldimage_name); imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight); $watermark = imagecreatefrompng($image_path); list($w_width, $w_height) = getimagesize($image_path); $pos_x = $width - $w_width; $pos_y = $height - $w_height; imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height); imagejpeg($im, $new_image_name, 100); imagedestroy($im); unlink($oldimage_name); return true; }
你可以在這里查看demo。
上傳圖片表單
我們使用下面的表單來上傳圖片:
<?php $demo_image= ""; if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit") { $path = "uploads/"; $valid_formats = array("jpg", "bmp","jpeg"); $name = $_FILES['imgfile']['name']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024) { $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']); if($upload_status){ $new_name = $path.time().".jpg"; // Here you have to user functins watermark_text or watermark_image if(watermark_text($path.$_FILES['imgfile']['name'], $new_name)) $demo_image = $new_name; } } else $msg="File size Max 256 KB or Invalid file format."; } } ?> // HTML Code <form name="imageUpload" method="post" enctype="multipart/form-data" > Upload Image Image :<input type="file" name="imgfile" /><br /> <input type="submit" name="createmark" value="Submit" /> <?php if(!emptyempty($demo_image)) echo '<img src="'.$demo_image.'" />'; ?> </form>
關于PHP中怎么實現生成圖片水印問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。