Copy codeThe Code is as follows:
<? Php
// Upload file type list
$ Uptypes = array (
'Image/jpg ',
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/gif ',
'Image/bmp ',
'Image/x-png'
);
$ Max_file_size = 200000; // size limit of uploaded files, in bytes
$ Path_im = "prod_img/"; // generate a large image save folder path
$ Path_sim = "prod_simg/"; // folder path for saving thumbnails
$ Watermark = 1; // whether to add a watermark (1 indicates adding a watermark, and others indicates not adding a watermark );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ Waterstring = "[url = http://www.jy17.com/?http://www.jy17.com/#/url]"; // watermark string
$ Waterimg = "water.png"; // watermark image file path
$ Waterclearly = 100; // watermark transparency: 0-100; small and high transparency
$ Imclearly = 100; // The image definition is 0-100. The larger the number, the clearer the file size.
$ Simclearly = 75; // The thumbnail definition ranges from 0 to 100. The larger the number, the clearer the file size.
$ Smallmark = 1; // whether to generate a thumbnail (1 is generated by adding a thumbnail, others are not );
$ Dst_sw = 80; // define the width and height of the thumbnail. I use proportional scaling, so you only need to compare the width.
?>
<Form enctype = "multipart/form-data" method = "post" name = "upform">
Upload files:
<Input name = "upfile" type = "file">
<Input type = "submit" value = "Upload"> <br>
The file type that can be uploaded is: <? = Implode (',', $ uptypes)?>
</Form>
<? Php
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "the image does not exist! ";
Exit;
}
$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{$ Max_file_size = $ max_file_size/1000;
Echo "the file is too large. It exceeds". $ max_file_size. "KB! ";
Exit;
}
If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "file type does not match! ". $ File [" type "];
Exit;
}
If (! File_exists ($ path_im ))
// Check whether the upload directory exists and does not exist
{
Mkdir ($ path_im );
}
If (! File_exists ($ path_sim ))
// Check whether the thumbnail directory exists and does not exist
{
Mkdir ($ path_sim );
}
$ Filename = $ file ["tmp_name"];
$ Im_size = getimagesize ($ filename );
$ Src_w = $ im_size [0];
$ Src_h = $ im_size [1];
$ Src_type = $ im_size [2];
$ Pinfo = pathinfo ($ file ["name"]);
$ Filetype = $ pinfo ['extension'];
$ All_path = $ path_im.time (). ".". $ filetype; // path + file name. The name of the uploaded time is
If (file_exists ($ all_path ))
{
Echo "a file with the same name already exists ";
Exit;
}
If (! Move_uploaded_file ($ filename, $ all_path ))
{
Echo "An error occurred while moving the file ";
Exit;
}
$ Pinfo = pathinfo ($ all_path );
$ Fname = $ pinfo [basename];
Echo "<font color = red> uploaded successfully </font> <br> file name: <font color = blue> ". $ all_path. "</font> <br> ";
Echo "width:". $ src_w. "px ";
Echo "Length:". $ src_h. "px ";
Echo "<br> size:". $ file ["size"]. "bytes ";
Switch ($ src_type) // determine the source image file type
{
Case 1: // gif
$ Src_im = imagecreatefromgif ($ all_path); // obtain the image from the source Image File
Break;
Case 2: // jpg
$ Src_im = imagecreatefromjpeg ($ all_path );
Break;
Case 3: // png
$ Src_im = imagecreatefrompng ($ all_path );
Break;
// Case 6:
// $ Src_im = imagecreatefromwbmp ($ all_path );
// Break;
Default:
Die ("unsupported file types ");
Exit;
}
If ($ watermark = 1)
{
// $ Iinfo = getimagesize ($ all_path, $ iinfo );
$ Dst_im = imagecreatetruecolor ($ src_w, $ src_h );
// Create a true color bitmap of the same size based on the source image size
$ White = imagecolorallocate ($ dst_im, 255,255,255); // white
// Fill in the background color for the new image
$ Black = imagecolorallocate ($ dst_im, 0, 0); // black
$ Red = imagecolorallocate ($ dst_im, 255, 0); // red
$ Orange = imagecolorallocate ($ dst_im, 255, 85, 0); // orange
Imagefill ($ dst_im, 0, 0, $ white );
Imagecopymerge ($ dst_im, $ src_im, 100, $ src_w, $ src_h,); // write the original image to the new true color bitmap.
// Imagefilledrectangle ($ dst_im, 1, $ src_h-15, 80, $ src_h, $ white );
Switch ($ watertype)
{
Case 1: // Add a watermark string
Imagestring ($ dst_im, 5, 5, $ src_h-20, $ waterstring, $ orange); // text watermark, font 5 color orange, located in the lower left corner of the background image
Break;
Case 2: // Add a watermark image
$ Lim_size = getimagesize ($ waterimg); // obtain the watermark image size and information
Switch ($ lim_size [2]) // determine the watermark image file type
{
Case 1: // gif
$ Src_lim = imagecreatefromgif ($ waterimg); // obtain the watermark image
Break;
Case 2: // jpg
$ Src_lim = imagecreatefromjpeg ($ waterimg );
Break;
Case 3: // png
$ Src_lim = imagecreatefrompng ($ waterimg );
Break;
// Case 6:
// $ Src_im = imagecreatefromwbmp ($ waterimg );
// Break;
Default:
Die ("unsupported file types ");
Exit;
}
$ Src_lw = ($ src_w-$ lim_size [0])/2; // position the watermark in the center width of the background image
$ Src_lh = ($ src_h-$ lim_size [1])/2; // height Positioning
Imagecopymerge ($ dst_im, $ src_lim, $ src_lw, $ src_lh, 0, 0, $ lim_size [0], $ lim_size [1], $ waterclearly); // merge two images, set watermark transparency $ waterclearly
Imagedestroy ($ src_lim );
Break;
}
Switch ($ src_type)
{
Case 1:
Imagegif ($ dst_im, $ all_path, $ imclearly); // generates a gif file. The image definition is 0-100.
Break;
Case 2:
Imagejpeg ($ dst_im, $ all_path, $ imclearly); // generate a jpg file. The image definition is 0-100.
Break;
Case 3:
Imagepng ($ dst_im, $ all_path, $ imclearly); // generate a png file. The image definition is 0-100.
Break;
// Case 6:
// Imagewbmp ($ dst_im, $ all_path );
Break;
}
// Release the cache
Imagedestroy ($ dst_im );
}
If ($ smallmark = 1)
{
$ Sall_path = $ path_sim.time (). ".". $ filetype;
If (file_exists ($ sall_path ))
{
Echo "a file with the same name already exists ";
Exit;
}
If ($ src_w <= $ dst_sw) // source image size <= thumbnail size
{
$ Dst_sim = imagecreatetruecolor ($ src_w, $ src_h); // create a thumbnail true color bitmap
Imagecopymerge ($ dst_sim, $ src_im, 100, $ src_w, $ src_h,); // write the original image to the new true color bitmap.
}
If ($ src_w> $ dst_sw) // source image size> thumbnail size
{
$ Dst_sh = $ dst_sw/$ src_w * $ src_h;
$ Dst_sim = imagecreatetruecolor ($ dst_sw, $ dst_sh); // create a thumbnail true color Bitmap (proportional reduction of the source image size)
Imagecopyresampled ($ dst_sim, $ src_im, $ dst_sw, $ dst_sh, $ src_w, $ src_h); // write the original image to the new true color bitmap.
}
Switch ($ src_type)
{
Case 1:
Imagegif ($ dst_sim, $ sall_path, $ simclearly); // generates a gif file. The image definition is 0-100.
Break;
Case 2:
Imagejpeg ($ dst_sim, $ sall_path, $ simclearly); // generate a jpg file. The image definition is 0-100.
Break;
Case 3:
Imagepng ($ dst_sim, $ sall_path, $ simclearly); // generate a png file. The image definition is 0-100.
Break;
// Case 6:
// Imagewbmp ($ dst_sim, $ sall_path );
Break;
}
// Release the cache
Imagedestroy ($ dst_sim );
}
// Release the cache
Imagedestroy ($ src_im );
}
?>
Function 2
Copy codeThe Code is as follows:
Function reSizeImg ($ imgSrc, $ resize_width, $ resize_height, $ isCut = false ){
// Image type
$ Type = substr (strrchr ($ imgSrc, "."), 1 );
// Initialize the image
If ($ type = "jpg "){
$ Im = imagecreatefromjpeg ($ imgSrc );
}
If ($ type = "gif "){
$ Im = imagecreatefromgif ($ imgSrc );
}
If ($ type = "png "){
$ Im = imagecreatefrompng ($ imgSrc );
}
// Target image address
$ Full_length = strlen ($ imgSrc );
$ Type_length = strlen ($ type );
$ Name_length = $ full_length-$ type_length;
$ Name = substr ($ imgSrc, 0, $ name_length-1 );
$ Dstimg = $ name. "_ s.". $ type;
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
// Generate an image
// Ratio of the changed image
$ Resize_ratio = ($ resize_width)/($ resize_height );
// Ratio of the actual image
$ Ratio = ($ width)/($ height );
If ($ isCut) = 1) // cut Graph
{
If ($ ratio> = $ resize_ratio) // high priority
{
$ Newimg = imagecreatetruecolor ($ resize_width, $ resize_height );
Imagecopyresampled ($ newimg, $ im, 0, 0, 0, 0, $ resize_width, $ resize_height, ($ height) * $ resize_ratio), $ height );
ImageJpeg ($ newimg, $ dstimg );
}
If ($ ratio <$ resize_ratio) // The width is preferred.
{
$ Newimg = imagecreatetruecolor ($ resize_width, $ resize_height );
Imagecopyresampled ($ newimg, $ im, 0, 0, 0, 0, $ resize_width, $ resize_height, $ width, ($ width)/$ resize_ratio ));
ImageJpeg ($ newimg, $ dstimg );
}
} Else // No cut chart
{
If ($ ratio >=$ resize_ratio ){
$ Newimg = imagecreatetruecolor ($ resize_width, ($ resize_width)/$ ratio );
Imagecopyresampled ($ newimg, $ im, 0, 0, 0, 0, $ resize_width, ($ resize_width)/$ ratio, $ width, $ height );
ImageJpeg ($ newimg, $ dstimg );
}
If ($ ratio <$ resize_ratio ){
$ Newimg = imagecreatetruecolor ($ resize_height) * $ ratio, $ resize_height );
Imagecopyresampled ($ newimg, $ im, 0, 0, 0, 0, ($ resize_height) * $ ratio, $ resize_height, $ width, $ height );
ImageJpeg ($ newimg, $ dstimg );
}
}
ImageDestroy ($ im );
}