help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Prepare images for website


From: Jean Louis
Subject: Prepare images for website
Date: Sun, 14 Feb 2021 11:51:07 +0100

(defvar *wrs-image-sizes* '(320 400 640 800 1536))
(defvar *wrs-public-html* "/home/data1/protected/public_html/")
(defvar *wrs-image-max-width* 1600)
(defvar *wrs-image-default-quality* 70)
(defvar *jpegtran* "/usr/bin/jpegtran")

(defun shell-escaped-file-name (s)
  (concat "\"" (replace-regexp-in-string "\"" "\\\\\"" s) "\""))

(defun rcd-mime-type (file)
  "Returns mime type of the file"
  (when (rcd-which-list '("file" "mimetype"))
    (let ((file-command (executable-find "file"))
          (mimetype (executable-find "mimetype")))
      ;; TODO:
      ;; There is much work to do here to work on various systems for
      ;; example there may not be same output on BSD and GNU systems.
      ;; Additional problem is file quoting.
      ;; file and mimetype may give different results
      (if file-command
          (string-trim (shell-command-to-string (format "%s -b --mime-type %s" 
file-command
                                                        
(shell-escaped-file-name file))))
        (if mimetype
            (string-trim (shell-command-to-string (format "%s -b %s" mimetype
                                                          
(shell-escaped-file-name file)))))))))

(defun slash-add (path)
  "Adds slash `/` quickly on the end of string"
  (let ((last (substring (reverse path) 0 1)))
    (if (string= last "/") path
      (concat path "/"))))

(defun image-mime-type-p (file)
  "Determines if mime type is image"
  (let ((mime-type (rcd-mime-type file)))
    (if (string-match "image" mime-type) file nil)))

(defun wrs-is-public-html-p ()
  "Verifies if we are in public HTML directory"
  (if (eql (string-match *wrs-public-html* (file-truename default-directory)) 0)
      t nil))

(defun rcd-normalize-files ()
  (shell-command "find . -type d -exec chmod 755 {} \\;")
  (shell-command "find . -type f -exec chmod 644 {} \\;"))

(defun wrs-prepare-images ()
  (interactive)
  (catch 'no-go
    (unless (wrs-is-public-html-p)
      (message "Cannot prepare images for Website Revision System outside of 
your public HTML directory")
      (throw 'no-go t))
    (let* ((async-shell-command-buffer 'new-buffer)
           (images (dired-get-marked-files))
           (images (mapcar 'image-mime-type-p images))
           (images (seq-remove 'null images))
           (command-main (format "mogrify -sampling-factor 4:2:0 -strip 
-interlace JPEG -colorspace RGB -resize %s -quality %s `?` && %s -progressive 
-outfile `?` `?` && " *wrs-image-max-width* *wrs-image-default-quality* 
*jpegtran*)))
      (dired-do-async-shell-command command-main nil images)
      (dolist (size *wrs-image-sizes*)
        (let* ((size-string (format "%s" size))
               (size-directory (concat (slash-add default-directory) 
size-string))
               (command (format "convert -sampling-factor 4:2:0 -strip 
-interlace JPEG -colorspace RGB -resize %s -quality %s `?` %s/$(basename `?`) 
&& %s -progressive -outfile %s/$(basename `?`) %s/$(basename `?`);" size 
*wrs-image-default-quality* size-directory *jpegtran* size-directory 
size-directory)))
          (make-directory size-directory t)
          (dired-do-async-shell-command command nil images))))
  (rcd-normalize-files)))

Now prepare your images in directory like:

/home/yourusername/public_html/www.example.com/images/img1.jpg
/home/yourusername/public_html/www.example.com/images/img2.jpg
/home/yourusername/public_html/www.example.com/images/img3.jpg

Mark images in Dired and run M-x wrs-prepare-images

Images will be resized to specified sizes while retaining one of
larger ones. They will pass the Google Page Speed and other online
tests for website speed. And you will get multiple various sizes.

Jean

P.S. This is because I don't like waiting for seconds to see
somebody's bike in the attic. ;-p



reply via email to

[Prev in Thread] Current Thread [Next in Thread]