How do I auto resize user's inputted images to a specific dimension in PHP?

前端 未结 5 1602
走了就别回头了
走了就别回头了 2021-01-14 16:17

When users input their images, their images are any size. I want to be able to resize all the images to a specific dimension. Is there a function that allows me to do that i

5条回答
  •  别那么骄傲
    2021-01-14 16:51

    The function you need is imagecopyresampled, that also interpolate pixels, (imagecopyresized does not);
    In my code I use a it in a function like this:

    function resizeAndSavePhoto($original, $destination, $dest_width, $dest_height){    
        $photo = createImage($original);
        $size =  getimagesize($original);
    
        $final_photo = imagecreatetruecolor($dest_width, $dest_height);
    
        imagecopyresampled($final_photo, $photo,0,0,0,0,$dest_width, $dest_height, $size[0], $size[1]);
        imagejpeg($final_photo, $destination, 100);
    }
    

    $orignal and $destination are filename paths

提交回复
热议问题