Php uploads images and generates bitmap code

Source: Internet
Author: User

Php uploads images and generates bitmap code. We often need to upload images, but we also need to retain our copyright so we will use image watermarks, the following program adds your own watermark effect to the image after the image is uploaded successfully.

<? Php

Class Image {
Var $ imageResource = NULL;
Var $ target = NULL;
Var $ enableTypes = array ();
Var $ imageInfo = array ();
Var $ createFunc = '';
Var $ imageType = NULL;
 
/**
* Construct for this class
  *
* @ Param string $ image
* @ Return Image
*/
Function Image ($ image = NULL ){
// Get enables
If (imagetypes () & IMG_GIF ){
$ This-> enableTypes [] = 'image/GIF ';
  }
If (imagetypes () & IMG_JPEG ){
$ This-> enableTypes [] = 'image/jpeg ';
  }
If (imagetypes () & IMG_JPG ){
$ This-> enableTypes [] = 'image/jpg ';
  }
If (imagetypes () & IMG_PNG ){
$ This-> enableTypes [] = 'image/png ';
  }
// End get
  
If ($ image! = NULL ){
$ This-> setImage ($ image );
  }
 }
 
/**
* Set a image resource
  *
* @ Param string $ image
* @ Return boolean
*/
Function setImage ($ image ){
If (file_exists ($ image) & is_file ($ image )){
$ This-> imageInfo = getimagesize ($ image );
$ Img_mime = strtolower ($ this-> imageInfo ['Mime ']);
If (! In_array ($ img_mime, $ this-> enableTypes )){
Exit ('system cannot operate this image type .');
   }
Switch ($ img_mime ){
Case 'image/GIF ':
$ Link = imagecreatefromgif ($ image );
$ This-> createFunc = 'imagegif ';
$ This-> imageType = 'GIF ';
Break;
Case 'image/jpeg ':
Case 'image/jpg ':
$ Link = imagecreatefromjpeg ($ image );
$ This-> createFunc = 'imagejpeg ';
$ This-> imageType = 'jpeg ';
Break;
Case 'image/png ':
$ Link = imagecreatefrompng ($ image );
$ This-> createFunc = 'imagepng ';
$ This-> imageType = 'PNG ';
Break;
Default:
$ Link = 'unknow ';
$ This-> imageType = 'unknow ';
Break;
   }
If ($ link! = 'Unknow '){
$ This-> imageResource = $ link;
} Else {
Exit ('size cannot be changed for this image type .');
   }
Unset ($ link );
Return true;
} Else {
Return false;
  }
 }
 
/**
* Set header
  *
*/
Function setHeader (){
Switch ($ this-> imageType ){
Case 'GIF ':
Header ('content-type: image/gif ');
Break;
Case 'jpeg ':
Header ('content-type: image/jpeg ');
Break;
Case 'PNG ':
Header ('content-type: image/png ');
Break;
Default:
Exit ('Can not set header .');
Break;
  }
Return true;
 }
 
/**
* Change the image size
  *
* @ Param int $ width
* @ Param int $ height
* @ Return boolean
*/
Function changeSize ($ width, $ height =-1 ){
If (! Is_resource ($ this-> imageResource )){
Exit ('the image size cannot be changed, maybe you have not set the image source .');
  }
$ S_width = $ this-> imageInfo [0];
$ S_height = $ this-> imageInfo [1];
$ Width = intval ($ width );
$ Height = intval ($ height );
  
If ($ width <= 0) exit ('image width must be greater than zero .');
If ($ height <= 0 ){
$ Height = ($ s_height/$ s_width) * $ width;
  }
  
$ This-> target = imagecreatetruecolor ($ width, $ height );
If (@ imagecopyresized ($ this-> target, $ this-> imageResource, 0, 0, 0, 0, $ width, $ height, $ s_width, $ s_height ))
Return true;
Else
Return false;
 }
 
/**
* Add watermark
  *
* @ Param string $ image
* @ Param int $ app
*/
Function addWatermark ($ image, $ app = 50 ){
If (file_exists ($ image) & is_file ($ image )){
$ S_info = getimagesize ($ image );
} Else {
Exit ($ image. 'the file does not exist .');
  }

$ R_width = $ s_info [0];
$ R_height = $ s_info [1];

If ($ r_width> $ this-> imageInfo [0]) exit ('watermark image must be smaller than target image ');
If ($ r_height> $ this-> imageInfo [1]) exit ('watermark image must be smaller than target image ');
  
Switch ($ s_info ['Mime ']) {
Case 'image/GIF ':
$ Resource = imagecreatefromgif ($ image );
Break;
Case 'image/jpeg ':
Case 'image/jpg ':
$ Resource = imagecreatefromjpeg ($ image );
Break;
Case 'image/png ':
$ Resource = imagecreatefrompng ($ image );
Break;
Default:
Exit ($ s_info ['Mime '].' type cannot be used as the watermark source .');
Break;
  }
  
$ This-> target = & $ this-> imageResource;
Imagecopymerge ($ this-> target, $ resource, $ this-> imageInfo [0]-$ r_width-5, $ this-> imageInfo [1]-$ r_height-5, 0, 0, $ r_width, $ r_height, $ app );
Imagedestroy ($ resource );
Unset ($ resource );
 }
 
/**
* Create image
  *
* @ Param string $ name
* @ Return boolean
*/
Function create ($ name = NULL ){
$ Function = $ this-> createFunc;
If ($ this-> target! = NULL & is_resource ($ this-> target )){
If ($ name! = NULL ){
$ Function ($ this-> target, $ name );
} Else {
$ Function ($ this-> target );
   }
Return true;
} Else if ($ this-> imageResource! = NULL & is_resource ($ this-> imageResource )){
If ($ name! = NULL ){
$ Function ($ this-> imageResource, $ name );
} Else {
$ Function ($ this-> imageResource );
   }
Return true;
} Else {
Exit ('the image cannot be created because the image source is not set .');
  }
 }
 
/**
* Free resource
  *
*/
Function free (){
If (is_resource ($ this-> imageResource )){
@ Imagedestroy ($ this-> imageResource );
  }
If (is_resource ($ this-> target )){
@ Imagedestroy ($ this-> target );
  }
 }
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.