PHP GD2 upload image/Text watermark/image watermark/equal scale thumbnail/implementation Code _php Tutorial

Source: Internet
Author: User
Tags imagejpeg unsupported
Copy CodeThe 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; Upload file size limit, unit byte
$path _im = "prod_img/"; Generate large Map Save folder path
$path _sim = "prod_simg/"; Thumbnail Save folder path
$watermark = 1; Whether Watermark is added (1 is watermark, the other is not watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterstring = "[Url=http://www.jy17.com/]http://www.jy17.com/[/url]"; Watermark String
$waterimg = "Water.png"; Watermark Picture File path
$waterclearly = 100; Watermark Transparency 0-100, digital small Transparent high
$imclearly = 100; Image clarity 0-100, the larger the number the clearer, the larger the file size
$simclearly = 75; Thumbnail sharpness 0-100, the larger the number the clearer, the larger the file size
$smallmark = 1; Whether to generate thumbnails (1 for addition, others are not);
$DST _SW = 80; Define the thumbnail width, height I use equal scale, so as long as the width can be
?>

if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name]))
Whether the file exists
{
echo "Picture does not exist!";
Exit
}
$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{$max _file_size = $max _file_size/1000;
echo "File is too large to exceed". $max _file_size. " Kb! ";
Exit
}
if (!in_array ($file ["type"], $uptypes))
Check file types
{
echo "file type does not match!". $file ["type"];
Exit
}
if (!file_exists ($path _im))
Check that the upload directory exists and that there is no create
{
mkdir ($path _im);
}
if (!file_exists ($path _sim))
Checks whether the thumbnail directory exists, does not exist, creates
{
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 + filename, currently named with upload time
if (file_exists ($all _path))
{
echo "file with the same name already exists";
Exit
}
if (!move_uploaded_file ($filename, $all _path))
{
echo "Error moving File";
Exit
}
$pinfo = PathInfo ($all _path);
$fname = $pinfo [basename];
echo "has been successfully uploaded
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 picture file type
{
Case 1://gif
$src _im = imagecreatefromgif ($all _path);//Get an image from the source picture 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 type");
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 original size
$white = Imagecolorallocate ($dst _im,255,255,255);//White
Fill a new picture with a background color
$black = Imagecolorallocate ($dst _im,0,0,0);//Black
$red = Imagecolorallocate ($dst _im,255,0,0);//Red
$orange = Imagecolorallocate ($dst _im,255,85,0);//Orange
Imagefill ($dst _im,0,0, $white);
Imagecopymerge ($dst _im, $src _im,0,0,0,0, $src _w, $src _h,100);//original image written in new True Color bitmap
Imagefilledrectangle ($dst _im,1, $src _h-15,80, $src _h, $white);
Switch ($watertype)
{
Case 1://Add watermark String
Imagestring ($dst _im,5,5, $src _h-20, $waterstring, $orange);//text watermark, Font No. 5th Color Orange, located in the lower left corner of the background map
Break
Case 2://Add watermark Picture
$lim _size = getimagesize ($waterimg); Get watermark image size, information
Switch ($lim _size[2])//Judging watermark picture file type
{
Case 1://gif
$src _lim = imagecreatefromgif ($waterimg); Get 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 type");
Exit
}
$src _LW = ($src _w-$lim _size[0])/2; The watermark is positioned in the center width of the background map
$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);//Generate GIF file, picture sharpness 0-100
Break
Case 2:
Imagejpeg ($dst _im, $all _path, $imclearly);//generate JPG files, picture sharpness 0-100
Break
Case 3:
Imagepng ($dst _im, $all _path, $imclearly);//Generate PNG file, picture sharpness 0-100
Break
Case 6:
Imagewbmp ($dst _im, $all _path);
Break
}
Releasing the cache
Imagedestroy ($dst _im);
}
if ($smallmark = = 1)
{
$sall _path = $path _sim.time (). ".". $filetype;
if (file_exists ($sall _path))
{
echo "file with the same name already exists";
Exit
}
if ($src _w <= $dst _sw)//original size <= thumbnail size
{
$DST _sim = Imagecreatetruecolor ($src _w, $src _h); New thumbnail True Color bitmap
Imagecopymerge ($dst _sim, $src _im,0,0,0,0, $src _w, $src _h,100); Original image written in new True Color bitmap
}
if ($src _w > $dst _sw)//original size > Thumbnail size
{
$DST _sh = $dst _sw/$src _w* $src _h;
$DST _sim = Imagecreatetruecolor ($dst _sw, $dst _sh); New thumbnail true-color bitmap (zoom out of the original size)
Imagecopyresampled ($dst _sim, $src _im,0,0,0,0, $dst _sw, $dst _sh, $src _w, $src _h); Original image written in new True Color bitmap
}
Switch ($src _type)
{
Case 1:
Imagegif ($dst _sim, $sall _path, $simclearly);//Generate GIF file, picture sharpness 0-100
Break
Case 2:
Imagejpeg ($dst _sim, $sall _path, $simclearly);//generate JPG files, picture sharpness 0-100
Break
Case 3:
Imagepng ($dst _sim, $sall _path, $simclearly);//Generate PNG file, picture sharpness 0-100
Break
Case 6:
Imagewbmp ($dst _sim, $sall _path);
Break
}
Releasing the cache
Imagedestroy ($dst _sim);
}
Releasing the cache
Imagedestroy ($src _im);
}
?>

PHP equal scale generate thumbnail function 2
Copy CodeThe code is as follows:
function resizeimg ($IMGSRC, $resize _width, $resize _height, $isCut =false) {
Type of picture
$type = substr (STRRCHR ($IMGSRC, "."), 1);
Initializing an image
if ($type = = "jpg") {
$im = Imagecreatefromjpeg ($IMGSRC);
}
if ($type = = "gif") {
$im = Imagecreatefromgif ($IMGSRC);
}
if ($type = = "png") {
$im = Imagecreatefrompng ($IMGSRC);
}
Destination 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);
Creating images
The proportions of the altered image
$resize _ratio = ($resize _width)/($resize _height);
Ratio of actual images
$ratio = ($width)/($height);
if (($isCut) = = 1)//crop
{
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)//Width First
{
$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 drawing
{
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);
}

http://www.bkjia.com/PHPjc/321752.html www.bkjia.com true http://www.bkjia.com/PHPjc/321752.html techarticle Copy the code 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 _fi ...

  • Related Article

    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.