Thumbnail generation program

Source: Internet
Author: User
Tags abs chmod explode file size file upload

I always wanted to find a program for the thumbnail of an image on the Internet, but I still didn't find the program without deformation. Generally, the source image is scaled simply.
I think it may already exist, but I did not find it, so I can't write one by myself. In fact, it's not all written by myself,
On the basis of existing changes!
The source code is as follows:

<? Php
/*
* Copyright Description: This version is developed on the basis of "IEB_UPLOAD CLASS Ver 1.1".
* Image cropping will make the image distorted and distorted! I made changes based on the program, using data parameters and the original image
* Compare the file parameters (mainly the width and height) to obtain the proportional value. The scaled image is scaled in the same proportion as the original image. However
* The image is then taken from the center of the intermediate image to obtain the thumbnail. Of course, the image will be cropped, but it is minimized.
* Crop!
* Programming: swin. wang Email: php_in_china@yahoo.com.cn QQ: 592298893
*
* You can use this program without restrictions, but please keep the copyright information
×
× Illegal use:
* $ Upfos = new ieb_upload ('photo _ file', './up_img'); name of the photo_file form file. Do not add $;./up_img to the upload location.
* $ Old_file_name = $ upfos-> getName ();
* $ Old_file_fooder = $ upfos-> getExt ();
* $ File_size = $ upfos-> getSize ();
* $ File_name = $ upfos-> newName ();
* $ Upfos-> upload ($ file_name );
* $ Upfos-> thumb ("small _", 125,120 );
* $ New_file_name = $ upfos-> UpFile (); $ new_file_name name of the uploaded big Image
× $ Small_file_name = "small _". $ new_file_name; $ small_file_name name
*/
Class ieb_upload {
Var $ FormName; // file domain name
Var $ Directroy; // upload to the Directory
Var $ MaxSize; // maximum upload size
Var $ CanUpload; // whether the file can be uploaded
Var $ doUpFile; // name of the uploaded file
Var $ sm_File; // The name of the thumbnail.
Var $ Error; // Error parameter

Function ieb_upload ($ formName = '', $ dirPath ='', $ maxSize = 2097152) // (1024*2) * 1024 = 2097152 is 2 M
{
Global $ FormName, $ Directroy, $ MaxSize, $ CanUpload, $ Error, $ doUpFile, $ sm_File;
// Initialize various parameters
$ FormName = $ formName;
$ MaxSize = $ maxSize;
$ CanUpload = true;
$ DoUpFile = '';
$ Sm_File = '';
$ Error = 0;
  
If ($ formName = ''){
$ CanUpload = false;
$ Error = 1;
Break;
   }
  
If ($ dirPath = ''){
$ Directroy = $ dirPath;
} Else {
$ Directroy = $ dirPath .'/';
  }
}

// Check whether the file exists
Function scanFile ()
{
Global $ FormName, $ Error, $ CanUpload;
  
If ($ CanUpload ){
  
$ Scan = is_readable ($ _ FILES [$ FormName] ['name']);
   
If ($ scan ){
$ Error = 2;
   }
   
Return $ scan;
  }
}


// Get the file size
Function getSize ($ format = 'B ')
{
Global $ FormName, $ Error, $ CanUpload;
  
If ($ CanUpload ){
  
If ($ _ FILES [$ FormName] ['size'] = 0 ){
$ Error = 3;
$ CanUpload = false;
   }
   
Switch ($ format ){
Case 'B ':
Return $ _ FILES [$ FormName] ['size'];
Break;
   
Case'm ':
Return ($ _ FILES [$ FormName] ['size'])/(1024*1024 );
   }
   
  }
}

// Obtain the file type
Function getExt ()
{
Global $ FormName, $ Error, $ CanUpload;
  
If ($ CanUpload ){
$ Ext = $ _ FILES [$ FormName] ['name'];
$ ExtStr = explode ('.', $ ext );
$ Count = count ($ extStr)-1;
  }
Return $ extStr [$ count];
}

// Obtain the file name
Function getName ()
{
Global $ FormName, $ CanUpload;
  
If ($ CanUpload ){
Return $ _ FILES [$ FormName] ['name'];
  }
}

// Create a file name
Function newName ()
{
Global $ CanUpload, $ FormName;
  
If ($ CanUpload ){
$ FullName = $ _ FILES [$ FormName] ['name'];
$ ExtStr = explode ('.', $ FullName );
$ Count = count ($ extStr)-1;
$ Ext = $ extStr [$ count];
   
Return date ('ymdhis '). rand ().'. '. $ ext;
  }
}

// Upload a file
Function upload ($ fileName = '')
{
Global $ FormName, $ Directroy, $ CanUpload, $ Error, $ doUpFile;
  
If ($ CanUpload ){
If ($ _ FILES [$ FormName] ['size'] = 0 ){
$ Error = 3;
$ CanUpload = false;
Return $ Error;
Break;
   }
  }
  
If ($ CanUpload ){
  
If ($ fileName = ''){
$ FileName = $ _ FILES [$ FormName] ['name'];
   }
      
$ DoUpload = @ copy ($ _ FILES [$ FormName] ['tmp _ name'], $ Directroy. $ fileName );
   
If ($ doUpload)
   {
$ DoUpFile = $ fileName;
Chmod ($ Directroy. $ fileName, 0777 );
Return true;
} Else {
$ Error = 4;
Return $ Error;
   }
  }
}

// Create an image thumbnail
Function thumb ($ dscChar = '', $ width = 160, $ height = 120)
{
Global $ CanUpload, $ Error, $ Directroy, $ doUpFile, $ sm_File;
  
If ($ CanUpload & $ doUpFile! = ''){
$ SrcFile = $ doUpFile;
   
If ($ dscChar = ''){
$ DscChar = 'SM _';
   }
   
$ DscFile = $ Directroy. $ dscChar. $ srcFile;
$ Data = getimagesize ($ Directroy. $ srcFile, & $ info );
   
Switch ($ data [2]) {
Case 1:
$ Im = @ imagecreatefromgif ($ Directroy. $ srcFile );
Break;
   
Case 2:
$ Im = @ imagecreatefromjpeg ($ Directroy. $ srcFile );
Break;
   
Case 3:
$ Im = @ imagecreatefrompng ($ Directroy. $ srcFile );
Break;
   }
   
$ SrcW = imagesx ($ im );
$ SrcH = imagesy ($ im );
   
If ($ srcW/$ width) >=( $ srcH/$ height )){
$ Temp_height = $ height;
$ Temp_width = $ srcW/($ srcH/$ height );
$ Src_X = abs ($ width-$ temp_width)/2 );
$ Src_Y = 0;
        }
Else {
$ Temp_width = $ width;
$ Temp_height = $ srcH/($ srcW/$ width );
$ Src_X = 0;
$ Src_Y = abs ($ height-$ temp_height)/2 );
                }
$ Temp_img = imagecreatetruecolor ($ temp_width, $ temp_height );
Imagecopyresized ($ temp_img, $ im, 0, 0, 0, $ temp_width, $ temp_height, $ srcW, $ srcH );
       
       
$ Ni = imagecreatetruecolor ($ width, $ height );
Imagecopyresized ($ ni, $ temp_img, 0, 0, $ src_X, $ src_Y, $ width, $ height, $ width, $ height );
$ Cr = imagejpeg ($ ni, $ dscFile );
Chmod ($ dscFile, 0777 );
          
  
If ($ cr ){
$ Sm_File = $ dscFile;
Return true;
} Else {
$ Error = 5;
Return $ Error;
   }
  }
}

// Display error parameters
Function Err (){
Global $ Error;
Return $ Error;
}

// Name of the uploaded file
Function UpFile (){
Global $ doUpFile, $ Error;
If ($ doUpFile! = ''){
Return $ doUpFile;
} Else {
$ Error = 6;
  }
}

// File Upload path
Function filePath (){
Global $ Directroy, $ doUpFile, $ Error;
If ($ doUpFile! = ''){
Return $ Directroy. $ doUpFile;
} Else {
$ Error = 6;
  }  
}

// Thumbnail file name
Function thumbMap (){
Global $ sm_File, $ Error;
If ($ sm_File! = ''){
Return $ sm_File;
} Else {
$ Error = 6;
  }
}

// Display version information
Function ieb_version (){
Return 'swin img class Ver 0.1 ';
}
}
?>

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.