Pada kesempatan kali ini kami kursus web programming grogol akan menjelaskan cara membuat watermark pada gambar di php. Perlu teman-teman ketahui sebelumnya, watermark adalah copyright atau hak cipta yang terdapat di setiap gambar website yang teman-teman buat. Tujuan dari watermark tersebut dimana supaya gambar yang teman-teman miliki di website tidak copy paste atau tidak di salah gunakan oleh orang lain, karena dari watermark tersebut menandakan hak cipta pembuat gambar tersebut atau pemilik gambar tersebut. Dan jika teman-teman menggunakan CMS seperti wordpress, tentunya plugin untuk pembuatan watermark tersebut sangat banyak tersedia di internet, mulai dari yang gratis sampai yang berbayar.
Namun yang akan kami kursus web programming grogol jelaskan beserta dengan contohnya adalah membuat watermark khusus di php native.
Berikut Tahapannya :
- Teman-teman buat terlebih dahulu satu buah folder project dengan nama watermark di dalam folder webserver local teman-teman seperti xampp/htdocs
- Jika sudah buat terlebih dahulu satu buah file dengan imageswatermark.php, dan ketikan kode seperti berikut :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
<?php $copyrightSymbol = 169; // Nilai untuk symbol ASCII $padding = 4; // untuk padding $basePath = getcwd() . '/'; // biarkan default $fontPath = 'fontswatermark/'; // Disesuaikan dengan Lokasi tempat penyimpanan file font.ttf teman-teman $fontArial = $fontPath . 'arial.ttf'; // membuat jenis font $fontTahoma = $fontPath . 'Tahoma.ttf'; // membuat jenis font $wm_font = $fontTahoma; // Tentukan default jenis fontnya yang sudah teman-teman buat dan ini PENTING $message = chr($copyrightSymbol) . " Brian Abraham"; // Sesuaikan dengan text yang mau teman-teman tampilkan di watermark tersebut $wm_image = $basePath . ''; // Untuk watermark dari gambar dan biarkan kosongkan karena tidak saya gunakan // Defaults $wm_type = 'msg'; // Default watermarking method atau type nya $fontColor = 'white'; // Default warna untuk text watermarknya dan bisa teman-teman sesuaikan $fontSize = 22; // bisa teman-teman sesuaikan untuk ukuran font size watermarknya $wm_bg = 0; // Fill background for message text watermarks flag (true/false) $position = 'bottom'; // Bisa teman-teman sesuaikan untuk posisi watermarknya $wm_factor = (1/3); $wm_opacity = 70; // Default watermark untuk opacity dan biarkan default $wm_rotate = 0; // Default watermark overlay rotation $dest_x = 4; // Default watermark x/horizontal posisi // Handle Query String option parameters // type, color, size, pos, wmsize, op, rot, x, m if (isset($_GET['type'])) { $wm_type = $_GET['type']; } if (isset($_GET['color'])) { $fontColor = $_GET['color']; } if (isset($_GET['size'])) { $fontSize = $_GET['size']; } if (isset($_GET['bg'])) { $wm_bg = $_GET['bg']; } if (isset($_GET['pos'])) { $position = $_GET['pos']; } if (isset($_GET['wmsize'])) { $wm_factor = 1/($_GET['wmsize']); } if (isset($_GET['op'])) { $wm_opacity = $_GET['op']; } if (isset($_GET['rot'])) { $wm_rotate = $_GET['rot']; } if (isset($_GET['x'])) { $dest_x = $_GET['x']; } if (isset($_GET['m'])) { $message = $_GET['m']; } // Get info about image to be watermarked $old_image = $_GET['src']; $size = getimagesize($old_image); $imagetype = $size[2]; // Create GD Image Resource from original image switch($imagetype) { case(1): $image = imagecreatefromgif($old_image); break; case(2): $image = imagecreatefromjpeg($old_image); break; case(3): $image = imagecreatefrompng($old_image); break; case(6): $image = imagecreatefrombmp($old_image); break; } // end switch($imagetype) if ($wm_type == 'img') { $watermark = imagecreatefromjpeg($wm_image); // Create a GD image resource from overlay image $wm_width = imagesx($watermark); // get the original width $wm_height = imagesy($watermark); // get the original height $watermark_rWidth = $wm_width * $wm_factor; // calculate new, reduced width $watermark_rHeight = $wm_height * $wm_factor; // calculate new, reduced height $watermark_r = @imagecreatetruecolor($watermark_rWidth, $watermark_rHeight); // Reduce the size of the overlay image GD resource // imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) imagecopyresampled($watermark_r, $watermark, 0, 0, 0, 0, $watermark_rWidth, $watermark_rHeight, $wm_width, $wm_height); imagedestroy($watermark); // original watermark image no longer needed } // endif $wm_image if ($wm_type == 'msg') { // Calculate size of overlay image for text // array imagettfbbox ( float $size , float $angle , string $fontfile , string $text ) $wm_bBox = imagettfbbox($fontSize, 0, $wm_font, $message); $lowerLeftX = $wm_bBox[0]; $lowerLeftY = $wm_bBox[1]; $lowerRightX = $wm_bBox[2]; $lowerRightY = $wm_bBox[3]; $upperRightX = $wm_bBox[4]; $upperRightY = $wm_bBox[5]; $upperLeftX = $wm_bBox[6]; $upperLeftY = $wm_bBox[7]; $watermark_rWidth = ($upperRightX - $upperLeftX) + $padding; $watermark_rHeight = ($lowerLeftY - $upperLeftY) + $padding; // Create the overlay image $watermark_r = imagecreatetruecolor($watermark_rWidth, $watermark_rHeight); $txtColorBk = hex2int(validHexColor('000000','ffffff')); // black $txtColorR = hex2int(validHexColor('800000','ffffff')); // red $txtColorG = hex2int(validHexColor('008000','ffffff')); // green $txtColorBl = hex2int(validHexColor('000080','ffffff')); // blue $txtColorYl = hex2int(validHexColor('ffff00','ffffff')); // yellow $txtColorWht = hex2int(validHexColor('ffffff','ffffff')); // white switch($fontColor) { case('black'): $txtColor = imageColorAllocate($watermark_r, $txtColorBk['r'], $txtColorBk['g'], $txtColorBk['b']); break; case('red'): $txtColor = imageColorAllocate($watermark_r, $txtColorR['r'], $txtColorR['g'], $txtColorR['b']); break; case('green'): $txtColor = imageColorAllocate($watermark_r, $txtColorG['r'], $txtColorG['g'], $txtColorG['b']); break; case('blue'): $txtColor = imageColorAllocate($watermark_r, $txtColorBl['r'], $txtColorBl['g'], $txtColorBl['b']); break; case('yellow'): $txtColor = imageColorAllocate($watermark_r, $txtColorYl['r'], $txtColorYl['g'], $txtColorYl['b']); break; case('white'): $txtColor = imageColorAllocate($watermark_r, $txtColorWht['r'], $txtColorWht['g'], $txtColorWht['b']); break; } // end switch($fontColor) if (!isset($txtColor)) { $userColor = hex2int(validHexColor($fontColor, 'ffffff')); $txtColor = imageColorAllocate($watermark_r, $userColor['r'], $userColor['g'], $userColor['b']); } // endif (!isset($txtColor) // Set an appropriate background color if (($fontColor == 'white') || ($fontColor == 'ffffff')) { $watermark_rBGColor = imageColorAllocate($watermark_r, $txtColorBk['r'], $txtColorBk['g'], $txtColorBk['b']); } else { $watermark_rBGColor = imageColorAllocate($watermark_r, $txtColorWht['r'], $txtColorWht['g'], $txtColorWht['b']); } imagefilledrectangle($watermark_r, 0, 0, $watermark_rWidth, $watermark_rHeight, $watermark_rBGColor); // Make the background transparent? if (!$wm_bg) { $bg = imagecolortransparent($watermark_r, $watermark_rBGColor); } // array imageTTFText ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text) imageFTText($watermark_r, $fontSize, 0, 1, $watermark_rHeight-$padding, $txtColor, $wm_font, $message); } // endif $wm_type == 'msg' // Handle rotation if ($wm_rotate != 0) { if (phpversion() >= 5.1) { $bg = imagecolortransparent($watermark_r); $watermark_r = imagerotate($watermark_r, $wm_rotate, $bg, 1); } else { $watermark_r = imagerotate($watermark_r, $wm_rotate, 0); } // endif phpversion() $watermark_rWidth = imagesx($watermark_r); $watermark_rHeight = imagesy($watermark_r); } // endif $wm_rotate !=0 // Calculate overlay image position if ($position == 'bottom') { $dest_y = $size[1] - ($watermark_rHeight + $padding); } else { $dest_y = 4; } // endif $position // Overlay the logo watermark image on the original image // int imagecopymerge ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct ); imagecopymerge($image, $watermark_r, $dest_x, $dest_y, 0, 0, $watermark_rWidth, $watermark_rHeight, $wm_opacity); imagedestroy($watermark_r); switch($imagetype) { case(1): header('Content-type: image/gif'); imagegif($image); break; case(2): header('Content-type: image/jpeg'); imagejpeg($image); break; case(3): header('Content-type: image/png'); imagepng($image); break; case(6): header('Content-type: image/bmp'); imagewbmp($image); break; } // end switch($imagetype) imagedestroy($image); exit; /** * @param $hex string 6-digit hexadecimal color * @return array 3 elements 'r', 'g', & 'b' = int color values * @desc Converts a 6 digit hexadecimal number into an array of * 3 integer values ('r' => red value, 'g' => green, 'b' => blue) */ function hex2int($hex) { return array( 'r' => hexdec(substr($hex, 0, 2)), // 1st pair of digits 'g' => hexdec(substr($hex, 2, 2)), // 2nd pair 'b' => hexdec(substr($hex, 4, 2)) // 3rd pair ); } /** * @param $input string 6-digit hexadecimal string to be validated * @param $default string default color to be returned if $input isn't valid * @return string the validated 6-digit hexadecimal color * @desc returns $input if it is a valid hexadecimal color, * otherwise returns $default (which defaults to black) */ function validHexColor($input = '000000', $default = '000000') { // A valid Hexadecimal color is exactly 6 characters long // and eigher a digit or letter from a to f return (preg_match('/^[0-9a-f]{6}$/', $input)) ? $input : $default ; } // function imageRotate courtesy beau@dragonflydevelopment.com on www.php.net function rotateImage($img, $rotation) { $width = imagesx($img); $height = imagesy($img); switch($rotation) { case 90: $newimg= @imagecreatetruecolor($height , $width );break; case 180: $newimg= @imagecreatetruecolor($width , $height );break; case 270: $newimg= @imagecreatetruecolor($height , $width );break; case 0: return $img;break; case 360: return $img;break; } if($newimg) { for($i = 0;$i < $width ; $i++) { for($j = 0;$j < $height ; $j++) { $reference = imagecolorat($img,$i,$j); switch($rotation) { case 90: if(!@imagesetpixel($newimg, ($height - 1) - $j, $i, $reference )){return false;}break; case 180: if(!@imagesetpixel($newimg, $width - $i, ($height - 1) - $j, $reference )){return false;}break; case 270: if(!@imagesetpixel($newimg, $j, $width - $i, $reference )){return false;}break; } } } return $newimg; } return false; } // end rotateImage() ?> |
- Jika sudah save terlebih dahulu file tersebut.
Untuk pembahasan selanjutnya, akan kami kursus web programming grogol jelaskan cara membuat watermark pada gambar di php pada sesi berikutnya, semoga bermanfaat.