-
/*
- Functions: resizing pictures or creating thumbnails
- return: True/false
- Parameters:
- $Image need to adjust the picture (with the path)
- $Dw =450 Maximum width when adjusting, absolute width when thumbnail
- $Dh Maximum height for =450 adjustment, absolute height at thumbnail
- $Type = 1 1, adjust the size; 2, generate thumbnail image
- Site http://bbs.it-home.org
- */
-
$path = ' img/';//path
- $phtypes =array (
- ' Img/gif ',
- ' Img/jpg ',
- ' Img/jpeg ',
- ' Img/bmp ',
- ' Img/pjpeg ',
- ' Img/x-png '
- );
-
Functionimg ($Image, $Dw =450, $Dh =450, $Type =1) {
- IF (! File_exists ($Image)) {
- Returnfalse;
- }
- #如果需要生成缩略图, copy the original image and assign it to $image again.
- IF ($Type!=1) {
- Copy ($Image, Str_replace (".", "_x.", $Image));
- $Image =str_replace (".", "_x.", $Image);
- }
-
#取得文件的类型, create different objects based on different types
- $ImgInfo =getimagesize ($Image);
- Switch ($ImgInfo [2]) {
- CASE1:
- $IMG = @ImageCreateFromGIF ($Image);
- break;
- CASE2:
- $IMG = @ImageCreateFromJPEG ($Image);
- break;
- CASE3:
- $IMG = @ImageCreateFromPNG ($Image);
- break;
- }
-
#如果对象没有创建成功, the non-picture file is indicated
- IF (Empty ($IMG)) {
- #如果是生成缩略图的时候出错, you need to delete the files that have been copied
- IF ($Type!=1) {Unlink ($Image);}
- Returnfalse;
- }
-
#如果是执行调整尺寸操作则
- IF ($Type ==1) {
- $w =imagesx ($IMG);
- $h =imagesy ($IMG);
- $width = $w;
- $height = $h;
- IF ($width > $Dw) {
- $Par = $Dw/$width;
- $width = $Dw;
- $height = $height * $PAR;
- IF ($height > $Dh) {
- $Par = $Dh/$height;
- $height = $Dh;
- $width = $width * $PAR;
- }
- }elseif ($height > $Dh) {
- $Par = $Dh/$height;
- $height = $Dh;
- $width = $width * $PAR;
- IF ($width > $Dw) {
- $Par = $Dw/$width;
- $width = $Dw;
- $height = $height * $PAR;
- }
- }else{
- $width = $width;
- $height = $height;
- }
- $NIMG =imagecreatetruecolor ($width, $height); #新建一个真彩色画布
- Imagecopyresampled ($NIMG, $IMG, 0,0,0,0, $width, $height, $w, $h); #重采样拷贝部分图像并调整大小
- Imagejpeg ($NIMG, $Image); #以JPEG格式将图像输出到浏览器或文件
- Returntrue;
- #如果是执行生成缩略图操作则
- }else{
- $w =imagesx ($IMG);
- $h =imagesy ($IMG);
- $width = $w;
- $height = $h;
-
$NIMG =imagecreatetruecolor ($Dw, $Dh);
- IF ($h/$w > $Dh/$Dw) {#高比较大
- $width = $Dw;
- $height = $h * $Dw/$w;
- $IntNH = $height-$Dh;
- Imagecopyresampled ($NIMG, $IMG, 0,-$IntNH/1.8,0,0, $Dw, $height, $w, $h);
- }else{#宽比较大
- $height = $Dh;
- $width = $w * $Dh/$h;
- $IntNW = $width-$Dw;
- Imagecopyresampled ($NIMG, $IMG,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
- }
- Imagejpeg ($NIMG, $Image);
- Returntrue;
- }
- }
- ?>
Copy Code
2. Invoking the example
-
- /**
- Image compression Processing
- */
- if ($_server[' Request_method ']== ' POST ') {
- if (!is_uploaded_file ($_files["photo"][tmp_name])) {
- echo "Picture does not exist";
- Exit ();
- }
-
if (!is_dir (' img ')) {//path is created if it does not exist)
- mkdir (' img ');
- }
-
$upfile =$_files["Photo"];
- $pinfo =pathinfo ($upfile ["name"]);
- $name = $pinfo [' basename '];//file name
- $tmp _name= $upfile ["Tmp_name"];
- $file _type= $pinfo [' extension '];//get file type
- $showphpath = $path. $name;
- if (In_array ($upfile ["type"], $phtypes)) {
- echo "file type does not match! ";
- Exit ();
- }
- if (Move_uploaded_file ($tmp _name, $path. $name)) {
- echo "Success!" ";
- IMG ($showphpath, 100,800,2);
- }
- echo "";
- }
- ?>
-
Copy Code |