- // Add watermark & proportional thumbnail & fixed height & fixed width in PHP.
- Class Image_process {
- Public $ source; // source image
- Public $ source_width; // source image width
- Public $ source_height; // source image height
- Public $ source_type_id;
- Public $ orign_name;
- Public $ orign_dirname;
-
- // Input the source image path
- Public function _ construct ($ source ){
- $ This-> typeList = array (1 => 'GIF', 2 => 'jpg ', 3 => 'PNG ');
- $ Ginfo = getimagesize ($ source );
- $ This-> source_width = $ ginfo [0];
- $ This-> source_height = $ ginfo [1];
- $ This-> source_type_id = $ ginfo [2];
- $ This-> orign_url = $ source;
- $ This-> orign_name = basename ($ source );
- $ This-> orign_dirname = dirname ($ source );
- }
-
- // Determine the format of the image file and return the PHP identifiable code
- Public function judgeType ($ type, $ source ){
- If ($ type = 1 ){
- Return imagecreatefromgif ($ source); // gif
- } Else if ($ type = 2 ){
- Return imagecreatefromjpeg ($ source); // jpg
- } Else if ($ type = 3 ){
- Return imagecreatefrompng ($ source); // png
- } Else {
- Return false;
- }
- }
-
- // Generate a watermark image
- Public function waterMakeImage ($ logo ){
- $ Linfo = getimagesize ($ logo );
- $ Logo_width = $ linfo [0];
- $ Logo_height = $ linfo [1];
- $ Logo_type_id = $ linfo [2];
- $ SourceHandle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url );
- $ LogoHandle = $ this-> judgeType ($ logo_type_id, $ logo );
- If (! $ SourceHandle |! $ LogoHandle ){
- Return false;
- }
- $ X = ($ this-> source_width-$ logo_width)/2;
- $ Y = ($ this-> source_height-$ logo_height)/2;
- Imagecopy ($ sourceHandle, $ logoHandle, $ x, $ y, 0, $ logo_width, $ logo_height );
- $ NewPic = $ this-> orign_dirname. '\ water _'. time (). '.'. $ this-> typeList [$ this-> source_type_id];
- If ($ this-> saveImage ($ sourceHandle, $ newPic )){
- Imagedestroy ($ sourceHandle );
- Imagedestroy ($ logoHandle );
- }
- }
-
- // Fixed height and width
- Public function fixSizeImage ($ width, $ height ){
- If ($ width> $ this-> source_width) $ this-> source_width;
- If ($ height> $ this-> source_height) $ this-> source_height;
- If ($ width = false ){
- $ Width = floor ($ this-> source_width/($ this-> source_height/$ height ));
- }
- If ($ height = false ){
- $ Height = floor ($ this-> source_height/($ this-> source_width/$ width ));
- }
- $ This-> tinyImage ($ width, $ height );
- }
-
- // Proportional scaling
- Public function scaleImage ($ scale ){
- $ Width = floor ($ this-> source_width * $ scale );
- $ Height = floor ($ this-> source_height * $ scale );
- $ This-> tinyImage ($ width, $ height );
- }
-
- // Create a thumbnail
- Public function tinyImage ($ width, $ height ){
- $ TinyImage = imagecreatetruecolor ($ width, $ height );
- $ Handle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url );
- If (function_exists ('imagecopyresampled ')){
- Imagecopyresampled ($ tinyImage, $ handle, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
- } Else {
- Imagecopyresized ($ tinyImage, $ handle, 0, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
- }
- $ NewPic = $ this-> orign_dirname. '\ thumb _'. time (). '_'. $ width. "_". $ height. ". ". $ this-> typeList [$ this-> source_type_id];
- If ($ this-> saveImage ($ tinyImage, $ newPic )){
- Imagedestroy ($ tinyImage );
- Imagedestroy ($ handle );
- }
- }
- // Save the image
- Private function saveImage ($ image, $ url ){
- If (imagejpeg ($ image, $ url )){
- Return true;
- }
- }
- }
- $ ImgHandle = new Image_process ('d: \ AppServ \ www \ test \ getimg \ 14063797445601.jpg ');
- // $ ImgHandle-> waterMakeImage ('d: \ AppServ \ www \ test \ getimg \ shougongke.png '); // Generate a watermark image
- // $ ImgHandle-> fixSizeImage (200,150); // fixed-length image
- $ ImgHandle-> scaleImage (0.2); // proportional scaling
- ?>
|