/** * Method to resize the current image. * * @param mixed $width The width of the resized image in pixels or a percentage. * @param mixed $height The height of the resized image in pixels or a percentage. * @param boolean $createNew If true the current image will be cloned, resized and returned; else * the current image will be resized and returned. * @param integer $scaleMethod Which method to use for scaling * * @return Image * * @since 1.0 * @throws \LogicException */ public function resize($width, $height, $createNew = true, $scaleMethod = self::SCALE_INSIDE) { // Make sure the resource handle is valid. if (!$this->isLoaded()) { throw new \LogicException('No valid image was loaded.'); } // Sanitize width. $width = $this->sanitizeWidth($width, $height); // Sanitize height. $height = $this->sanitizeHeight($height, $width); // Prepare the dimensions for the resize operation. $dimensions = $this->prepareDimensions($width, $height, $scaleMethod); // Instantiate offset. $offset = new \stdClass(); $offset->x = $offset->y = 0; // Center image if needed and create the new truecolor image handle. if ($scaleMethod == self::SCALE_FIT) { // Get the offsets $offset->x = round(($width - $dimensions->width) / 2); $offset->y = round(($height - $dimensions->height) / 2); $handle = imagecreatetruecolor($width, $height); // Make image transparent, otherwise canvas outside initial image would default to black if (!$this->isTransparent()) { $transparency = imagecolorAllocateAlpha($this->handle, 0, 0, 0, 127); imagecolorTransparent($this->handle, $transparency); } } else { $handle = imagecreatetruecolor($dimensions->width, $dimensions->height); } // Allow transparency for the new image handle. imagealphablending($handle, false); imagesavealpha($handle, true); if ($this->isTransparent()) { // Get the transparent color values for the current image. $rgba = imageColorsForIndex($this->handle, imagecolortransparent($this->handle)); $color = imageColorAllocateAlpha($this->handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); // Set the transparent color values for the new image. imagecolortransparent($handle, $color); imagefill($handle, 0, 0, $color); } // Use resampling for better quality imagecopyresampled($handle, $this->handle, $offset->x, $offset->y, 0, 0, $dimensions->width, $dimensions->height, $this->getWidth(), $this->getHeight()); // If we are resizing to a new image, create a new JImage object. if ($createNew) { // @codeCoverageIgnoreStart $new = new Image($handle); return $new; // @codeCoverageIgnoreEnd } else { // Free the memory from the current handle $this->destroy(); $this->handle = $handle; return $this; } }
public function resize($width, $height, $createNew = true, $scaleMethod = self::SCALE_INSIDE) { if (!$this->isLoaded()) { throw new LogicException('No valid image was loaded.'); } $width = $this->sanitizeWidth($width, $height); $height = $this->sanitizeHeight($height, $width); $dimensions = $this->prepareDimensions($width, $height, $scaleMethod); $offset = new stdClass(); $offset->x = $offset->y = 0; if ($scaleMethod == self::SCALE_FIT) { $offset->x = round(($width - $dimensions->width) / 2); $offset->y = round(($height - $dimensions->height) / 2); $handle = imagecreatetruecolor($width, $height); if (!$this->isTransparent()) { $transparency = imagecolorAllocateAlpha($this->handle, 0, 0, 0, 127); imagecolorTransparent($this->handle, $transparency); } } else { $handle = imagecreatetruecolor($dimensions->width, $dimensions->height); } imagealphablending($handle, false); imagesavealpha($handle, true); if ($this->isTransparent()) { $rgba = imageColorsForIndex($this->handle, imagecolortransparent($this->handle)); $color = imageColorAllocateAlpha($this->handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); imagecolortransparent($handle, $color); imagefill($handle, 0, 0, $color); imagecopyresized($handle, $this->handle, $offset->x, $offset->y, 0, 0, $dimensions->width, $dimensions->height, $this->getWidth(), $this->getHeight()); } else { imagecopyresampled($handle, $this->handle, $offset->x, $offset->y, 0, 0, $dimensions->width, $dimensions->height, $this->getWidth(), $this->getHeight()); } if ($createNew) { $new = new JImage($handle); return $new; } else { $this->destroy(); $this->handle = $handle; return $this; } }
/** * Method to resize the current image. * * @param mixed $width The width of the resized image in pixels or a percentage. * @param mixed $height The height of the resized image in pixels or a percentage. * @param boolean $createNew If true the current image will be cloned, resized and returned; else * the current image will be resized and returned. * @param integer $scaleMethod Which method to use for scaling * * @return KunenaImage * * @since 11.3 * @throws LogicException */ public function resize($width, $height, $createNew = true, $scaleMethod = self::SCALE_INSIDE) { $config = KunenaFactory::getConfig(); switch ($config->avatarresizemethod) { case '0': $resizemethod = 'imagecopyresized'; break; case '1': $resizemethod = 'imagecopyresampled'; break; default: $resizemethod = 'self::imageCopyResampledBicubic'; break; } // Make sure the resource handle is valid. if (!$this->isLoaded()) { throw new LogicException('No valid image was loaded.'); } // Sanitize width. $width = $this->sanitizeWidth($width, $height); // Sanitize height. $height = $this->sanitizeHeight($height, $width); // Prepare the dimensions for the resize operation. $dimensions = $this->prepareDimensions($width, $height, $scaleMethod); // Instantiate offset. $offset = new stdClass(); $offset->x = $offset->y = 0; // Get truecolor handle $handle = imagecreatetruecolor($dimensions->width, $dimensions->height); // Center image if needed and create the new truecolor image handle. if ($scaleMethod == self::SCALE_FIT) { // Get the offsets $offset->x = round(($width - $dimensions->width) / 2); $offset->y = round(($height - $dimensions->height) / 2); // Make image transparent, otherwise cavas outside initial image would default to black if (!$this->isTransparent()) { $transparency = imagecolorAllocateAlpha($this->handle, 0, 0, 0, 127); imagecolorTransparent($this->handle, $transparency); } } $imgProperties = self::getImageFileProperties($this->getPath()); if ($imgProperties->mime == MIME_GIF) { $trnprt_indx = imagecolortransparent($this->handle); if ($trnprt_indx >= 0 && $trnprt_indx < imagecolorstotal($this->handle)) { $trnprt_color = imagecolorsforindex($this->handle, $trnprt_indx); $trnprt_indx = imagecolorallocate($handle, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($handle, 0, 0, $trnprt_indx); imagecolortransparent($handle, $trnprt_indx); } } elseif ($imgProperties->mime == MIME_PNG) { imagealphablending($handle, false); imagesavealpha($handle, true); if ($this->isTransparent()) { $transparent = imagecolorallocatealpha($this->handle, 255, 255, 255, 127); imagefilledrectangle($this->handle, 0, 0, $width, $height, $transparent); } } if ($this->isTransparent()) { $trnprt_indx = imagecolortransparent($this->handle); if ($trnprt_indx >= 0 && $trnprt_indx < imagecolorstotal($this->handle)) { // Get the transparent color values for the current image. $rgba = imageColorsForIndex($this->handle, imagecolortransparent($this->handle)); $color = imageColorAllocateAlpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); } else { $color = imageColorAllocateAlpha($handle, 255, 255, 255, 127); } // Set the transparent color values for the new image. imagecolortransparent($handle, $color); imagefill($handle, 0, 0, $color); imagecopyresized($handle, $this->handle, $offset->x, $offset->y, 0, 0, $dimensions->width, $dimensions->height, $this->getWidth(), $this->getHeight()); } else { call_user_func_array($resizemethod, array(&$handle, &$this->handle, $offset->x, $offset->y, 0, 0, $dimensions->width, $dimensions->height, $this->getWidth(), $this->getHeight())); } // If we are resizing to a new image, create a new KunenaImage object. if ($createNew) { // @codeCoverageIgnoreStart $new = new KunenaImage($handle); return $new; // @codeCoverageIgnoreEnd } else { // Free the memory from the current handle $this->destroy(); $this->handle = $handle; return $this; } }