/**
- *
- * @ Author zhao jinhan
- * @ Date January 13, 2014 11:54:30
- * @ Email xb_zjh@126.com
- *
- */
- Header ('content-type: text/html; charset = utf-8 ');
- // Define the width and height of the thumbnail
- Define ('thumb _ width', 300 );
- Define ('thumb _ height', 300 );
- /**
- * Regenerate the uploaded file name
- * @ Return string
- * @ Author zhao jinhan
- *
- */
- Function _ file_type ($ filetype = null ){
- Switch ($ filetype)
- {
- Case "image/jpeg ":
- $ Fileextname = "jpg ";
- Break;
- Case "image/gif ":
- $ Fileextname = "gif ";
- Break;
- Case "image/png ":
- $ Fileextname = "png ";
- Break;
- Default:
- $ Fileextname = false;
- Break;
- }
- Return $ fileextname? Date ('ymdhis ', time ().'. '. $ fileextname: false;
- }
- /**
- *
- * @ Param string $ filename
- * @ Param string $ width
- * @ Param string $ height
- * @ Param string $ quality
- * @ Param string $ savepath
- * @ Return boolean
- */
- Function _ make_thumb ($ filename = '', $ width = THUMB_WIDTH, $ height = THUMB_HEIGHT, $ savepath = './upload '){
- If (file_exists ($ filename )){
- // Size of the uploaded image
- $ Imagesize = getimagesize ($ filename );
- $ Imagewidth = $ imagesize [0];
- $ Imageheight = $ imagesize [1];
- $ Mime = $ imagesize ['Mime '];
- // Width/height ratio
- $ Ratio = $ imagewidth/$ imageheight;
- // Create a background image
- $ Bgimg = imagecreatetruecolor ($ width, $ height );
- $ White = imagecolorallocate ($ BGB, 255,255,255 );
- // The background color is white.
- Imagefill ($ bgimg, 0, 0, $ white );
- If ($ mime = 'image/GIF '){
- $ Im = @ imagecreatefromgif ($ filename);/* Attempt to open */
- $ Outfun = 'imagegif ';
- } Elseif ($ mime = 'image/png '){
- $ Im = @ imagecreatefrompng ($ filename);/* Attempt to open */
- $ Outfun = 'imagepng ';
- } Else {
- $ Im = @ imagecreatefromjpeg ($ filename);/* Attempt to open */
- $ Outfun = 'imagejpeg ';
- }
- If ($ ratio> 1 ){
- // The width is large.
- If ($ imagewidth> $ width ){
- // Scale the image to the background image
- $ New_width = $ width;
- $ New_height = ($ width * $ imageheight)/$ imagewidth;
- $ Bg_y = ceil (abs ($ height-$ new_height)/2 ));
- Imagecopyresampled ($ bgimg, $ im, 0, $ bg_y, 0, 0, $ new_width, $ new_height, $ imagewidth, $ imageheight );
- } Else {
- // Copy the image to the background image
- $ Copy = true;
- }
- } Else {
- // High
- If ($ imageheight> $ height ){
- // Scale the image
- $ New_height = $ height;
- $ New_width = ($ height * $ imagewidth)/$ imageheight;
- $ Bg_x = ceil ($ width-$ new_width)/2 );
- Imagecopyresampled ($ bgimg, $ im, $ bg_x, 0, 0, 0, $ new_width, $ new_height, $ imagewidth, $ imageheight );
- } Else {
- // Copy the image to the background image
- $ Copy = true;
- }
- }
- If ($ copy ){
- // Copy the image to the background image
- $ Bg_x = ceil ($ width-$ imagewidth)/2 );
- $ Bg_y = ceil ($ height-$ imageheight)/2 );
- Imagecopy ($ bgimg, $ im, $ bg_x, $ bg_y, 0, 0, $ imagewidth, $ imageheight );
- }
- $ Ext = _ file_type ($ mime );
- $ Outfun ($ bgimg, $ savepath. '/'. $ ext );
- Imagedestroy ($ bgimg );
- Return $ savepath. '/'. $ ext;
- } Else {
- Return false;
- }
- }
- If ($ _ POST ){
- $ Size = $ _ POST ['size']? Strtoupper (trim ($ _ POST ['size']): '2M ';
- $ Imgsize = $ _ FILES ['IMG '] ['size']? $ _ FILES ['IMG '] ['size']/(1024*1024): 0;
- $ Imgwidth = $ imgheight = $ _ POST ['width-height']? Intval ($ _ POST ['width-height']): 300;
- // Customize the file upload size
- Ini_set ('upload _ max_filesize ', $ size );
- $ Mathsize = str_replace ('M', '', $ size );
- If ($ imgsize> $ mathsize ){
- Echo "the image size cannot exceed {$ size }! ";
- Return;
- }
- If ($ file_name = _ file_type ($ _ FILES ['IMG '] ['type']) {
- If ($ _ FILES ['IMG '] ['error'] = UPLOAD_ERR_ OK ){
- $ Savepath = 'upload /';
- If (! Is_dir ($ savepath )){
- Mkdir ($ savepath, 0644 );
- }
- // Generate a thumbnail
- $ Thumb_file = _ make_thumb ($ _ FILES ['IMG '] ['tmp _ name'], $ imgwidth, $ imgheight, $ savepath );
- // Move_uploaded_file ($ _ FILES ['IMG '] ['tmp _ name'], $ savepath. $ file_name );
- Echo "the generated image is :";
- } Else {
- Echo $ _ FILES ['IMG '] ['error'];
- Return;
- }
- } Else {
- Echo "the image format is incorrect. Upload jpg, gif, and png formats! ";
- Return;
- }
} Else {
- Echo <
-
-
-
- Save the scaled image as a square
-
-
-
-
-
- EOT;
- }
|