- /**
- *
- * Function: Resize the picture or create a thumbnail image
- * Revision: 2013-2-15
- * Return: True/false
- Parameters
- * $Image need to adjust the picture (including the path)
- * $Dw =450 The maximum width when adjusting; absolute width when thumbnail
- * $Dh Maximum height for =450 adjustment, absolute height when thumbnail
- * $Type = 1 1, adjust the size; 2, generate thumbnail image
- * Site http://bbs.it-home.org
- */
- $phtypes =array (' img/gif ', ' img/jpg ', ' img/jpeg ', ' img/bmp ', ' img/pjpeg ', ' img/x-png ');
- function compressimg ($Image, $Dw, $Dh, $Type) {
- Echo $Image;
- IF (!file_exists ($Image)) {
- echo "no picture exists";
- return false;
- }
- echo "Existence picture";
- If you need to generate thumbnails, copy the original image to $image assignment (generate thumbnail action)
- When Type==1, the original image file is not copied, but the reduced image is regenerated on the original image file (resizing operation)
- IF ($Type!=1) {
- Copy ($Image, Str_replace (".", "_x.", $Image));
- $Image =str_replace (".", "_x.", $Image);
- }
- Get the type of file, build different objects according to different types
- $ImgInfo =getimagesize ($Image);
- Switch ($ImgInfo [2]) {
- Case 1:
- $IMG = @imagecreatefromgif ($Image);
- Break
- Case 2:
- $IMG = @imagecreatefromjpeg ($Image);
- break;
- Case 3:
- $IMG = @imagecreatefrompng ($Image);
- Break
- }
- If the object is not created successfully, the non-picture file
- IF (Empty ($IMG)) {
- If an error occurs when generating thumbnails, you need to delete the files that have already been copied
- IF ($Type!=1) {
- Unlink ($Image);
- }
- return false;
- }
- If you are performing a resize operation
- 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);//Create a new True color canvas
- Imagecopyresampled ($NIMG, $IMG, 0,0,0,0, $width, $height, $w, $h);//Resample copy part of the image and resize it
- Imagejpeg ($NIMG, $Image);//output images to a browser or file in JPEG format
- return true;
- } Else {//If you are performing a build thumbnail operation
- $w =imagesx ($IMG);
- $h =imagesy ($IMG);
- $width = $w;
- $height = $h;
- $NIMG =imagecreatetruecolor ($Dw, $Dh);
- IF ($h/$w > $Dh/$Dw) {//height ratio large
- $width = $Dw;
- $height = $h * $Dw/$w;
- $IntNH = $height-$Dh;
- Imagecopyresampled ($NIMG, $IMG, 0,-$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
- } Else {//width ratio is large
- $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);
- return true;
- }
- };
- ?>
Copy Code2. Get remote pictures
- Network picture Path
- $imgPath = ' http://bbs.it-home.org/phone/compress-img/251139474ba926db3d7850.jpg ';
- $imgPath = "Http://bbs.it-home.org/userfiles/image/20111125/251139474ba926db3d7850.jpg";
- $tempPath = Str_replace (' http://bbs.it-home.org/', ' ', $imgPath);//Replace newline character
- $name = STRRCHR ($tempPath, "/");
- $path = Str_replace ($name, ", $tempPath);//Replace newline character
- /**
- * Set up a multi-level directory based on paths path
- * $dir The target directory $mode permissions, 0700 indicates the highest privilege
- */
- function MakeDir ($dir, $mode = "0700") {
- if (Strpos ($dir, "/")) {
- $dir _path = "";
- $dir _info = Explode ("/", $dir);
- foreach ($dir _info as $key = + $value) {
- $dir _path. = $value;
- if (!file_exists ($dir _path)) {
- @mkdir ($dir _path, $mode) or Die ("Failed to create folder");
- @chmod ($dir _path, $mode);
- } else {
- $dir _path. = "/";
- Continue;
- }
- $dir _path. = "/";
- }
- return $dir _path;
- } else {
- @mkdir ($dir, $mode) or Die ("Setup failed, check permissions");
- @chmod ($dir, $mode);
- return $dir;
- }
- }//end MakeDir
- MakeDir ($path);
- /**
- * Get pictures on the server based on URL
- * $url Server Slice path $filename file name
- */
- function Grabimage ($url, $filename = "") {
- if ($url = = "") return false;
- if ($filename = = "") {
- $ext =STRRCHR ($url, ".");
- if ($ext! = ". gif" && $ext! = ". jpg" && $ext! = ". png")
- return false;
- $filename =date ("Ymdhis"). $ext;
- }
- Ob_start ();
- ReadFile ($url);
- $img = Ob_get_contents ();
- Ob_end_clean ();
- $size = strlen ($img);
- $FP 2= @fopen ($filename, "a");
- Fwrite ($fp 2, $img);
- Fclose ($fp 2);
- return $filename;
- }
- ?>
Copy Code3. Invoking the example
-
- The types of files that are allowed to be uploaded are:
- echo $path. "
";
- /**/
- $BIGIMG =grabimage ($imgPath, $tempPath);
- if ($BIGIMG) {
- Echo '
';
- } else {
- echo "false";
- }
- Compressimg ($BIGIMG, 100,80,1);
- ?>
-
-
Copy Code |