[geeklog-cvs] geeklog: Allow setting the JPEG quality (Feature request #0000720)

geeklog-cvs at lists.geeklog.net geeklog-cvs at lists.geeklog.net
Sat Nov 15 15:13:00 EST 2008


details:   http://project.geeklog.net/cgi-bin/hgweb.cgi/rev/4b8a2444f299
changeset: 6467:4b8a2444f299
user:      Dirk Haun <dirk at haun-online.de>
date:      Sat Nov 15 17:44:44 2008 +0100
description:
Allow setting the JPEG quality (Feature request #0000720)

diffstat:

1 file changed, 47 insertions(+), 5 deletions(-)
system/classes/upload.class.php |   52 +++++++++++++++++++++++++++++++++++----

diffs (104 lines):

diff -r 309c2727ccb5 -r 4b8a2444f299 system/classes/upload.class.php
--- a/system/classes/upload.class.php	Sat Nov 15 16:21:23 2008 +0100
+++ b/system/classes/upload.class.php	Sat Nov 15 17:44:44 2008 +0100
@@ -37,7 +37,7 @@
 * submitted via POST method.  Please read documentation as there are a number of
 * security related features that will come in handy for you.
 *
-* @author       Tony Bibbs <tony at tonybibbs.com>
+* @author       Tony Bibbs <tony AT tonybibbs DOT com>
 *
 */
 class upload
@@ -90,6 +90,10 @@
     * @access private
     */
     var $_maxFileSize = 1048576;          // Long, in bytes
+    /**
+    * @access private
+    */
+    var $_jpegQuality = 0;                // int (0-100)
     /**
     * @access private
     */
@@ -531,7 +535,11 @@
 
             if ($this->_imageLib == 'imagemagick') {
                 $newsize = $newwidth . 'x' . $newheight;
-                $cmd = $this->_pathToMogrify . ' -resize '. $newsize . ' "' . $this->_fileUploadDirectory . '/' . $this->_getDestinationName() . '" 2>&1';
+                $quality = '';
+                if ($this->_jpegQuality > 0) {
+                    $quality = sprintf(' -quality %d', $this->_jpegQuality);
+                }
+                $cmd = $this->_pathToMogrify . $quality . ' -resize '. $newsize . ' "' . $this->_fileUploadDirectory . '/' . $this->_getDestinationName() . '" 2>&1';
                 $this->_addDebugMsg('Attempting to resize with this command (imagemagick): ' . $cmd);
 
                 $filename = $this->_fileUploadDirectory . '/'
@@ -553,7 +561,11 @@
                     $cmd .= 'pngtopnm ' . $cmd_end . 'pnmtopng > ' . $tmpfile;
                 } else if (eregi ('\.(jpg|jpeg)', $filename)) {
                     $tmpfile = $this->_fileUploadDirectory . '/tmp.jpg';
-                    $cmd .= 'jpegtopnm ' . $cmd_end . 'pnmtojpeg > ' . $tmpfile;
+                    $quality = '';
+                    if ($this->_jpegQuality > 0) {
+                        $quality = sprintf(' -quality=%d', $this->_jpegQuality);
+                    }
+                    $cmd .= 'jpegtopnm ' . $cmd_end . 'pnmtojpeg' . $quality . ' > ' . $tmpfile;
                 }  else if (eregi ('\.gif', $filename)) {
                     $tmpfile = $this->_fileUploadDirectory . '/tmp.gif';
                     $cmd .= 'giftopnm ' . $cmd_end . 'ppmquant 256 | '
@@ -659,7 +671,11 @@
                 $image_dest = @ImageCreateTrueColor($newwidth, $newheight);
                 if (!$image_dest) {
                     $thumb = imagecreate ($newwidth, $newheight);
-                    imageJPEG ($thumb, $filename);
+                    if ($this->_jpegQuality > 0) {
+                        imagejpeg($thumb, $filename, $this->_jpegQuality);
+                    } else {
+                        imagejpeg($thumb, $filename);
+                    }
                     imagedestroy ($thumb);
                     $image_dest = @imagecreatefromjpeg ($filename);
                     unlink ($filename);
@@ -677,7 +693,13 @@
                     }
                 } elseif (($this->_currentFile['type'] == 'image/jpeg') OR
                           ($this->_currentFile['type'] == 'image/pjpeg')) {
-                    if (!imagejpeg ($image_dest, $filename)) {
+                    if ($this->_jpegQuality > 0) {
+                        $jpsuccess = imagejpeg($image_dest, $filename,
+                                               $this->_jpegQuality);
+                    } else {
+                        $jpsuccess = imagejpeg($image_dest, $filename);
+                    }
+                    if (!$jpsuccess) {
                         $this->_addError ('Could not create JPEG: '. $filename);
                         $this->printErrors ();
                         exit;
@@ -837,6 +859,26 @@
     function keepOriginalImage ($keepit)
     {
         $this->_keepOriginalImage = $keepit;
+
+        return true;
+    }
+
+    /**
+    * Set JPEG quality
+    *
+    * @param    int       $quality  JPEG quality (0-100)
+    * @return   boolean   true if we set values OK, otherwise false
+    * @note     The 'quality' is an arbitrary value used by the IJG library.
+    *           It is not a percent value! The default (and a good value) is 75.
+    *
+    */
+    function setJpegQuality($quality)
+    {
+        if (($quality < 0) || ($quality > 100)) {
+            return false;
+        }
+
+        $this->_jpegQuality = $quality;
 
         return true;
     }



More information about the geeklog-cvs mailing list