PHP adds watermark & proportional thumbnails & fixed height & fixed width class. When using foreach loop processing, you need to set a time for sleep or follow the returned value after processing. Otherwise, the processing will not end.
Download: http://pan.baidu.com/s/1ntKAfFF
- // File name: image_process.class.php
- Class Image_process {
- Public $ source; // source image
- Public $ source_width; // width
- Public $ source_height; // high
- Public $ source_type_id;
- Public $ orign_name;
- Public $ orign_dirname;
- // Input the 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 );
- }
- // Judge and process, 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 watermarkImage ($ 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;
- $ Y = $ this-> source_height-$ logo_height;
- ImageCopy ($ sourceHandle, $ logoHandle, $ x, $ y, 0, $ logo_width, $ logo_width) or die ("fail to combine ");
- $ NewPic = $ this-> orign_dirname. '\ water _'. time (). '.'. $ this-> typeList [$ this-> source_type_id];
- If ($ this-> saveImage ($ sourceHandle, $ newPic )){
- Imagedestroy ($ sourceHandle );
- Imagedestroy ($ logoHandle );
- }
- }
- // Fix width
- // Height = true fixed top height
- // Width = true fixed top 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
- // Scale the scale of $ scale
- Public function scaleImage ($ scale ){
- $ Width = floor ($ this-> source_width * $ scale );
- $ Height = floor ($ this-> source_height * $ scale );
- $ This-> tinyImage ($ width, $ height );
- }
- // Create a thumbnail
- Private 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, $ width, $ height, $ this-> source_width, $ this-> source_height );
- }
- $ NewPic = time (). '_'. $ width. '_'. $ height. '.'. $ this-> typeList [$ this-> source_type_id];
- $ NewPic = $ this-> orign_dirname. '\ thumb _'. $ newPic;
- If ($ this-> saveImage ($ tinyImage, $ newPic )){
- Imagedestroy ($ tinyImage );
- Imagedestroy ($ handle );
- }
- }
- // Save the image
- Private function saveImage ($ image, $ url ){
- If (ImageJpeg ($ image, $ url )){
- Return true;
- }
- }
- }
- // Use
-
- Include ('image _ process. class. php ');
- $ M = array (
- 'D: \ myspace \ test \ image_process \ 1.jpg ',
- 'D: \ myspace \ test \ image_process \ 2.jpg ',
- 'D: \ myspace \ test \ image_process \ 3.jpg ',
- 'D: \ myspace \ test \ image_process \ 4.jpg'
- );
- $ Img = 'd: \ myspace \ test \ image_process \ 1.jpg ';
- $ Logo = 'd: \ myspace \ test \ image_process \ logo.png ';
- Foreach ($ m as $ item ){
- $ S = new Image_process ($ item );
- $ S-> watermarkImage ($ logo );
- $ S-> scaleImage (0, 0.8 );
- $ S-> fixSizeImage (200, false );
- Sleep (1 );
- }
|