; Resize and Save Image Rel 0.01 -Created by graechan
; Comments directed to http://gimpchat.com or http://gimpscripts.com
;
; License: GPLv3
;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;    GNU General Public License for more details.
;
;    To view a copy of the GNU General Public License
;    visit: http://www.gnu.org/licenses/gpl.html
;
;    Rel -0.01 Initial Release
;    
;;;;
(define (script-fu-resize-and-save-image image drawable
                                      out-width
                                      out-height
									  method
									  savepng
									  savejpg)
							  

 (let* (
            (image-layer (car (gimp-image-get-active-layer image)))
			(width (car (gimp-image-width image)))
			(height (car (gimp-image-height image)))
            (dupImage 0)
			(dup-layer 0)			
            (shortname (string-append (unbreakupstr (butlast (strbreakup (car (gimp-image-get-name image)) ".")) ".") "-" (number->string out-width) "x" (number->string out-height)))
			(fullname (string-append (unbreakupstr (butlast (strbreakup (car (gimp-image-get-filename image)) ".")) ".") "-" (number->string out-width) "x" (number->string out-height)))
			;(handler (car (gimp-message-get-handler)))
			 
		)	
	(gimp-context-push)
    (gimp-image-undo-group-start image)
	(gimp-context-set-foreground '(0 0 0))
	(gimp-context-set-background '(255 255 255))


;;;; Create a copy of the image
	(set! dupImage (car (gimp-image-duplicate image)))
	(set! dup-layer (car (gimp-image-merge-visible-layers dupImage EXPAND-AS-NECESSARY)))
	
	(if (= method 1) (gimp-image-scale-full dupImage out-width out-height 2))
	
	(if (= method 0) (plug-in-lqr 1 dupImage dup-layer out-width out-height 0 1000 0 1000 0 0 1 100 1 1 0 0 3 0 0 0 0 1 "" "" "" ""))


    	
 (if(= savepng TRUE)
   (begin 
;;;;save file as png
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".png")))
      (gimp-file-save 1 dupImage dup-layer (string-append fullname ".png") (string-append shortname ".png"))
   )
 )
					 
 (if(= savejpg TRUE)
   (begin
;;;;save file as jpg
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".jpg")))
      (gimp-file-save 1 dupImage dup-layer (string-append fullname ".jpg") (string-append shortname ".jpg"))
   )
 )

;;;;clean up by deleting the dupImage  
    (gimp-image-delete dupImage)					  
	
    
	(gimp-displays-flush)
	(gimp-image-undo-group-end image)
	(gimp-context-pop)
 )
) 



(script-fu-register "script-fu-resize-and-save-image"        		    
                    "Resize and Save Image"
                     "(Requires the Liqid Rescale Plug-in) Resizes and Saves Image to the same location as Original Image"
                    "Graechan"
                    "Graechan"
                    "2012"
                    "RGB*"
                    SF-IMAGE      "image"      0
                    SF-DRAWABLE   "drawable"   0
                    SF-ADJUSTMENT "Output width (pixels)" '(1000 1 5000 1 100 0 1)
                    SF-ADJUSTMENT "Output height(pixels)" '(1000 1 5000 1 100 0 1)
					SF-OPTION "Scaling method" '("Liquid Rescale" "Scale")
					SF-TOGGLE     "Save as PNG" FALSE
					SF-TOGGLE     "Save as JPG" TRUE
)

(script-fu-menu-register "script-fu-resize-and-save-image" "<Image>/Image/")

