JBZoo\Image\Image::resize PHP Метод

resize() публичный Метод

Resize an image to the specified dimensions
public resize ( integer $width, integer $height )
$width integer
$height integer
    public function resize($width, $height)
    {
        $width = VarFilter::int($width);
        $height = VarFilter::int($height);
        // Generate new GD image
        $newImage = imagecreatetruecolor($width, $height);
        if ($this->isGif()) {
            // Preserve transparency in GIFs
            $transIndex = imagecolortransparent($this->_image);
            $palletsize = imagecolorstotal($this->_image);
            if ($transIndex >= 0 && $transIndex < $palletsize) {
                $trColor = imagecolorsforindex($this->_image, $transIndex);
                $red = VarFilter::int($trColor['red']);
                $green = VarFilter::int($trColor['green']);
                $blue = VarFilter::int($trColor['blue']);
                $transIndex = imagecolorallocate($newImage, $red, $green, $blue);
                imagefill($newImage, 0, 0, $transIndex);
                imagecolortransparent($newImage, $transIndex);
            }
        } else {
            // Preserve transparency in PNG
            Helper::addAlpha($newImage, false);
        }
        // Resize
        imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
        // Update meta data
        $this->_replaceImage($newImage);
        $this->_width = $width;
        $this->_height = $height;
        return $this;
    }

Usage Example

Пример #1
0
 public function testResizeTransparent()
 {
     $excepted = Helper::getExpected(__FUNCTION__ . '.gif');
     $actual = Helper::getActual(__FUNCTION__ . '.gif');
     $original = Helper::getOrig('1x1.gif');
     $img = new Image($original);
     $img->resize(50, 50)->saveAs($actual);
     Helper::isFileEq($actual, $excepted);
 }