xref: /dokuwiki/vendor/splitbrain/slika/src/GdAdapter.php (revision 6cb05674d342c24351edf98467e681cbaabf9d0b)
192a8473aSAndreas Gohr<?php /** @noinspection PhpComposerExtensionStubsInspection */
292a8473aSAndreas Gohr
392a8473aSAndreas Gohr
492a8473aSAndreas Gohrnamespace splitbrain\slika;
592a8473aSAndreas Gohr
692a8473aSAndreas Gohr
792a8473aSAndreas Gohrclass GdAdapter extends Adapter
892a8473aSAndreas Gohr{
992a8473aSAndreas Gohr    /** @var resource libGD image */
1092a8473aSAndreas Gohr    protected $image;
1192a8473aSAndreas Gohr    /** @var int width of the current image */
1292a8473aSAndreas Gohr    protected $width = 0;
1392a8473aSAndreas Gohr    /** @var int height of the current image */
1492a8473aSAndreas Gohr    protected $height = 0;
1592a8473aSAndreas Gohr    /** @var string the extension of the file we're working with */
1692a8473aSAndreas Gohr    protected $extension;
1792a8473aSAndreas Gohr
1892a8473aSAndreas Gohr
1992a8473aSAndreas Gohr    /** @inheritDoc */
2092a8473aSAndreas Gohr    public function __construct($imagepath, $options = [])
2192a8473aSAndreas Gohr    {
2292a8473aSAndreas Gohr        parent::__construct($imagepath, $options);
2392a8473aSAndreas Gohr        $this->image = $this->loadImage($imagepath);
2492a8473aSAndreas Gohr    }
2592a8473aSAndreas Gohr
2692a8473aSAndreas Gohr    /**
2792a8473aSAndreas Gohr     * Clean up
2892a8473aSAndreas Gohr     */
2992a8473aSAndreas Gohr    public function __destruct()
3092a8473aSAndreas Gohr    {
3192a8473aSAndreas Gohr        if (is_resource($this->image)) {
3292a8473aSAndreas Gohr            imagedestroy($this->image);
3392a8473aSAndreas Gohr        }
3492a8473aSAndreas Gohr    }
3592a8473aSAndreas Gohr
3692a8473aSAndreas Gohr    /** @inheritDoc
3792a8473aSAndreas Gohr     * @throws Exception
3892a8473aSAndreas Gohr     * @link https://gist.github.com/EionRobb/8e0c76178522bc963c75caa6a77d3d37#file-imagecreatefromstring_autorotate-php-L15
3992a8473aSAndreas Gohr     */
4092a8473aSAndreas Gohr    public function autorotate()
4192a8473aSAndreas Gohr    {
4292a8473aSAndreas Gohr        if ($this->extension !== 'jpeg') {
4392a8473aSAndreas Gohr            return $this;
4492a8473aSAndreas Gohr        }
4592a8473aSAndreas Gohr
4692a8473aSAndreas Gohr        $orientation = 1;
4792a8473aSAndreas Gohr
4892a8473aSAndreas Gohr        if (function_exists('exif_read_data')) {
4992a8473aSAndreas Gohr            // use PHP's exif capablities
5092a8473aSAndreas Gohr            $exif = exif_read_data($this->imagepath);
5192a8473aSAndreas Gohr            if (!empty($exif['Orientation'])) {
5292a8473aSAndreas Gohr                $orientation = $exif['Orientation'];
5392a8473aSAndreas Gohr            }
5492a8473aSAndreas Gohr        } else {
5592a8473aSAndreas Gohr            // grep the exif info from the raw contents
5692a8473aSAndreas Gohr            // we read only the first 70k bytes
5792a8473aSAndreas Gohr            $data = file_get_contents($this->imagepath, false, null, 0, 70000);
5892a8473aSAndreas Gohr            if (preg_match('@\x12\x01\x03\x00\x01\x00\x00\x00(.)\x00\x00\x00@', $data, $matches)) {
5992a8473aSAndreas Gohr                // Little endian EXIF
6092a8473aSAndreas Gohr                $orientation = ord($matches[1]);
6192a8473aSAndreas Gohr            } else if (preg_match('@\x01\x12\x00\x03\x00\x00\x00\x01\x00(.)\x00\x00@', $data, $matches)) {
6292a8473aSAndreas Gohr                // Big endian EXIF
6392a8473aSAndreas Gohr                $orientation = ord($matches[1]);
6492a8473aSAndreas Gohr            }
6592a8473aSAndreas Gohr        }
6692a8473aSAndreas Gohr
6792a8473aSAndreas Gohr        return $this->rotate($orientation);
6892a8473aSAndreas Gohr    }
6992a8473aSAndreas Gohr
7092a8473aSAndreas Gohr    /**
7192a8473aSAndreas Gohr     * @inheritDoc
7292a8473aSAndreas Gohr     * @throws Exception
7392a8473aSAndreas Gohr     */
7492a8473aSAndreas Gohr    public function rotate($orientation)
7592a8473aSAndreas Gohr    {
7692a8473aSAndreas Gohr        $orientation = (int)$orientation;
7792a8473aSAndreas Gohr        if ($orientation < 0 || $orientation > 8) {
7892a8473aSAndreas Gohr            throw new Exception('Unknown rotation given');
7992a8473aSAndreas Gohr        }
8092a8473aSAndreas Gohr
8192a8473aSAndreas Gohr        if ($orientation <= 1) {
8292a8473aSAndreas Gohr            // no rotation wanted
8392a8473aSAndreas Gohr            return $this;
8492a8473aSAndreas Gohr        }
8592a8473aSAndreas Gohr
8692a8473aSAndreas Gohr        // fill color
8792a8473aSAndreas Gohr        $transparency = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
8892a8473aSAndreas Gohr
8992a8473aSAndreas Gohr        // rotate
9092a8473aSAndreas Gohr        if (in_array($orientation, [3, 4])) {
9192a8473aSAndreas Gohr            $image = imagerotate($this->image, 180, $transparency, 1);
9292a8473aSAndreas Gohr        }
9392a8473aSAndreas Gohr        if (in_array($orientation, [5, 6])) {
9492a8473aSAndreas Gohr            $image = imagerotate($this->image, -90, $transparency, 1);
9592a8473aSAndreas Gohr            list($this->width, $this->height) = [$this->height, $this->width];
9692a8473aSAndreas Gohr        } elseif (in_array($orientation, [7, 8])) {
9792a8473aSAndreas Gohr            $image = imagerotate($this->image, 90, $transparency, 1);
9892a8473aSAndreas Gohr            list($this->width, $this->height) = [$this->height, $this->width];
9992a8473aSAndreas Gohr        }
10092a8473aSAndreas Gohr        /** @var resource $image is now defined */
10192a8473aSAndreas Gohr
10292a8473aSAndreas Gohr        // additionally flip
10392a8473aSAndreas Gohr        if (in_array($orientation, [2, 5, 7, 4])) {
10492a8473aSAndreas Gohr            imageflip($image, IMG_FLIP_HORIZONTAL);
10592a8473aSAndreas Gohr        }
10692a8473aSAndreas Gohr
10792a8473aSAndreas Gohr        imagedestroy($this->image);
10892a8473aSAndreas Gohr        $this->image = $image;
10992a8473aSAndreas Gohr
11092a8473aSAndreas Gohr        //keep png alpha channel if possible
11192a8473aSAndreas Gohr        if ($this->extension == 'png' && function_exists('imagesavealpha')) {
11292a8473aSAndreas Gohr            imagealphablending($this->image, false);
11392a8473aSAndreas Gohr            imagesavealpha($this->image, true);
11492a8473aSAndreas Gohr        }
11592a8473aSAndreas Gohr
11692a8473aSAndreas Gohr        return $this;
11792a8473aSAndreas Gohr    }
11892a8473aSAndreas Gohr
11992a8473aSAndreas Gohr    /**
12092a8473aSAndreas Gohr     * @inheritDoc
12192a8473aSAndreas Gohr     * @throws Exception
12292a8473aSAndreas Gohr     */
12392a8473aSAndreas Gohr    public function resize($width, $height)
12492a8473aSAndreas Gohr    {
12592a8473aSAndreas Gohr        list($width, $height) = $this->boundingBox($width, $height);
12692a8473aSAndreas Gohr        $this->resizeOperation($width, $height);
12792a8473aSAndreas Gohr        return $this;
12892a8473aSAndreas Gohr    }
12992a8473aSAndreas Gohr
13092a8473aSAndreas Gohr    /**
13192a8473aSAndreas Gohr     * @inheritDoc
13292a8473aSAndreas Gohr     * @throws Exception
13392a8473aSAndreas Gohr     */
13492a8473aSAndreas Gohr    public function crop($width, $height)
13592a8473aSAndreas Gohr    {
13692a8473aSAndreas Gohr        list($this->width, $this->height, $offsetX, $offsetY) = $this->cropPosition($width, $height);
13792a8473aSAndreas Gohr        $this->resizeOperation($width, $height, $offsetX, $offsetY);
13892a8473aSAndreas Gohr        return $this;
13992a8473aSAndreas Gohr    }
14092a8473aSAndreas Gohr
14192a8473aSAndreas Gohr    /**
14292a8473aSAndreas Gohr     * @inheritDoc
14392a8473aSAndreas Gohr     * @throws Exception
14492a8473aSAndreas Gohr     */
14592a8473aSAndreas Gohr    public function save($path, $extension = '')
14692a8473aSAndreas Gohr    {
14792a8473aSAndreas Gohr        if ($extension === 'jpg') {
14892a8473aSAndreas Gohr            $extension = 'jpeg';
14992a8473aSAndreas Gohr        }
15092a8473aSAndreas Gohr        if ($extension === '') {
15192a8473aSAndreas Gohr            $extension = $this->extension;
15292a8473aSAndreas Gohr        }
15392a8473aSAndreas Gohr        $saver = 'image' . $extension;
15492a8473aSAndreas Gohr        if (!function_exists($saver)) {
15592a8473aSAndreas Gohr            throw new Exception('Can not save image format ' . $extension);
15692a8473aSAndreas Gohr        }
15792a8473aSAndreas Gohr
15892a8473aSAndreas Gohr        if ($extension == 'jpeg') {
15992a8473aSAndreas Gohr            imagejpeg($this->image, $path, $this->options['quality']);
16092a8473aSAndreas Gohr        } else {
16192a8473aSAndreas Gohr            $saver($this->image, $path);
16292a8473aSAndreas Gohr        }
16392a8473aSAndreas Gohr
16492a8473aSAndreas Gohr        imagedestroy($this->image);
16592a8473aSAndreas Gohr    }
16692a8473aSAndreas Gohr
16792a8473aSAndreas Gohr    /**
16892a8473aSAndreas Gohr     * Initialize libGD on the given image
16992a8473aSAndreas Gohr     *
17092a8473aSAndreas Gohr     * @param string $path
17192a8473aSAndreas Gohr     * @return resource
17292a8473aSAndreas Gohr     * @throws Exception
17392a8473aSAndreas Gohr     */
17492a8473aSAndreas Gohr    protected function loadImage($path)
17592a8473aSAndreas Gohr    {
17692a8473aSAndreas Gohr        // Figure out the file info
17792a8473aSAndreas Gohr        $info = getimagesize($path);
17892a8473aSAndreas Gohr        if ($info === false) {
17992a8473aSAndreas Gohr            throw new Exception('Failed to read image information');
18092a8473aSAndreas Gohr        }
18192a8473aSAndreas Gohr        $this->width = $info[0];
18292a8473aSAndreas Gohr        $this->height = $info[1];
18392a8473aSAndreas Gohr
18492a8473aSAndreas Gohr        // what type of image is it?
18592a8473aSAndreas Gohr        $this->extension = image_type_to_extension($info[2], false);
18692a8473aSAndreas Gohr        $creator = 'imagecreatefrom' . $this->extension;
18792a8473aSAndreas Gohr        if (!function_exists($creator)) {
18892a8473aSAndreas Gohr            throw new Exception('Can not work with image format ' . $this->extension);
18992a8473aSAndreas Gohr        }
19092a8473aSAndreas Gohr
19192a8473aSAndreas Gohr        // create the GD instance
19292a8473aSAndreas Gohr        $image = @$creator($path);
19392a8473aSAndreas Gohr
19492a8473aSAndreas Gohr        if ($image === false) {
19592a8473aSAndreas Gohr            throw new Exception('Failed to load image wiht libGD');
19692a8473aSAndreas Gohr        }
19792a8473aSAndreas Gohr
19892a8473aSAndreas Gohr        return $image;
19992a8473aSAndreas Gohr    }
20092a8473aSAndreas Gohr
20192a8473aSAndreas Gohr    /**
20292a8473aSAndreas Gohr     * Creates a new blank image to which we can copy
20392a8473aSAndreas Gohr     *
20492a8473aSAndreas Gohr     * Tries to set up alpha/transparency stuff correctly
20592a8473aSAndreas Gohr     *
20692a8473aSAndreas Gohr     * @param int $width
20792a8473aSAndreas Gohr     * @param int $height
20892a8473aSAndreas Gohr     * @return resource
20992a8473aSAndreas Gohr     * @throws Exception
21092a8473aSAndreas Gohr     */
21192a8473aSAndreas Gohr    protected function createImage($width, $height)
21292a8473aSAndreas Gohr    {
21392a8473aSAndreas Gohr        // create a canvas to copy to, use truecolor if possible (except for gif)
21492a8473aSAndreas Gohr        $canvas = false;
21592a8473aSAndreas Gohr        if (function_exists('imagecreatetruecolor') && $this->extension != 'gif') {
21692a8473aSAndreas Gohr            $canvas = @imagecreatetruecolor($width, $height);
21792a8473aSAndreas Gohr        }
21892a8473aSAndreas Gohr        if (!$canvas) {
21992a8473aSAndreas Gohr            $canvas = @imagecreate($width, $height);
22092a8473aSAndreas Gohr        }
22192a8473aSAndreas Gohr        if (!$canvas) {
22292a8473aSAndreas Gohr            throw new Exception('Failed to create new canvas');
22392a8473aSAndreas Gohr        }
22492a8473aSAndreas Gohr
22592a8473aSAndreas Gohr        //keep png alpha channel if possible
22692a8473aSAndreas Gohr        if ($this->extension == 'png' && function_exists('imagesavealpha')) {
22792a8473aSAndreas Gohr            imagealphablending($canvas, false);
22892a8473aSAndreas Gohr            imagesavealpha($canvas, true);
22992a8473aSAndreas Gohr        }
23092a8473aSAndreas Gohr
23192a8473aSAndreas Gohr        //keep gif transparent color if possible
23292a8473aSAndreas Gohr        if ($this->extension == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
23392a8473aSAndreas Gohr            if (function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
23492a8473aSAndreas Gohr                $transcolorindex = @imagecolortransparent($this->image);
23592a8473aSAndreas Gohr                if ($transcolorindex >= 0) { //transparent color exists
23692a8473aSAndreas Gohr                    $transcolor = @imagecolorsforindex($this->image, $transcolorindex);
23792a8473aSAndreas Gohr                    $transcolorindex = @imagecolorallocate(
23892a8473aSAndreas Gohr                        $canvas,
23992a8473aSAndreas Gohr                        $transcolor['red'],
24092a8473aSAndreas Gohr                        $transcolor['green'],
24192a8473aSAndreas Gohr                        $transcolor['blue']
24292a8473aSAndreas Gohr                    );
24392a8473aSAndreas Gohr                    @imagefill($canvas, 0, 0, $transcolorindex);
24492a8473aSAndreas Gohr                    @imagecolortransparent($canvas, $transcolorindex);
24592a8473aSAndreas Gohr                } else { //filling with white
24692a8473aSAndreas Gohr                    $whitecolorindex = @imagecolorallocate($canvas, 255, 255, 255);
24792a8473aSAndreas Gohr                    @imagefill($canvas, 0, 0, $whitecolorindex);
24892a8473aSAndreas Gohr                }
24992a8473aSAndreas Gohr            } else { //filling with white
25092a8473aSAndreas Gohr                $whitecolorindex = @imagecolorallocate($canvas, 255, 255, 255);
25192a8473aSAndreas Gohr                @imagefill($canvas, 0, 0, $whitecolorindex);
25292a8473aSAndreas Gohr            }
25392a8473aSAndreas Gohr        }
25492a8473aSAndreas Gohr
25592a8473aSAndreas Gohr        return $canvas;
25692a8473aSAndreas Gohr    }
25792a8473aSAndreas Gohr
25892a8473aSAndreas Gohr    /**
25992a8473aSAndreas Gohr     * Calculate new size
26092a8473aSAndreas Gohr     *
26192a8473aSAndreas Gohr     * If widht and height are given, the new size will be fit within this bounding box.
26292a8473aSAndreas Gohr     * If only one value is given the other is adjusted to match according to the aspect ratio
26392a8473aSAndreas Gohr     *
26492a8473aSAndreas Gohr     * @param int $width width of the bounding box
26592a8473aSAndreas Gohr     * @param int $height height of the bounding box
26692a8473aSAndreas Gohr     * @return array (width, height)
26792a8473aSAndreas Gohr     * @throws Exception
26892a8473aSAndreas Gohr     */
26992a8473aSAndreas Gohr    protected function boundingBox($width, $height)
27092a8473aSAndreas Gohr    {
271*6cb05674SAndreas Gohr        $width = $this->cleanDimension($width, $this->width);
272*6cb05674SAndreas Gohr        $height = $this->cleanDimension($height, $this->height);
273*6cb05674SAndreas Gohr
27492a8473aSAndreas Gohr        if ($width == 0 && $height == 0) {
27592a8473aSAndreas Gohr            throw new Exception('You can not resize to 0x0');
27692a8473aSAndreas Gohr        }
27792a8473aSAndreas Gohr
27892a8473aSAndreas Gohr        if (!$height) {
27992a8473aSAndreas Gohr            // adjust to match width
28092a8473aSAndreas Gohr            $height = round(($width * $this->height) / $this->width);
28192a8473aSAndreas Gohr        } else if (!$width) {
28292a8473aSAndreas Gohr            // adjust to match height
28392a8473aSAndreas Gohr            $width = round(($height * $this->width) / $this->height);
28492a8473aSAndreas Gohr        } else {
28592a8473aSAndreas Gohr            // fit into bounding box
28692a8473aSAndreas Gohr            $scale = min($width / $this->width, $height / $this->height);
28792a8473aSAndreas Gohr            $width = $this->width * $scale;
28892a8473aSAndreas Gohr            $height = $this->height * $scale;
28992a8473aSAndreas Gohr        }
29092a8473aSAndreas Gohr
29192a8473aSAndreas Gohr        return [$width, $height];
29292a8473aSAndreas Gohr    }
29392a8473aSAndreas Gohr
29492a8473aSAndreas Gohr    /**
295*6cb05674SAndreas Gohr     * Ensure the given Dimension is a proper pixel value
296*6cb05674SAndreas Gohr     *
297*6cb05674SAndreas Gohr     * When a percentage is given, the value is calculated based on the given original dimension
298*6cb05674SAndreas Gohr     *
299*6cb05674SAndreas Gohr     * @param int|string $dim New Dimension
300*6cb05674SAndreas Gohr     * @param int $orig Original dimension
301*6cb05674SAndreas Gohr     * @return int
302*6cb05674SAndreas Gohr     */
303*6cb05674SAndreas Gohr    protected function cleanDimension($dim, $orig)
304*6cb05674SAndreas Gohr    {
305*6cb05674SAndreas Gohr        if ($dim && substr($dim, -1) == '%') {
306*6cb05674SAndreas Gohr            $dim = round($orig * ((float)$dim / 100));
307*6cb05674SAndreas Gohr        } else {
308*6cb05674SAndreas Gohr            $dim = (int)$dim;
309*6cb05674SAndreas Gohr        }
310*6cb05674SAndreas Gohr
311*6cb05674SAndreas Gohr        return $dim;
312*6cb05674SAndreas Gohr    }
313*6cb05674SAndreas Gohr
314*6cb05674SAndreas Gohr    /**
31592a8473aSAndreas Gohr     * Calculates crop position
31692a8473aSAndreas Gohr     *
31792a8473aSAndreas Gohr     * Given the wanted final size, this calculates which exact area needs to be cut
31892a8473aSAndreas Gohr     * from the original image to be then resized to the wanted dimensions.
31992a8473aSAndreas Gohr     *
32092a8473aSAndreas Gohr     * @param int $width
32192a8473aSAndreas Gohr     * @param int $height
32292a8473aSAndreas Gohr     * @return array (cropWidth, cropHeight, offsetX, offsetY)
32392a8473aSAndreas Gohr     * @throws Exception
32492a8473aSAndreas Gohr     */
32592a8473aSAndreas Gohr    protected function cropPosition($width, $height)
32692a8473aSAndreas Gohr    {
32792a8473aSAndreas Gohr        if ($width == 0 && $height == 0) {
32892a8473aSAndreas Gohr            throw new Exception('You can not crop to 0x0');
32992a8473aSAndreas Gohr        }
33092a8473aSAndreas Gohr
33192a8473aSAndreas Gohr        if (!$height) {
33292a8473aSAndreas Gohr            $height = $width;
33392a8473aSAndreas Gohr        }
33492a8473aSAndreas Gohr
33592a8473aSAndreas Gohr        if (!$width) {
33692a8473aSAndreas Gohr            $width = $height;
33792a8473aSAndreas Gohr        }
33892a8473aSAndreas Gohr
33992a8473aSAndreas Gohr        // calculate ratios
34092a8473aSAndreas Gohr        $oldRatio = $this->width / $this->height;
34192a8473aSAndreas Gohr        $newRatio = $width / $height;
34292a8473aSAndreas Gohr
34392a8473aSAndreas Gohr        // calulate new size
34492a8473aSAndreas Gohr        if ($newRatio >= 1) {
34592a8473aSAndreas Gohr            if ($newRatio > $oldRatio) {
34692a8473aSAndreas Gohr                $cropWidth = $this->width;
34792a8473aSAndreas Gohr                $cropHeight = (int)($this->width / $newRatio);
34892a8473aSAndreas Gohr            } else {
34992a8473aSAndreas Gohr                $cropWidth = (int)($this->height * $newRatio);
35092a8473aSAndreas Gohr                $cropHeight = $this->height;
35192a8473aSAndreas Gohr            }
35292a8473aSAndreas Gohr        } else {
35392a8473aSAndreas Gohr            if ($newRatio < $oldRatio) {
35492a8473aSAndreas Gohr                $cropWidth = (int)($this->height * $newRatio);
35592a8473aSAndreas Gohr                $cropHeight = $this->height;
35692a8473aSAndreas Gohr            } else {
35792a8473aSAndreas Gohr                $cropWidth = $this->width;
35892a8473aSAndreas Gohr                $cropHeight = (int)($this->width / $newRatio);
35992a8473aSAndreas Gohr            }
36092a8473aSAndreas Gohr        }
36192a8473aSAndreas Gohr
36292a8473aSAndreas Gohr        // calculate crop offset
36392a8473aSAndreas Gohr        $offsetX = (int)(($this->width - $cropWidth) / 2);
36492a8473aSAndreas Gohr        $offsetY = (int)(($this->height - $cropHeight) / 2);
36592a8473aSAndreas Gohr
36692a8473aSAndreas Gohr        return [$cropWidth, $cropHeight, $offsetX, $offsetY];
36792a8473aSAndreas Gohr    }
36892a8473aSAndreas Gohr
36992a8473aSAndreas Gohr    /**
37092a8473aSAndreas Gohr     * resize or crop images using PHP's libGD support
37192a8473aSAndreas Gohr     *
37292a8473aSAndreas Gohr     * @param int $toWidth desired width
37392a8473aSAndreas Gohr     * @param int $toHeight desired height
37492a8473aSAndreas Gohr     * @param int $offsetX offset of crop centre
37592a8473aSAndreas Gohr     * @param int $offsetY offset of crop centre
37692a8473aSAndreas Gohr     * @throws Exception
37792a8473aSAndreas Gohr     */
37892a8473aSAndreas Gohr    protected function resizeOperation($toWidth, $toHeight, $offsetX = 0, $offsetY = 0)
37992a8473aSAndreas Gohr    {
38092a8473aSAndreas Gohr        $newimg = $this->createImage($toWidth, $toHeight);
38192a8473aSAndreas Gohr
38292a8473aSAndreas Gohr        //try resampling first, fall back to resizing
38392a8473aSAndreas Gohr        if (
38492a8473aSAndreas Gohr            !function_exists('imagecopyresampled') ||
38592a8473aSAndreas Gohr            !@imagecopyresampled(
38692a8473aSAndreas Gohr                $newimg,
38792a8473aSAndreas Gohr                $this->image,
38892a8473aSAndreas Gohr                0,
38992a8473aSAndreas Gohr                0,
39092a8473aSAndreas Gohr                $offsetX,
39192a8473aSAndreas Gohr                $offsetY,
39292a8473aSAndreas Gohr                $toWidth,
39392a8473aSAndreas Gohr                $toHeight,
39492a8473aSAndreas Gohr                $this->width,
39592a8473aSAndreas Gohr                $this->height
39692a8473aSAndreas Gohr            )
39792a8473aSAndreas Gohr        ) {
39892a8473aSAndreas Gohr            imagecopyresized(
39992a8473aSAndreas Gohr                $newimg,
40092a8473aSAndreas Gohr                $this->image,
40192a8473aSAndreas Gohr                0,
40292a8473aSAndreas Gohr                0,
40392a8473aSAndreas Gohr                $offsetX,
40492a8473aSAndreas Gohr                $offsetY,
40592a8473aSAndreas Gohr                $toWidth,
40692a8473aSAndreas Gohr                $toHeight,
40792a8473aSAndreas Gohr                $this->width,
40892a8473aSAndreas Gohr                $this->height
40992a8473aSAndreas Gohr            );
41092a8473aSAndreas Gohr        }
41192a8473aSAndreas Gohr
41292a8473aSAndreas Gohr        // destroy original GD image ressource and replace with new one
41392a8473aSAndreas Gohr        imagedestroy($this->image);
41492a8473aSAndreas Gohr        $this->image = $newimg;
41592a8473aSAndreas Gohr        $this->width = $toWidth;
41692a8473aSAndreas Gohr        $this->height = $toHeight;
41792a8473aSAndreas Gohr    }
41892a8473aSAndreas Gohr
41992a8473aSAndreas Gohr}
420