A class that generates thumbnails and copyright to the image

xiaoxiao2021-03-06  85

In recent days, I read the functionality of PHP's picture processing.

In the past, the demand in this area was less, but I didn't look at it. I have already seen it recently. I feel that some simple features can be, complicated, even if the GD Ku 20.0.1, still does not support Chinese, read a few articles, I want to use Chinese to convert GB2312 to Unicode and write pictures, It's too much trouble, and the sure is only used in English.

The image generating portion can define the maximum, wide, and compare the news and albums such as news and albums.

GD2.0.1 has great improvement in image processing,

I tried imagecopyresized and imagecopyresampled,

The latter processing is obviously better than the former. According to the manual, the latter re-sampling the picture after changing the size is not distorted, and the effect of generating thumbnails is really good.

The following is the original image (the original picture is too big, it is not posted)

http://www.1001w.net/mm.jpg

Thumbnail

After the addition of the right author (using imagecopyresample, there is basically no distortion

After adding the right, it is relatively powerful to use imagecopyResized.

-------------------------------------------------- -----------

Here is a class

-----------------------

/ / =========================================================================================================================================================================================== ====

// filename: gdimage.inc.php

// summary: image handler

// Author: Ice_berg16 (Dream Scarecrow)

// CreateTime: 2004-10-12

// LastModifed: 2004-10-12

// Copyright (C) 2004 ICE_BERG16@163.com

/ / =========================================================================================================================================================================================== ====

Class Gdimage

{

Var $ sourcePath; // Picture Storage Path

Var $ galleryPath; / / Picture Thumbnail Storage Path

Var $ TOFILE = false; // Does a file

Var $ fontname; // TTF font name used

Var $ maxWidth = 500; // Picture Maximum Width

Var $ maxHeight = 600; // Picture Maximum Height

/ / =================================================== // function: gdimage $ SOURLEPATH, $ GALLERYPATH, $ fontpath

// Function: Constructor

// Parameters: $ sourcePath image source path (including the last "/")

// Parameters: $ GalleryPath generates a path to the image

// Parameters: $ fontpath font path

/ / ===========================================

Function GDIMAGE ($ SOURCEPATH, $ GALLERYPATH, $ fontpath)

{

$ this-> SourcePath = $ sourcepath;

$ this-> gallerypath = $ gallerypath;

$ this-> fontname = $ fontpath. "04b_08 __. TTF";

}

/ / ===========================================

// Function: Makethumb ($ SOLLE, $ Width = 128, $ Height = 128)

// Features: Generate thumbnails (output to browser)

/ / Parameters: $ SOURFILE Image Source File

// Parameters: $ width generates the width of the thumbnail

// Parameters: $ Height Generates the height of the thumbnail

// Return: 0 Returns the generated image path when the failed success

/ / ===========================================

Function Makethumb ($ SOURFILE, $ Width = 128, $ Height = 128)

{

$ imageInfo = $ this-> getInfo ($ SOURFILE);

$ SOURFILE = $ THIS-> SourcePath. $ SOURFILE;

$ newName = Substr ($ imageInfo ["name"], 0, StrPos ($ ImageInfo ["Name"], ".")). "_thumb.jpg";

Switch ($ imageInfo ["type"])

{

Case 1: // GIF $ IMG = ImageCreateFromgif ($ SOURFILE);

Break;

Case 2: // jpg

$ IMG = ImageCreateFromJPEG ($ SOURFILE);

Break;

Case 3: // PNG

$ IMG = ImageCreateFromNG ($ SOURFILE);

Break;

DEFAULT:

Return 0;

Break;

}

IF ($ IMG)

Return 0;

$ width = ($ width> $ imageinfo ["width"])? $ imageinfo ["width"]: $ width;

$ Height = ($ Height> $ imageinfo ["height"])? $ imageinfo ["Height": $ Height;

$ SRCW = $ imageInfo ["width"];

$ SRCH = $ imageInfo ["height"];

IF ($ SRCW * $ Width> $ srch * $ heiGHT)

$ HEIGHT = ROUND ($ srch * $ width / $ srcw);

Else

$ width = round ($ SRCW * $ HEIGHT / $ SRCH);

// *

IF (Function_exists ("ImageCreatetrueColor") //gd2.0.1

{

$ new = imagecreatetruecolor ($ Width, $ HEIGHT);

ImageCopyResampled ($ NEW, $ IMG, 0, 0, 0, 0, $ Width, $ HEIGHT, $ ImageInfo ["Width"], $ imageInfo ["height"]);

}

Else

{

$ new = imagecreate ($ Width, $ HEIGHT);

ImageCopyResized ($ New, $ IMG, 0, 0, 0, 0, $ Width, $ HEIGHT, $ ImageInfo ["Width"], $ imageInfo ["height"]);

}

// * /

IF ($ this-> tofile)

{

IF (File_Exists ($ this-> gallerypath. $ newname))

Unlink ($ this-> gallerypath. $ newname);

Imagejpeg ($ NEW, $ THIS-> GalleryPath. $ NEWNAME);

Return $ this-> gallerypath. $ newname;

}

Else

{

Imagejpeg ($ new);

}

ImageDestroy ($ new);

ImageDestroy ($ IMG);

}

/ / ===========================================

// Function: Watermark ($ SOURFILE, $ TEXT)

// Function: give a picture watermark

// Parameters: $ SOURFILE Image File Name

// Parameters: $ text text array (including two strings)

// Return: 1 Returns the generated image path when successfully successfully

// ============================================================================================================ $ TEXT)

{

$ imageInfo = $ this-> getInfo ($ SOURFILE);

$ SOURFILE = $ THIS-> SourcePath. $ SOURFILE;

$ newname = Substr ($ imageinfo ["name"], 0, strrpos ($ imageInfo ["name"], ".")). "_mark.jpg";

Switch ($ imageInfo ["type"])

{

Case 1: // gif

$ IMG = ImageCreateFromgif ($ SOURFILE);

Break;

Case 2: // jpg

$ IMG = ImageCreateFromJPEG ($ SOURFILE);

Break;

Case 3: // PNG

$ IMG = ImageCreateFromNG ($ SOURFILE);

Break;

DEFAULT:

Return 0;

Break;

}

IF ($ IMG)

Return 0;

$ width = ($ this-> maxwidth> $ imageInfo ["width"])? $ imageinfo ["width"]: $ this-> maxwidth;

$ HEIGHT = ($ this-> maxheight> $ imageinfo ["height"])? $ imageinfo ["Height"]: $ this-> maxheight;

$ SRCW = $ imageInfo ["width"];

$ SRCH = $ imageInfo ["height"];

IF ($ SRCW * $ Width> $ srch * $ heiGHT)

$ HEIGHT = ROUND ($ srch * $ width / $ srcw);

Else

$ width = round ($ SRCW * $ HEIGHT / $ SRCH);

// *

IF (Function_exists ("ImageCreatetrueColor") //gd2.0.1

{

$ new = imagecreatetruecolor ($ Width, $ HEIGHT);

ImageCopyResampled ($ NEW, $ IMG, 0, 0, 0, 0, $ Width, $ HEIGHT, $ ImageInfo ["Width"], $ imageInfo ["height"]);

}

Else

{

$ new = imagecreate ($ Width, $ HEIGHT);

ImageCopyResized ($ New, $ IMG, 0, 0, 0, 0, $ Width, $ HEIGHT, $ ImageInfo ["Width"], $ imageInfo ["height"]);

}

$ white = imagecolorlocate ($ NEW, 255, 255, 255);

$ black = imagecoloralloccate ($ NEW, 0, 0, 0); $ alpha = ImageColoralLocateAlpha ($ NEW, 230, 230, 230, 40);

// $ recentw = max (Strlen ($ text [0]), Strlen ($ text [1])) * 7;

ImagefilledRectangle ($ New, 0, $ HEIGHT-26, $ Width, $ HEIGHT, $ Alpha)

ImageFilledRectangle ($ NEW, 13, $ HEIGHT-20, 15, $ HEIGHT-7, $ Black);

ImagetTftext ($ New, 4.9, 0, 20, $ HEIGHT-14, $ Black, $ THIS-> FontName, $ text [0]);

ImagetTftext ($ New, 4.9, 0, 20, $ HEIGHT-6, $ Black, $ THIS-> FontName, $ text [1]);

// * /

IF ($ this-> tofile)

{

IF (File_Exists ($ this-> gallerypath. $ newname))

Unlink ($ this-> gallerypath. $ newname);

Imagejpeg ($ NEW, $ THIS-> GalleryPath. $ NEWNAME);

Return $ this-> gallerypath. $ newname;

}

Else

{

Imagejpeg ($ new);

}

ImageDestroy ($ new);

ImageDestroy ($ IMG);

}

/ / ===========================================

// Function: Displaythumb ($ file)

// Features: Display thumbnails of the specified image

// Parameter: $ file file name

// Return: 0 pictures do not exist

/ / ===========================================

Function Displaythumb ($ file)

{

$ thumbname = substr ($ file, 0, strpos ($ file, "))." _thumb.jpg ";

$ FILE = $ this-> GalleryPath. $ thumbname;

IF (! File_exists ($ file))

Return 0;

$ html = "

Echo $ HTML;

}

/ / ================================================== // function: DisplayMark $ FILE)

// Features: Show the watermark map for the specified image

// Parameter: $ file file name

// Return: 0 pictures do not exist

/ / ===========================================

Function Displaymark ($ file)

{

$ Markname = Substr ($ File, 0, Strrpos ($ file, "))." _mark.jpg ";

$ file = $ this-> gallerypath. $ markname;

IF (! File_exists ($ file))

Return 0;

$ html = "

Echo $ HTML;

}

/ / ===========================================

// Function: getInfo ($ file)

// Function: Return to image information

// Parameters: $ file file path

// Return: Picture information array

/ / ===========================================

Function GetInfo ($ file)

{

$ FILE = $ this-> sourcepath. $ file;

$ DATA = GetImagesize ($ file);

$ imageInfo ["width"] = $ data [0];

$ imageInfo ["height"] = $ data [1];

$ imageInfo ["type"] = $ data [2];

$ imageInfo ["name"] = basename ($ file);

RETURN $ imageInfo;

}

}

?>

----------------------------------

Below is the method of use

This class uses a 04B_08 __. TTF font

Specify the font path when using the class

----------------------------------- Require_once ("gdimage.inc.php");

// Header ("Content-Type: Image / JPEG"); / / Don't forget to open this to the browser

$ img = new gdimage (ib_upload_path, ib_gallery, ib_temp);

$ text = array ("Ice-berg.org", "all rights reserved");

$ IMG-> MaxWidth = $ IMG-> maxHEight = 300;

$ IMG-> TOFILE = TRUE;

$ IMG-> Watermark ("mm.jpg", $ text);

$ IMG-> Makethumb ("mm.jpg");

$ IMG-> Displaythumb ("mm.jpg");

$ IMG-> DisplayMark ("mm.jpg");

转载请注明原文地址:https://www.9cbs.com/read-106120.html

New Post(0)