PHP GD2 upload image/Text watermark/image watermark/proportional thumbnail/implementation code _php Tips

Source: Internet
Author: User
Tags imagejpeg mkdir strlen unsupported
Copy Code 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 _file_size = 200000; Upload file size limit, unit byte
$path _im = "prod_img/"; Generate large picture Save folder path
$path _sim = "prod_simg/"; Thumbnails Save Folder path
$watermark = 1; Whether Watermark is added (1 is watermark, other is without 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; Picture Clarity 0-100, the larger the number the clearer, the larger the file size
$simclearly = 75; Thumbnail clarity 0-100, the larger the number the clearer, the larger the file size
$smallmark = 1; Whether to generate thumbnails (1 is added, others are not);
$DST _SW = 80; Define thumbnail width, height i use equal scaling, so as long as the comparison width can be
?>
<form enctype= "Multipart/form-data" method= "post" name= "Upform" >
Upload file:
<input name= "upfile" type= "File" >
<input type= "Submit" value= "Upload" ><br>
The types of files allowed to upload are: <?=implode (', ', $uptypes)?>
</form>
<?php
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name))
Is there a file
{
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;
The echo "file is too large to exceed". $max _file_size. Kb! ";
Exit
}
if (!in_array ($file ["type"], $uptypes))
Check file type
{
echo "file type does not match!". $file [' type '];
Exit
}
if (!file_exists ($path _im))
Check if the upload directory exists, no creation
{
mkdir ($path _im);
}
if (!file_exists ($path _sim))
Check to see if the thumbnail directory exists and there is no creation
{
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 for upload time
if (file_exists ($all _path))
{
echo "file with the same name already exists";
Exit
}
if (!move_uploaded_file ($filename, $all _path))
{
echo "Move file Error";
Exit
}
$pinfo = PathInfo ($all _path);
$fname = $pinfo [basename];
echo "<font color=red> has been successfully uploaded </font><br> FileName: <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 source picture file type
{
Case 1://gif
$src _im = imagecreatefromgif ($all _path);//Get image from 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 background color for a new diagram
$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 write to 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 5th Color Orange, located in the lower left corner of the background image
Break
Case 2://Add watermark Picture
$lim _size = getimagesize ($waterimg); Get the watermark image size, information
Switch ($lim _size[2])//Determine 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; Watermark is located in the center width of 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);//Generate GIF file, picture definition 0-100
Break
Case 2:
Imagejpeg ($dst _im, $all _path, $imclearly);//generate JPG file, picture definition 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 (equal proportions to reduce 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 definition 0-100
Break
Case 2:
Imagejpeg ($dst _sim, $sall _path, $simclearly);//generate JPG file, picture definition 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 Code code as follows:

function resizeimg ($IMGSRC, $resize _width, $resize _height, $isCut =false) {
Type of picture
$type = substr (STRRCHR ($IMGSRC, "."), 1);
Initializing images
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);
Build image
The proportions of the altered image
$resize _ratio = ($resize _width)/($resize _height);
The proportions of the actual image
$ratio = ($width)/($height);
if (($isCut) = = 1)//Map
{
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 map
{
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);
}
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.