9951 explained code solutions for 126 technologies


php-gdHow to scale up an image


To scale images up, we use the same approach as in resizing images.

<?php

$file = '/var/www/examples/small.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

$imf = imagecreatetruecolor($size[0]*10,$size[1]*10);
imagealphablending($imf, false);
imagesavealpha($imf, true);
imagecopyresampled($imf, $im, 0,0,0,0, $size[0]*10,$size[1]*10,$size[0],$size[1]);

imagePng($imf, '/tmp/image.png');ctrl + c
/var/www/examples/small.png

path to image to scale up

getimagesize

returns image size from given path

imagecreatefrompng

creates GD image object from given PNG image

imagecreatetruecolor

creates true color GD image object with specified width & height

imagealphablending

we're disabling alpha blending to prevent transparent background being converted to black color

imagesavealpha

we're enabling alpha channel (controls transparency) for resulting image

imagecopyresampled

resizes source image and writes result to destination image

imagePng

saves image in PNG format to the given path


How to scale up an image, php gd

Usage example

<?php

$file = '/var/www/examples/small.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

$imf = imagecreatetruecolor($size[0]*10,$size[1]*10);
imagealphablending($imf, false);
imagesavealpha($imf, true);
imagecopyresampled($imf, $im, 0,0,0,0, $size[0]*10,$size[1]*10,$size[0],$size[1]);

imagePng($imf, '/tmp/image.png');