Force non-proportional image resize.

This will forcibly resize an image non-proportionally.

function scale_image($source, $target, $size, $quality)


       // max values for downsizing

       $width = $size;

       $height = $size;

       list($width_orig, $height_orig) = getimagesize($source);

       $width = $size;

       $height = $size;

       // Resample

       $image_p = @imagecreatetruecolor($width, $height);

       if ($image_p === false) return false;

       $image = @imagecreatefromjpeg($source);

       if ($image === false)


           $data = file_get_contents($source);

           $image = @imagecreatefromstring($data);

           if ($image === false) return false;


       if (!@imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width,
$height, $width_orig, $height_orig)) return false;

       $res = @imagejpeg($image_p, $target, $quality);



       return $res;



This resizes the image to {var}x{var}. Essentially, it will generate a square image. Useful for avatars.

$source is the image to be resized, $target is where you want it saved.


Add your comment