Php uploads an image and generates a specified size chart proportionally. This is an image scaling function, after the new image is uploaded to $ srcfile, the file is scaled down by $ thumbwidth to the maximum image width and $ thumbheitht to generate a small image.
Upload an image in the php tutorial and generate a specified size chart proportionally
This is an image scaling function. You can give the newly uploaded image $ srcfile and then compress the image size by using $ thumbwidth to reduce the maximum size of the Image Width and $ thumbheitht to reduce the image height and the maximum size to generate a small image.
Image Scaling Function
Parameter description:
$ Srcfile source image address;
$ Dir New Graph directory
$ Thumbwidth: Zoom out the maximum graph width
$ Thumbheitht zoom in to the maximum Image Height
$ Ratio by default, proportional scaling is reduced to 1 to a fixed size.
*/
Function makethumb ($ srcfile, $ dir, $ thumbwidth, $ thumbheight, $ ratio = 0)
{
// Determine whether a file exists
If (! File_exists ($ srcfile) return false;
// Generate a new file with the same name, but the directory is different
$ Imgname = explode ('/', $ srcfile );
$ Arrcount = count ($ imgname );
$ Dstfile = $ dir. $ imgname [$ arrcount-1];
// Thumbnail size
$ Tow = $ thumbwidth;
$ Toh = $ thumbheight;
If ($ tow <40) $ tow = 40;
If ($ toh <45) $ toh = 45;
// Obtain image information
$ Im = '';
If ($ data = getimagesize ($ srcfile )){
If ($ data [2] = 1 ){
$ Make_max = 0; // gif is not processed
If (function_exists ("imagecreatefromgif ")){
$ Im = imagecreatefromgif ($ srcfile );
}
} Elseif ($ data [2] = 2 ){
If (function_exists ("imagecreatefromjpeg ")){
$ Im = imagecreatefromjpeg ($ srcfile );
}
} Elseif ($ data [2] = 3 ){
If (function_exists ("imagecreatefrompng ")){
$ Im = imagecreatefrompng ($ srcfile );
}
}
}
If (! $ Im) return '';
$ 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 ($ ratio ){
$ Ftow = $ tow;
$ Ftoh = $ toh;
}
// Zoom out the image
If ($ srcw> $ tow | $ srch> $ toh | $ ratio ){
If (function_exists ("imagecreatetruecolor") & function_exists ("imagecopyresampled") & @ $ ni = imagecreatetruecolor ($ ftow, $ ftoh )){
Imagecopyresampled ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
} Elseif (function_exists ("imagecreate") & function_exists ("imagecopyresized") & @ $ ni = imagecreate ($ ftow, $ ftoh )){
Imagecopyresized ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
} Else {
Return '';
}
If (function_exists ('imagejpeg ')){
Imagejpeg ($ ni, $ dstfile );
} Elseif (function_exists ('imagepng ')){
Imagepng ($ ni, $ dstfile );
}
} Else {
// Directly copy smaller than the size
Copy ($ srcfile, $ dstfile );
}
Imagedestroy ($ im );
If (! File_exists ($ dstfile )){
Return '';
} Else {
Return $ dstfile;
}
}
?>