Phpgd2 Upload Image/text watermark/image watermark/proportional thumbnail/implementation code

Source: Internet
Author: User
Tags unsupported
Phpgd2 uploads image text watermark, image watermark, and other proportional thumbnail source code. For more information, see. The code is as follows:


// 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.
?>

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 "uploaded successfully
File name: ". $ all_path ."
";
Echo "width:". $ src_w. "px ";
Echo "length:". $ src_h. "px ";
Echo"
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

The 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 );
}

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.