I have looked at PHP's image processing functions in the past few days. in the past, there were few requirements in this field, so I didn't have to worry about it. I feel that some simple functions of image processing are still acceptable. even if it is complicated, the GD database is 2.0.1 and still does not support Chinese. I have read several articles, if you want to use Chinese characters, you can only convert GB2312 to UNICODE before writing images. this is too troublesome,
I have looked at PHP's image processing functions in the past few days. in the past, there were few requirements in this field, so I didn't have to worry about it. I feel that some simple functions of image processing are still acceptable. even if it is complicated, the GD database is 2.0.1 and still does not support Chinese. I have read several articles, if you want to use Chinese characters, you can only convert GB2312 to UNICODE before writing the image. this is too troublesome. simply use English.
In the image generation part, you can define the maximum height and width of the image, which is suitable for news and album systems.
GD2.0.1 has greatly improved image processing. I tried imageCopyResized and imageCopyResampled, and the latter processed images much better than the former, according to the manual, the latter basically keeps the image with the changed size undistorted, and the effect of generating thumbnails is really good.
-------------------------------------------------------------
Below is the class
-----------------------
// FileName: GDImage. inc. php
// Summary: Image processing program
// Author: ice_berg16 (Scarecrow for dream searching)
// CreateTime: 2004-10-12
// LastModifed: 2004-10-12
// Copyright (c) 2004 ice_berg16@163.com
// ================================================ ====================
Class GDImage
{
Var $ sourcePath; // image storage path
Var $ galleryPath; // path for storing thumbnail images
Var $ toFile = false; // whether to generate a file
Var $ fontName; // TTF font name used
Var $ maxWidth = 500; // maximum image width
Var $ maxHeight = 600; // maximum image height
// ================================================ ====
// Function: GDImage ($ sourcePath, $ galleryPath, $ fontPath)
// Function: constructor
// Parameter: $ sourcePath image source path (including the last "/")
// Parameter: $ galleryPath: Path of the image generated
// Parameter: $ fontPath font path
// ================================================ ====
Function GDImage ($ sourcePath, $ galleryPath, $ fontPath)
{
$ This-> sourcePath = $ sourcePath;
$ This-> galleryPath = $ galleryPath;
$ This-> fontName = $ fontPath. "04b_082.16.ttf ";
}
// ================================================ ====
// Function: makeThumb ($ sourFile, $ width = 128, $ height = 128)
// Function: generate a thumbnail (output to the browser)
// Parameter: $ sourFile image source file
// Parameter: $ width: width of the thumbnail generated
// Parameter: $ height: height of the thumbnail generated
// Return: the generated image path is returned when 0 fails.
// ================================================ ====
Function makeThumb ($ sourFile, $ width = 128, $ height = 128)
{
$ ImageInfo = $ this-> getInfo ($ sourFile );
$ SourFile = $ this-> sourcePath. $ sourFile;
$ NewName = substr ($ imageInfo ["name"], 0, strrpos ($ 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 = imagecreatefrompng ($ 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: add a watermark to the image
// Parameter: $ sourFile image file name
// Parameter: $ text array (contains two strings)
// Return: 1. the generated image path is returned when the operation succeeds.
// ================================================ ====
Function waterMark ($ sourFile, $ 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 = imagecreatefrompng ($ 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"]);
}
$ Whitelist = imageColorAllocate ($ new, 255,255,255 );
$ Black = imageColorAllocate ($ new, 0, 0, 0 );
$ Alpha = imageColorAllocateAlpha ($ new, 230,230,230, 40 );
// $ RectW = 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)
// Function: Display the thumbnail of a specified image
// Parameter: $ file name
// Return value: 0. The image does not exist.
// ================================================ ====
Function displayThumb ($ file)
{
$ ThumbName = substr ($ file, 0, strrpos ($ file, "."). "_thumb.jpg ";
$ File = $ this-> galleryPath. $ thumbName;
If (! File_exists ($ file ))
Return 0;
$ Html = ""
Ho $ html;
}
// ================================================ ====
// Function: displayMark ($ file)
// Function: Display the watermark of the specified image
// Parameter: $ file name
// Return value: 0. The image does 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 image information
// Parameter: $ file path
// Return: image 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;
}
}
?>
----------------------------------
The following is how to use
This class uses a 04b_082.16.ttf font.
Specify the font path when using the class.
-----------------------------------
Require_once ("GDImage. inc. php ");
// Header ("Content-type: image/jpeg"); // do not forget to open this
$ 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 ");