<?php
Class Protect_img {/** * Image File variables * @var string $file//img file * @var string $save//Saved Water marked file * @var string $im//img resource * @var string $tmp//temp img for encoding */private $file; Private $save; Private $im; Private $tmp; /** * secure_img, Bootstrap the class with IMG file * @param string $file */function __construct ($file) { if (Is_file ($file)) {$this->file= $file; $this->save= ' wm '. basename ($this->file). JPG '; New file to save watermarked image as $this->im= $this->open_img ($this->file); Open image $this->tmp=__dir__. ' /'. __class__. '. JPG '; TMP img File} else {$this->error_img ("works with a file"); }}/** * Clean up tmp file */function __destruct () {if (Is_file ($this->tmp)) {unlink ($this-&G T;TMP); }}/** * Private variables for watermark String * * @var array $watermark file or String * @var int $mark _alpha watermark transpanency * @var int $mark _FO NT Font type * @var int $mark _start_x left and right position * @var int $mark _start_y up and down position * @var int $mark _border watermark boarder * @var int $mark _height watermark Height * @var int $mark _width Watermark Width * @var string $color 1 boarder and Font color * @var string $color 2 background color * @var string $encoded encoded image String */private $watermark =array (); File or string private $mark _alpha=50; Private $mark _font=4; Private $mark _start_x=20; Private $mark _start_y=20; Private $mark _border=9; Private $mark _height=36; Private $mark _width; Private $color 1= ' 0x0000FF '; Private $color 2= ' 0xFFFFFF '; Private $encoded; /** * __get () Select private Values magicly * @param string $name * @return string, on error display image wit H Error */function __get ($name) {switch ($name) {case ' encoded ': return $this->encoded; break; Case ' save ': Return $this->save; Break Default: $this->error_img (' called an unknown variable '); }; }/** * __set () for setting private watermark Properties * @param type $name * @param type $value * @r eturn bool, return error image */function __set ($name, $value) {switch ($name) {case ' Add_watermark ': $this->watermark[]= $value; $new =round (((strlen ($value)) * (((strlen ($value)/2) +10)/2)); if ($new > $this->mark_width) {$this->mark_width= $new; } $this->mark_height= $this->mark_height+20; Break Case ' Watermark_alpha ': if (is_numeric ($value) && (0 <= $value | | $value <=)) {$this-& Gt;mark_alpha= $value; } else {$this->error_img ($value. "is not a number or betweEn 0 and, plese fix this "); } break; Case ' Watermark_font ': if (Is_numeric ($value)) {$this->mark_font= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' watermark_start_x ': if (Is_numeric ($value)) {$this->mark_start_x= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' watermark_start_y ': if (Is_numeric ($value)) {$this->mark_start_y= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' Watermark_border ': if (Is_numeric ($value)) {$this->mark_border= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' watermark_height ': if (Is_numeric ($value)) {$this->mark_height= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' watermark_width ': if (Is_numeric ($value)) {$this->mark_width= $value; } else {$this->error_img ($value. "Is not a number, plese fix this"); } break; Case ' watermark_colors ': if (Is_array ($value) && count ($value) = = 2) {$this->color1=array_shift ($v Alue); $this->color2=array_shift ($value); } else {$this->error_img ("works with a array with values!"); } break; Default: $this->error_img ("Can ' t set the following value". $name); Break } return TRUE; }/** * Apply water mark to image * @param type $x pass all to unset right alignment * @param type $y pass all to U Nset Bottom Alignment * @return Resource Returns an imagecopymerge resource */function Water_mark ($x = ' right ', $y = ' bo Ttom ') {if (is_aRray ($this->watermark) && count ($this->watermark) > 0) {$stamp = Imagecreatetruecolor ($this->mark _width, $this->mark_height); Imagefilledrectangle ($stamp, 0, 0, $this->mark_width-5, $this->mark_height-5, $this->color1); Imagefilledrectangle ($stamp, $this->mark_border, $this->mark_border, $this->mark_width-($this->mark_ border+5), $this->mark_height-($this->mark_border+5), $this->color2); $top = $this->mark_border+11; foreach ($this->watermark as $w) {imagestring ($stamp, $this->mark_font, $this->mark_border+11, $top, $w, $th Is->color1); $top = $top +18; }} else {$this->error_img (' Watermark is not set '); }/Subtract watermark width and start width from image width if ($x = = ' right ') {$sta Rt_x=imagesx ($this->im)-imagesx ($stamp)-$this->mark_start_x; } else {$start _x= $this->mark_start_x; }//If set to bottom and then subtract WATermark height and start height from image height if ($y = = ' Bottom ') {$start _y=imagesy ($this->im)-imagesy ($s Tamp)-$this->mark_start_y; } else {$start _y= $this->mark_start_y; } return Imagecopymerge ($this->im, $stamp, $start _x, $start _y, 0, 0, Imagesx ($stamp), Imagesy ($stamp), $this->mark _alpha); }/** * Send image to the browser as JPEG */function send_img () {Header ("content-type:image/jpeg"); Imagejpeg ($this->im); Imagedestroy ($this->im); Exit (); }/** * Save the water marked image to a new file on the current path */function save_img () {return $this->WR Ite_img ($this->save); }/** * Write new image to a file * @param type $file * @return Boolean */function write_img ($file) {imag Ejpeg ($this->im, $file); Imagedestroy ($this->im); return TRUE; }/** * Encode JPEG image as base64 with Data:prefix * @return Boolean */function encode_img () {imagejpeg ($t His->im, $this->TMP); Imagedestroy ($this->im); $t =file_get_contents ($this->tmp); $this->encoded= ' data:image/jpeg;base64, '. Base64_encode ($t); return TRUE; }/** * encode img with HTML tag * @return String */function encode_img_html ($alt = ' watermark Teste R ') {if (Isset ($this->encoded)) {return ' encoded. ' alt= '. $alt. '/> '; } else {$this->error_img ("must encode this image first"); }}/** * open_img * @param type $filename * @return Boolean */function open_img ($file) {$fn =explode ('. '), $file); $type =array_pop ($FN); Switch (Strtolower ($type)) {//Add more MIME types case ' jpg ': case ' jpeg ': Case ' JPE ': Return Imagecreatefromjpeg ($f ILE); Break Case "GIF": Return Imagecreatefromgif ($file); Break Case "PNG": Return Imagecreatefrompng ($file); Break Case "BMP": Case "wbmp": Return Imagecreatefromwbmp ($file); Break Default $this->error_img ("$type is invalid, with an image file [bmp, JPG, GIF or PNG]"); } return TRUE; }/** * Prints error in an image instead of text * @param string $txt */Private Function error_img ($txt) { $w = 600; $h = 60; $f = 12; $stamp = Imagecreatetruecolor ($w, $h); Imagefilledrectangle ($stamp, 0, 0, $w -5, $h -5, ' 0xcd0000 '); Imagefilledrectangle ($stamp, $this->mark_border, $this->mark_border, $w-($this->mark_border+5), $h-($ this->mark_border+5), ' 0xFFFAFA '); Imagestring ($stamp, $f, $this->mark_border+11, $this->mark_border+11, $txt, ' 0xcc1100 '); Header ("http/1.0 Internal Server Error"); Header ("Content-type:image/jpeg"); Imagejpeg ($stamp); Imagedestroy ($stamp); Exit (); }}
Usage:
<?php/* * To change this template, choose Tools | Templates * and open the template in the editor. */$file = ' oldboxes.png ';//$test = ' string '; $test = ' save ';//$test = ' encode ';//$test = ' invalid_file ';//$test = ' Encode_ HTML ';//$test = ' invalid_img '; require_once ' protectimg.class.php '; switch ($test) {case ' string ': $img =new protect_img ( $file); $img->add_watermark= "Name here"; $img->add_watermark= "<[email protected]>"; $img->add_watermark= "http://www.website.com"; Add watermark text First, width guessing might not work out as your want,//Just play around to get the proper width To meet your needs $img->watermark_width=300; $img->watermark_font=8; /** $img->watermark_alpha=46; $img->watermark_font=5; $img->watermark_start_x=20; $img->watermark_ start_y=180, $img->watermark_border=8; $img->watermark_height=70; $img->watermark_colors=array (' 0xABCDEF ', ' 0x012345 '); */$img->water_mark (); $img->send_img (); Break Case ' InvaLid_file ': $img =new protect_img (' $invalid '); Break Case ' invalid_img ': $invalid = '/var/log/apache2/error.log '; $img =new protect_img ($invalid); Break Case ' encode '://return encoded string, for storing in a db, after Water_mark header ("Content-type:text/plain"); $img =new protect_img ($file); $img->add_watermark= $mark; $img->add_watermark= "Name here"; $img->add_watermark= "<[email protected]>"; $img->add_watermark= "http://www.website.com"; Add watermark text First, width guessing might not work out as your want,//Just play around to get the proper width To meet your needs $img->watermark_width=300; $img->watermark_font=8; $img->encode_img (); Echo $img->encoded; Exit (); Break Case ' encode_html '://Encode image, after Water_mark header ("content-type:text/html"); $img =new protect_img ($file); $img->add_watermark= $mark; $img->add_watermark= "Name here"; $img->add_watermark= "<[emaiL protected]> "; $img->add_watermark= "http://www.website.com"; Add watermark text First, width guessing might not work out as your want,//Just play around to get the proper width To meet your needs $img->watermark_width=300; $img->watermark_font=8; $img->water_mark (); $img->encode_img (); echo $img->encode_img_html (); Break Case ' save ': $img =new protect_img ($file); $img->add_watermark= $mark; $img->add_watermark= "Name here"; $img->add_watermark= "<[email protected]>"; $img->add_watermark= "http://www.website.com"; Add watermark text First, width guessing might not work out as your want,//Just play around to get the proper width To meet your needs $img->watermark_width=300; $img->watermark_font=8; $img->water_mark (); $img->save_img (); $file = Str_replace (basename ($_server[' Script_name "), $img->save, $_server[' script_name ']); Header ("content-type:text/html"); Echo '<a href= "http://". $_server[' Http_host '). $file. ' > ' $file. ' </a> saved '; break;} ?>