- /**
- *
- * Function: adjust the image size or generate a thumbnail.
- * Modification:
- * Return value: True/False
- * Parameters:
- * $ Image the Image to be adjusted (including the path)
- * $ Dw = 450 max width when adjusted; absolute width when thumbnails
- * $ Dh = 450 max. height when adjusted; absolute height when thumbnails
- * $ Type = 1 1, adjust the size; 2, generate a thumbnail
- * 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 "image does not exist ";
- Return false;
- }
- Echo "images exist ";
- // If you need to generate a thumbnail, copy the source Image and assign a value to $ Image (create a thumbnail)
- // When Type = 1, the original image file will not be copied, but the reduced image will be re-generated on the original image file (resize operation)
- IF ($ Type! = 1 ){
- Copy ($ Image, str_replace (".", "_ x.", $ Image ));
- $ Image = str_replace (".", "_ x.", $ Image );
- }
- // Obtain the object type and create different objects based on 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 object is not an image file.
- IF (Empty ($ Img )){
- // If an error occurs when a thumbnail is generated, delete the copied file.
- IF ($ Type! = 1 ){
- Unlink ($ Image );
- }
- Return false;
- }
- // If you want to adjust the size
- 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 true color canvas
- ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h); // Copy part of the image and resize it.
- ImageJpeg ($ nImg, $ Image); // output the Image to a browser or file in JPEG format
- Return true;
- } Else {// if a thumbnail is generated
- $ W = ImagesX ($ Img );
- $ H = ImagesY ($ Img );
- $ Width = $ w;
- $ Height = $ h;
- $ NImg = ImageCreateTrueColor ($ Dw, $ Dh );
- IF ($ h/$ w> $ Dh/$ Dw) {// relatively high
- $ Width = $ Dw;
- $ Height = $ h * $ Dw/$ w;
- $ IntNH = $ height-$ Dh;
- ImageCopyReSampled ($ nImg, $ Img, 0,-$ intnh/ 1.8, 0, 0, $ Dw, $ height, $ w, $ h );
- } Else {// relatively wide
- $ Height = $ Dh;
- $ Width = $ w * $ Dh/$ h;
- $ IntNW = $ width-$ Dw;
- ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, $ width, $ Dh, $ w, $ h );
- }
- ImageJpeg ($ nImg, $ Image );
- Return true;
- }
- };
- ?>
2. obtain remote images
- // Network Image 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 line feed characters
- $ Name = strrchr ($ tempPath ,"/");
- $ Path = str_replace ($ name, '', $ tempPath); // replace line feed characters
- /**
- * Create a multi-level directory based on the path
- * $ Dir target directory $ mode permission. 0700 indicates the highest permission.
- */
- 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 ("failed to establish, please check the permission ");
- @ Chmod ($ dir, $ mode );
- Return $ dir;
- }
- } // End makedir
- Makedir ($ path );
- /**
- * Retrieve images on the server based on the 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 );
- $ Fp2 = @ fopen ($ filename, "");
- Fwrite ($ fp2, $ img );
- Fclose ($ fp2 );
- Return $ filename;
- }
- ?>
3. call example
- The file types that can be uploaded are:
- Echo $ path ."
";
- /**/
- $ BigImg = GrabImage ($ imgPath, $ tempPath );
- If ($ bigImg ){
- Echo'
';
- } Else {
- Echo "false ";
- }
- CompressImg ($ bigImg, 100,80, 1 );
- ?>
|