Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.68 KB

how-to-scale-up-an-image.md

File metadata and controls

46 lines (34 loc) · 1.68 KB

How 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');
  • /var/www/examples/small.png - path to image to scale up
  • getimagesize - returns image size from given path
  • imagecreatefrompng - creates lib:GD image object from given PNG image
  • imagecreatetruecolor - creates true color lib: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

group: resize

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');