Supports file Upload compatibility and good image thumbnails

Source: Internet
Author: User
Tags file upload imagejpeg php tutorial

The following provides three generated reduction codes, which have good compatibility. You can also customize the height and width.

Function imageresize ($ srcfile, $ tow, $ toh, $ tofile = "")
{
If ($ tofile = "") {$ tofile = $ srcfile ;}
$ Info = "";
$ Data = getimagesize ($ srcfile, $ info );
Switch ($ data [2])
{
Case 1:
If (! Function_exists ("imagecreatefromgif ")){
Echo "your gd library cannot use images in gif format. Please use jpeg or png format! <A href = 'webpage effect: go (-1); '> return </a> ";
Exit ();
}
$ Im = imagecreatefromgif ($ srcfile );
Break;
Case 2:
If (! Function_exists ("imagecreatefromjpeg ")){
Echo "your gd library cannot use images in jpeg format. Please use images in other formats! <A href = 'javascript: go (-1); '> return </a> ";
Exit ();
}
$ Im = imagecreatefromjpeg ($ srcfile );
Break;
Case 3:
$ Im = imagecreatefrompng ($ srcfile );
Break;
}
$ Srcw = imagesx ($ im );
$ Srch = imagesy ($ im );
$ Towh = $ tow/$ toh;
$ Srcwh = $ srcw/$ srch;
If ($ towh <= $ srcwh ){
$ Ftow = $ tow;
$ Ftoh = $ ftow * ($ srch/$ srcw );
}
Else {
$ Ftoh = $ toh;
$ Ftow = $ ftoh * ($ srcw/$ srch );
}
If ($ srcw> $ tow | $ srch> $ toh)
{
If (function_exists ("imagecreatetruecolor ")){
@ $ Ni = imagecreatetruecolor ($ ftow, $ ftoh );
If ($ ni) imagecopyresampled ($ ni, $ im, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
Else {
$ Ni = imagecreate ($ ftow, $ ftoh );
Imagecopyresized ($ ni, $ im, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
}
} Else {
$ Ni = imagecreate ($ ftow, $ ftoh );
Imagecopyresized ($ ni, $ im, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
}
If (function_exists ('imagejpeg ') imagejpeg ($ ni, $ tofile );
Else imagepng ($ ni, $ tofile );
Imagedestroy ($ ni );
}
Imagedestroy ($ im );
}

Instance code 2

<? Php Tutorial
/* Constructor-generate thumbnails + watermarks. Parameter description:
$ Srcfile-image file name,
$ Dstfile-save another file name,
$ Markwords-watermark text,
$ Markimage-watermark image,
$ Dstw-Image save width,
$ Dsomething-image storage height,
$ Rate-image storage quality */
Makethumb ("a.jpg", "B .jpg", "50", "50 ");
Function makethumb ($ srcfile, $ dstfile, $ dstw, $ dsomething, $ rate = 100, $ markwords = null, $ markimage = null)
{
$ Data = getimagesize ($ srcfile );
Switch ($ data [2])
{
Case 1:
$ Im = @ imagecreatefromgif ($ srcfile );
Break;
Case 2:
$ Im = @ imagecreatefromjpeg ($ srcfile );
Break;
Case 3:
$ Im = @ imagecreatefrompng ($ srcfile );
Break;
}
If (! $ Im) return false;
$ Srcw = imagesx ($ im );
$ Srch = imagesy ($ im );
$ Dstx = 0;
$ Dsty = 0;
If ($ srcw * $ DTH> $ srch * $ dstw)
{
$ Fd…… = round ($ srch * $ dstw/$ srcw );
$ Dsty = floor ($ DTH-$ fdth)/2 );
$ Fdstw = $ dstw;
}
Else
{
$ Fdstw = round ($ srcw * $ dsomething/$ srch );
$ Dstx = floor ($ dstw-$ fdstw)/2 );
$ Fdth = $ DTH;
}
$ Ni = imagecreatetruecolor ($ dstw, $ dsomething );
$ Dstx = ($ dstx <0 )? 0: $ dstx;
$ Dsty = ($ dstx <0 )? 0: $ dsty;
$ Dstx = ($ dstx> ($ dstw/2 ))? Floor ($ dstw/2): $ dstx;
$ Dsty = ($ dsty> ($ dsomething/2 ))? Floor ($ dsomething/s): $ dsty;
$ White = imagecolorallocate ($ ni, 255,255,255 );
$ Black = imagecolorallocate ($ ni, 0, 0 );
Imagefilledrectangle ($ ni, $ dstw, $ DTH, $ white); // fill the background color
Imagecopyresized ($ ni, $ im, $ dstx, $ dsty, 0,0, $ fdstw, $ fdsomething, $ srcw, $ srch );
If ($ markwords! = Null)
{
$ Markwords = iconv ("gb2312", "UTF-8", $ markwords );
// Convert the text encoding
Imagettftext ($ ni, 20, 30, 450,560, $ black, "simhei. ttf", $ markwords); // write a text watermark
// The parameters are in sequence: text size | deflection degree | abscissa | ordinate | text color | text type | text content
}
Elseif ($ markimage! = Null)
{
$ Wimage_data = getimagesize ($ markimage );
Switch ($ wimage_data [2])
{
Case 1:
$ Wimage = @ imagecreatefromgif ($ markimage );
Break;
Case 2:
$ Wimage = @ imagecreatefromjpeg ($ markimage );
Break;
Case 3:
$ Wimage = @ imagecreatefrompng ($ markimage );
Break;
}
Imagecopy ($ ni, $ wimage, 500,560, 88,31); // write the image Watermark. The default image size is 88*31.
Imagedestroy ($ wimage );
}
Imagejpeg ($ ni, $ dstfile, $ rate );
Imagejpeg ($ ni, $ srcfile, $ rate );
Imagedestroy ($ im );
Imagedestroy ($ ni );
}
?>

Supports image upload code

<? Php

$ Pic_name = date ("dmyhis ");

// Generate the image width
$ Pic_width = $ _ post ['width'];

// Generate the image height
$ Pic_height = $ _ post ['length'];

Function resizeimage ($ im, $ maxwidth, $ maxheight, $ name ){
// Obtain the current image size
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
// Generate the size of the thumbnail
If ($ width> $ maxwidth) | ($ height> $ maxheight )){
$ Widthratio = $ maxwidth/$ width;
$ Heightratio = $ maxheight/$ height;
If ($ widthratio <$ heightratio ){
$ Ratio = $ widthratio;
} Else {
$ Ratio = $ heightratio;
   }
$ Newwidth = $ width * $ ratio;
$ Newheight = $ height * $ ratio;
  
If (function_exists ("imagecopyresampled ")){
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
} Else {
$ Newim = imagecreate ($ newwidth, $ newheight );
Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
   }
Imagejpeg ($ newim, $ name. ". jpg ");
Imagedestroy ($ newim );
} Else {
Imagejpeg ($ im, $ name. ". jpg ");
  }
 }

If ($ _ files ['image'] ['size']) {
// Echo $ _ files ['image'] ['type'];
If ($ _ files ['image'] ['type'] = "image/pjpeg" | $ _ files ['image'] ['type'] =" image/jpg "| $ _ files ['image'] ['type'] =" image/jpeg ") {
$ Im = imagecreatefromjpeg ($ _ files ['image'] ['tmp _ name']);
} Elseif ($ _ files ['image'] ['type'] = "image/x-png "){
$ Im = imagecreatefrompng ($ _ files ['image'] ['tmp _ name']);
} Elseif ($ _ files ['image'] ['type'] = "image/gif "){
$ Im = imagecreatefromgif ($ _ files ['image'] ['tmp _ name']);
  }
If ($ im ){
If(file_exists($pic_name.'.jpg ')){
Unlink($pic_name.'.jpg ');
   }
Resizeimage ($ im, $ pic_width, $ pic_height, $ pic_name );
Imagedestroy ($ im );
  }
 }
?>

"> <Br>
<Form enctype = "multipart/form-data" method = "post" action = "small_picture.php">
<Br>
<Input type = "file" name = "image" size = "50" value = ""> <p>
Width of the generated thumbnail: <input type = "text" name = "width" size = "5"> <p>
Length of the generated thumbnail: <input type = "text" name = "length" size = "5"> <p>
<Input type = "submit" value = "upload image">
</Form>

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.