Scaling images not working properly?

281 views Asked by At

I have a $this->_tempFile that holds the temp path of an uploaded image file.

Now I wish to scale an image, it do resizes it but it doesn't scale right. The output is a big black square the bottom at the height and half of the image (width). And i tried with other images, and all turns out being these dimensions: 293px × 453px , why i don't know?

Here's my function for the scaleImage();

scaleImage(900, 582);

public function scaleImage($width, $height){
    $rel_difference = array('width'=>0, 'height'=>0);
    if($width > 604) $rel_difference['width'] = ($width-604)/604;
    if($height > 453) $rel_difference['height'] = ($height-453)/453;

    asort($rel_difference);
    $tmpname = $this->_tempFile;
    $newwidth = $width/(1+end($rel_difference));
    $newheight = $height/(1+end($rel_difference)); 
    $newwidth = round($newwidth);
    $newheight = round($newheight);
    $jpeg_quality = 90;

    switch(exif_imagetype($tmpname)) {
        case IMAGETYPE_GIF:
            $img_r = imagecreatefromgif($tmpname);
            break;
        case IMAGETYPE_JPEG:
            $img_r = imagecreatefromjpeg($tmpname);
            break;
        case IMAGETYPE_PNG:
            $img_r = imagecreatefrompng($tmpname);
            break;
        default:
            echo json_encode(array('error' => 'Not an image!'));
            exit(0);
            break;
    }
    $dst_r = ImageCreateTrueColor( $newwidth, $newheight );

    imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);

    imagejpeg($dst_r,$tmpname,$jpeg_quality);

    }

What is wrong here?

1

There are 1 answers

1
Mike C On BEST ANSWER

I suspect you have accidentally flipped the height and width in the parameters you're passing. So do

scaleImage(582, 900);