Search code examples
phpimagescale

PHP upscale an image?


I am new to php and trying to learn about image upscaling. can you please show me how can I upscale an image to a certain size? For example, I would like to re-size the following image while keeping the aspect ratio. If this can be done, can you please show me with an example enter image description here


Solution

  • $source_image = imagecreatefromjpeg("osaka.jpg");
    $source_imagex = imagesx($source_image);
    $source_imagey = imagesy($source_image);
    $dest_imagex = 300;
    $dest_imagey = 200;
    $dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
    

    Poor quality, but fast:

    imagecopyresized($dest_image, $source_image, 0, 0, 0, 0, 
                $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
    

    Best quality, but slow:

    imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 
                $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);