Mukesh Yadav
Mukesh Yadav

Reputation: 2396

Image getting pixelated while resizing

I'm trying to resize an large image, but the out come of the image is getting pixelated using following funciton

function resize($width, $height) {
        $this->width = $width;
        $this->height = $height;

        $this->image_reproportion();

        $new_image = imagecreatetruecolor($this->width, $this->height);

        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $this->width, $this->height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

    function image_reproportion() {

        if (!is_numeric($this->width) OR !is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
            return;

        if (!is_numeric($this->getWidth()) OR !is_numeric($this->getheight()) OR $this->getWidth() == 0 OR $this->getheight() == 0)
            return;

        $new_width = ceil($this->getWidth() * $this->height / $this->getheight());
        $new_height = ceil($this->width * $this->getheight() / $this->getWidth());

        $ratio = (($this->getheight() / $this->getWidth()) - ($this->height / $this->width));

        if ($this->master_dim != 'width' AND $this->master_dim != 'height') {
            $this->master_dim = ($ratio < 0) ? 'width' : 'height';
        }

        if (($this->width != $new_width) AND ($this->height != $new_height)) {
            if ($this->master_dim == 'height') {
                $this->width = $new_width;
            } else {
                $this->height = $new_height;
            }
        }
    }

To use the above code I'm calling resize('200','200');
Image path and file are proper. it is being resized but image gets pixelating.

Upvotes: 1

Views: 686

Answers (1)

Mukesh Yadav
Mukesh Yadav

Reputation: 2396

I installed Gmagick that did the magic :)

Upvotes: 2

Related Questions