-
/*
- Function: adjust the image size or generate a thumbnail.
- Return value: True/False
- Parameters:
- $ Image: the Image to be adjusted (including the path)
- $ Dw = 450 max width for resizing; absolute width for thumbnails
- $ Dh = 450 max. height when adjusting; absolute height when thumbnails
- $ Type = 1 1, adjust the size; 2, generate a thumbnail
- Site http://bbs.it-home.org
- */
-
$ Path = 'IMG/'; // path
- $ Phtypes = array (
- 'IMG/GIF ',
- 'IMG/jpg ',
- 'IMG/jpeg ',
- 'IMG/bmp ',
- 'IMG/pjpeg ',
- 'IMG/x-png'
- );
-
FunctionImg ($ Image, $ Dw = 450, $ Dh = 450, $ Type = 1 ){
- IF (! File_Exists ($ Image )){
- ReturnFalse;
- }
- # If you want to generate a thumbnail, copy the source Image and assign a value to $ Image.
- IF ($ Type! = 1 ){
- Copy ($ Image, Str_Replace (".", "_ x.", $ Image ));
- $ Image = Str_Replace (".", "_ x.", $ Image );
- }
-
# Obtain the file type and create different objects based on different types
- $ ImgInfo = GetImageSize ($ Image );
- Switch ($ ImgInfo [2]) {
- Case1:
- $ Img = @ ImageCreateFromGIF ($ Image );
- Break;
- Case2:
- $ Img = @ ImageCreateFromJPEG ($ Image );
- Break;
- Case3:
- $ Img = @ ImageCreateFromPNG ($ Image );
- Break;
- }
-
# If the object is not created successfully, it indicates that the object is not an image file.
- IF (Empty ($ Img )){
- # If an error occurs when generating a thumbnail, delete the copied file.
- IF ($ Type! = 1) {Unlink ($ Image );}
- ReturnFalse;
- }
-
# If you want to adjust the size
- IF ($ Type = 1 ){
- $ W = ImagesX ($ Img );
- $ H = ImagesY ($ Img );
- $ Width = $ w;
- $ Height = $ h;
- IF ($ width> $ Dw ){
- $ Par = $ Dw/$ width;
- $ Width = $ Dw;
- $ Height = $ height * $ Par;
- IF ($ height> $ Dh ){
- $ Par = $ Dh/$ height;
- $ Height = $ Dh;
- $ Width = $ width * $ Par;
- }
- } ElseIF ($ height> $ Dh ){
- $ Par = $ Dh/$ height;
- $ Height = $ Dh;
- $ Width = $ width * $ Par;
- IF ($ width> $ Dw ){
- $ Par = $ Dw/$ width;
- $ Width = $ Dw;
- $ Height = $ height * $ Par;
- }
- } Else {
- $ Width = $ width;
- $ Height = $ height;
- }
- $ NImg = ImageCreateTrueColor ($ width, $ height); # Create a true color canvas
- ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h); # Copy part of the image and resize it.
- ImageJpeg ($ nImg, $ Image); # output the Image to a browser or file in JPEG format
- ReturnTrue;
- # If you want to generate a thumbnail
- } Else {
- $ W = ImagesX ($ Img );
- $ H = ImagesY ($ Img );
- $ Width = $ w;
- $ Height = $ h;
-
$ NImg = ImageCreateTrueColor ($ Dw, $ Dh );
- IF ($ h/$ w> $ Dh/$ Dw) {# relatively high
- $ Width = $ Dw;
- $ Height = $ h * $ Dw/$ w;
- $ IntNH = $ height-$ Dh;
- ImageCopyReSampled ($ nImg, $ Img, 0,-$ IntNH/1.8, $ Dw, $ height, $ w, $ h );
- } Else {# relatively wide
- $ Height = $ Dh;
- $ Width = $ w * $ Dh/$ h;
- $ IntNW = $ width-$ Dw;
- ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, $ width, $ Dh, $ w, $ h );
- }
- ImageJpeg ($ nImg, $ Image );
- ReturnTrue;
- }
- }
- ?>
2. call example
-
- /**
- Image compression
- */
- If ($ _ SERVER ['request _ method'] = 'post '){
- If (! Is_uploaded_file ($ _ FILES ["photo"] [tmp_name]) {
- Echo "the image does not exist ";
- Exit ();
- }
-
If (! Is_dir ('IMG ') {// create a path if it does not exist
- Mkdir ('IMG ');
- }
-
$ Upfile = $ _ FILES ["photo"];
- $ Pinfo = pathinfo ($ upfile ["name"]);
- $ Name = $ pinfo ['basename']; // file name
- $ Tmp_name = $ upfile ["tmp_name"];
- $ File_type = $ pinfo ['extension']; // Obtain the file type
- $ Showphpath = $ path. $ name;
- If (in_array ($ upfile ["type"], $ phtypes )){
- Echo "file type does not match! ";
- Exit ();
- }
- If (move_uploaded_file ($ tmp_name, $ path. $ name )){
- Echo "successful! ";
- Img ($ showphpath, 100,800, 2 );
- }
- Echo "";
- }
- ?>
-
|