]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
improved the GD resizing code - writing files rather than returning jpegs
authorCash Costello <cash.costello@gmail.com>
Sat, 20 Jun 2009 19:02:06 +0000 (19:02 +0000)
committerCash Costello <cash.costello@gmail.com>
Sat, 20 Jun 2009 19:02:06 +0000 (19:02 +0000)
lib/resize.php
system.php

index ebbf587d70e327ff89f3b3588e50a76de0102ba7..9db81f318b8067b0dddb11a1586fb106e4b93b44 100644 (file)
        function tp_create_gd_thumbnails($file, $prefix, $filestorename)\r
        {\r
                global $CONFIG;\r
-               \r
-               $mime = $file->getMimeType();\r
-               \r
+\r
                $image_sizes = get_plugin_setting('image_sizes', 'tidypics');\r
                if (!$image_sizes) {\r
                        register_error(elgg_echo('tidypics:nosettings'));\r
                        return false;\r
                }\r
                $image_sizes = unserialize($image_sizes);\r
-               \r
-               // Generate thumbnails\r
-               $thumbnail = tp_gd_resize(      $file->getFilenameOnFilestore(),\r
+\r
+               $thumb = new ElggFile();\r
+\r
+               // tiny thumbail\r
+               $thumb->setFilename($prefix."thumb".$filestorename);\r
+               $thumbname = $thumb->getFilenameOnFilestore();\r
+               $rtn_code = tp_gd_resize(       $file->getFilenameOnFilestore(),\r
+                                                                       $thumbname,\r
                                                                        $image_sizes['thumb_image_width'],\r
                                                                        $image_sizes['thumb_image_height'], \r
-                                                                       true); \r
+                                                                       true);\r
+               if (!$rtn_code)\r
+                       return false;\r
+               $file->thumbnail = $prefix."thumb".$filestorename;\r
 \r
-               if ($thumbnail) {\r
-                       $thumb = new ElggFile();\r
-                       $thumb->setMimeType($mime);\r
-                       $thumb->setFilename($prefix."thumb".$filestorename);\r
-                       $thumb->open("write");\r
-                       if ($thumb->write($thumbnail)) {\r
-                               $file->thumbnail = $prefix."thumb".$filestorename;\r
-                       } else {\r
-                               $thumb->delete();\r
-                       }\r
-                       $thumb->close();\r
-                       unset($thumb);\r
-               }\r
-               unset($thumbnail);\r
-               \r
-               $thumbsmall = tp_gd_resize(     $file->getFilenameOnFilestore(),\r
+\r
+               // album thumbnail\r
+               $thumb->setFilename($prefix."smallthumb".$filestorename);\r
+               $thumbname = $thumb->getFilenameOnFilestore();\r
+               $rtn_code = tp_gd_resize(       $file->getFilenameOnFilestore(),\r
+                                                                       $thumbname,\r
                                                                        $image_sizes['small_image_width'],\r
                                                                        $image_sizes['small_image_height'], \r
                                                                        true); \r
+               if (!$rtn_code)\r
+                       return false;\r
+               $file->smallthumb = $prefix."smallthumb".$filestorename;\r
 \r
-               \r
-               if ($thumbsmall) {\r
-                       $thumb = new ElggFile();\r
-                       $thumb->setMimeType($mime);\r
-                       $thumb->setFilename($prefix."smallthumb".$filestorename);\r
-                       $thumb->open("write");\r
-                       if ($thumb->write($thumbsmall)) {\r
-                               $file->smallthumb = $prefix."smallthumb".$filestorename;\r
-                       } else {\r
-                               $thumb->delete();\r
-                       }\r
-                       $thumb->close();\r
-                       unset($thumb);\r
-               }\r
-               unset($thumbsmall);\r
 \r
-               $thumblarge = tp_gd_resize(     $file->getFilenameOnFilestore(),\r
+               // main image\r
+               $thumb->setFilename($prefix."largethumb".$filestorename);\r
+               $thumbname = $thumb->getFilenameOnFilestore();\r
+               $rtn_code = tp_gd_resize(       $file->getFilenameOnFilestore(),\r
+                                                                       $thumbname,\r
                                                                        $image_sizes['large_image_width'],\r
                                                                        $image_sizes['large_image_height'], \r
                                                                        false); \r
-               \r
-               if ($thumblarge) {\r
-                       $thumb = new ElggFile();\r
-                       $thumb->setMimeType($mime);\r
-                       $thumb->setFilename($prefix."largethumb".$filestorename);\r
-                       $thumb->open("write");\r
-                       if ($thumb->write($thumblarge)) {\r
-                               $file->largethumb = $prefix."largethumb".$filestorename;\r
-                       } else {\r
-                               $thumb->delete();\r
-                       }\r
-                       $thumb->close();\r
-                       unset($thumb);\r
-               }\r
-               unset($thumblarge);\r
+               if (!$rtn_code)\r
+                       return false;\r
+               $file->largethumb = $prefix."largethumb".$filestorename;\r
+\r
+               unset($thumb);\r
 \r
                return true;\r
        }\r
 \r
        /**\r
-        * Gets the jpeg contents of the resized version of an already uploaded image - original from Elgg filestore.php \r
-        * (Returns false if the uploaded file was not an image)\r
+        * Writes resized version of an already uploaded image - original from Elgg filestore.php \r
+        * Saves it in the same format as uploaded\r
         *\r
-        * @param string $input_name The name of the file input field on the submission form\r
+        * @param string $input_name The name of the file on the disk\r
+        * @param string $output_name The name of the file to be written\r
         * @param int $maxwidth The maximum width of the resized image\r
         * @param int $maxheight The maximum height of the resized image\r
         * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.\r
-        * @return false|mixed The contents of the resized image, or false on failure\r
+        * @return bool true on success or false on failure\r
         */\r
-       function tp_gd_resize($input_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {\r
+       function tp_gd_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {\r
                \r
                // Get the size information from the image\r
-               if ($imgsizearray = getimagesize($input_name)) {\r
+               $imgsizearray = getimagesize($input_name);\r
+               if (!imgsizearray)\r
+                       return false;\r
                \r
-                       // Get width and height\r
-                       $width = $imgsizearray[0];\r
-                       $height = $imgsizearray[1];\r
-                       $newwidth = $width;\r
-                       $newheight = $height;\r
-                       \r
-                       // Square the image dimensions if we're wanting a square image\r
-                       if ($square) {\r
-                               if ($width < $height) {\r
-                                       $height = $width;\r
-                               } else {\r
-                                       $width = $height;\r
-                               }\r
-                               \r
-                               $newwidth = $width;\r
-                               $newheight = $height;\r
-                               \r
-                       }\r
-                       \r
-                       if ($width > $maxwidth) {\r
-                               $newheight = floor($height * ($maxwidth / $width));\r
-                               $newwidth = $maxwidth;\r
-                       }\r
-                       if ($newheight > $maxheight) {\r
-                               $newwidth = floor($newwidth * ($maxheight / $newheight));\r
-                               $newheight = $maxheight; \r
+               // Get width and height\r
+               $width = $imgsizearray[0];\r
+               $height = $imgsizearray[1];\r
+               $newwidth = $width;\r
+               $newheight = $height;\r
+               \r
+               // Square the image dimensions if we're wanting a square image\r
+               if ($square) {\r
+                       if ($width < $height) {\r
+                               $height = $width;\r
+                       } else {\r
+                               $width = $height;\r
                        }\r
                        \r
-                       $accepted_formats = array(\r
-                                                                                       'image/jpeg' => 'jpeg',\r
-                                                                                       'image/png' => 'png',\r
-                                                                                       'image/gif' => 'gif'\r
-                                                                       );\r
+                       $newwidth = $width;\r
+                       $newheight = $height;\r
                        \r
-                       // If it's a file we can manipulate ...\r
-                       if (array_key_exists($imgsizearray['mime'],$accepted_formats)) {\r
+               }\r
+               \r
+               if ($width > $maxwidth) {\r
+                       $newheight = floor($height * ($maxwidth / $width));\r
+                       $newwidth = $maxwidth;\r
+               }\r
+               if ($newheight > $maxheight) {\r
+                       $newwidth = floor($newwidth * ($maxheight / $newheight));\r
+                       $newheight = $maxheight; \r
+               }\r
+               \r
+               $accepted_formats = array(\r
+                                                                               'image/jpeg' => 'jpeg',\r
+                                                                               'image/pjpeg' => 'jpeg',\r
+                                                                               'image/png' => 'png',\r
+                                                                               'image/x-png' => 'png',\r
+                                                                               'image/gif' => 'gif'\r
+                                                               );\r
 \r
-                               $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];\r
-                               $newimage = imagecreatetruecolor($newwidth,$newheight);\r
-                               \r
-                               if (is_callable($function) && $oldimage = $function($input_name)) {\r
-                               \r
-                                       // Crop the image if we need a square\r
-                                       if ($square) {\r
-                                               if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {\r
-                                                       $widthoffset = floor(($imgsizearray[0] - $width) / 2);\r
-                                                       $heightoffset = floor(($imgsizearray[1] - $height) / 2);\r
-                                               } else {\r
-                                                       $widthoffset = $x1;\r
-                                                       $heightoffset = $y1;\r
-                                                       $width = ($x2 - $x1);\r
-                                                       $height = $width;\r
-                                               }\r
-                                       } else {\r
-                                               if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {\r
-                                                       $widthoffset = 0;\r
-                                                       $heightoffset = 0;\r
-                                               } else {\r
-                                                       $widthoffset = $x1;\r
-                                                       $heightoffset = $y1;\r
-                                                       $width = ($x2 - $x1);\r
-                                                       $height = ($y2 - $y1);\r
-                                               }\r
-                                       }\r
-                                       \r
-                                       if ($square) {\r
-                                               $newheight = $maxheight;\r
-                                               $newwidth = $maxwidth;\r
-                                       }\r
-                                       \r
-                                       imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height);\r
-                                       \r
-                                       ob_start();\r
-                                       imagejpeg($newimage, null, 90);\r
-                                       $jpeg = ob_get_clean();\r
-                                       return $jpeg;\r
-                                       \r
-                               }\r
-                               \r
+               // make sure the function is available\r
+               $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];\r
+               if (!is_callable($function))\r
+                       return false;\r
+               \r
+               \r
+               // load old image\r
+               $oldimage = $function($input_name);\r
+               if (!$oldimage)\r
+                       return false;\r
+               \r
+               // allocate the new image\r
+               $newimage = imagecreatetruecolor($newwidth, $newheight);\r
+               if (!$newimage)\r
+                       return false;\r
+               \r
+               // Crop the image if we need a square\r
+               if ($square) {\r
+                       if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {\r
+                               $widthoffset = floor(($imgsizearray[0] - $width) / 2);\r
+                               $heightoffset = floor(($imgsizearray[1] - $height) / 2);\r
+                       } else {\r
+                               $widthoffset = $x1;\r
+                               $heightoffset = $y1;\r
+                               $width = ($x2 - $x1);\r
+                               $height = $width;\r
+                       }\r
+               } else {\r
+                       if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {\r
+                               $widthoffset = 0;\r
+                               $heightoffset = 0;\r
+                       } else {\r
+                               $widthoffset = $x1;\r
+                               $heightoffset = $y1;\r
+                               $width = ($x2 - $x1);\r
+                               $height = ($y2 - $y1);\r
                        }\r
-                       \r
                }\r
-                       \r
-               return false;\r
+               \r
+               if ($square) {\r
+                       $newheight = $maxheight;\r
+                       $newwidth = $maxwidth;\r
+               }\r
+               \r
+               $rtn_code = imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height);\r
+               if (!rtn_code)\r
+                       return $rtn_code;\r
+               \r
+               switch ($imgsizearray['mime']) {\r
+                       case 'image/jpeg':\r
+                       case 'image/pjpeg':\r
+                               $rtn_code = imagejpeg($newimage, $output_name, 85); \r
+                               break;\r
+                       case 'image/png':\r
+                       case 'image/x-png':\r
+                               $rtn_code = imagepng($newimage, $output_name);\r
+                               break;\r
+                       case 'image/gif':\r
+                               $rtn_code = imagegif($newimage, $output_name);\r
+                               break;\r
+               }\r
+               \r
+               return $rtn_code;\r
        }\r
 \r
 \r
        /**\r
         * Resize using PHP ImageMagick Library\r
         *\r
-        * Gets the jpeg contents of the resized version of an already uploaded image\r
-        * (Returns false if the uploaded file was not an image)\r
+        * Writes resized version of an already uploaded image\r
+        * \r
         *\r
         * @param string $input_name The name of the file input field on the submission form\r
         * @param string $output_name The name of the file to be written\r
         * @param int $maxwidth The maximum width of the resized image\r
         * @param int $maxheight The maximum height of the resized image\r
         * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.\r
-        * @return false|mixed The contents of the resized image, or false on failure\r
+        * @return bool true on success\r
         */\r
        function tp_imagick_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {\r
                \r
index ef781b92a5a95657dac95c58ba806aafa1c2af40..833722db8a76deffe9517b8da82938f40cacc877 100644 (file)
@@ -8,7 +8,9 @@
  *
  ********************************************************************/   
 
-       require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
+       include_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
+
+       global $CONFIG;
 
        admin_gatekeeper();