]> git.mxchange.org Git - friendica.git/commitdiff
Add new common Image::scale() method
authorHypolite Petovan <mrpetovan@gmail.com>
Sun, 17 Dec 2017 20:35:07 +0000 (15:35 -0500)
committerHypolite Petovan <mrpetovan@gmail.com>
Sun, 17 Dec 2017 20:35:07 +0000 (15:35 -0500)
- Fix undefined variable $type

src/Object/Image.php

index f979e4943863aa0d4827b55ab27b3fb7e9ca36db..7cbc2432d995ecf2f4352b4eb80bcdc3c6d7349d 100644 (file)
@@ -142,7 +142,7 @@ class Image
                         * Setup the image to the format it will be saved to
                         */
                        $map = self::getFormatsMap();
-                       $format = $map[$type];
+                       $format = $map[$this->type];
                        $this->image->setFormat($format);
 
                        // Always coalesce, if it is not a multi-frame image it won't hurt anyway
@@ -338,42 +338,7 @@ class Image
                        }
                }
 
-
-               if ($this->isImagick()) {
-                       /*
-                        * If it is not animated, there will be only one iteration here,
-                        * so don't bother checking
-                        */
-                       // Don't forget to go back to the first frame
-                       $this->image->setFirstIterator();
-                       do {
-                               // FIXME - implement horizantal bias for scaling as in followin GD functions
-                               // to allow very tall images to be constrained only horizontally.
-
-                               $this->image->scaleImage($dest_width, $dest_height);
-                       } while ($this->image->nextImage());
-
-                       // These may not be necessary any more
-                       $this->width  = $this->image->getImageWidth();
-                       $this->height = $this->image->getImageHeight();
-
-                       return;
-               }
-
-
-               $dest = imagecreatetruecolor($dest_width, $dest_height);
-               imagealphablending($dest, false);
-               imagesavealpha($dest, true);
-               if ($this->type=='image/png') {
-                       imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
-               }
-               imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
-               if ($this->image) {
-                       imagedestroy($this->image);
-               }
-               $this->image = $dest;
-               $this->width  = imagesx($this->image);
-               $this->height = imagesy($this->image);
+               return $this->scale($dest_width, $dest_height);
        }
 
        /**
@@ -562,23 +527,7 @@ class Image
                        }
                }
 
-               if ($this->isImagick()) {
-                       return $this->scaleDown($dest_width, $dest_height);
-               }
-
-               $dest = imagecreatetruecolor($dest_width, $dest_height);
-               imagealphablending($dest, false);
-               imagesavealpha($dest, true);
-               if ($this->type=='image/png') {
-                       imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
-               }
-               imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
-               if ($this->image) {
-                       imagedestroy($this->image);
-               }
-               $this->image = $dest;
-               $this->width  = imagesx($this->image);
-               $this->height = imagesy($this->image);
+               return $this->scale($dest_width, $dest_height);
        }
 
        /**
@@ -591,27 +540,59 @@ class Image
                        return false;
                }
 
+               return $this->scale($dim, $dim);
+       }
+
+       /**
+        * @brief Scale image to target dimensions
+        *
+        * @param int $dest_width
+        * @param int $dest_height
+        * @return boolean
+        */
+       private function scale($dest_width, $dest_height)
+       {
+               if (!$this->isValid()) {
+                       return false;
+               }
+
                if ($this->isImagick()) {
+                       /*
+                        * If it is not animated, there will be only one iteration here,
+                        * so don't bother checking
+                        */
+                       // Don't forget to go back to the first frame
                        $this->image->setFirstIterator();
                        do {
-                               $this->image->scaleImage($dim, $dim);
+                               // FIXME - implement horizontal bias for scaling as in following GD functions
+                               // to allow very tall images to be constrained only horizontally.
+                               $this->image->scaleImage($dest_width, $dest_height);
                        } while ($this->image->nextImage());
-                       return;
-               }
 
-               $dest = imagecreatetruecolor($dim, $dim);
-               imagealphablending($dest, false);
-               imagesavealpha($dest, true);
-               if ($this->type=='image/png') {
-                       imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
-               }
-               imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
-               if ($this->image) {
-                       imagedestroy($this->image);
+                       // These may not be necessary anymore
+                       $this->width  = $this->image->getImageWidth();
+                       $this->height = $this->image->getImageHeight();
+               } else {
+                       $dest = imagecreatetruecolor($dest_width, $dest_height);
+                       imagealphablending($dest, false);
+                       imagesavealpha($dest, true);
+
+                       if ($this->type=='image/png') {
+                               imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
+                       }
+
+                       imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $this->width, $this->height);
+
+                       if ($this->image) {
+                               imagedestroy($this->image);
+                       }
+
+                       $this->image = $dest;
+                       $this->width  = imagesx($this->image);
+                       $this->height = imagesy($this->image);
                }
-               $this->image = $dest;
-               $this->width  = imagesx($this->image);
-               $this->height = imagesy($this->image);
+
+               return true;
        }
 
        /**