An instance of image scaling in PHP

  • 2020-05-30 19:43:35
  • OfStack


<?php 
     // Proportional scaling of images  

     // because PHP Only resources can be manipulated, so copy the image that needs to be scaled and create it as a new resource  
     $src=imagecreatefromjpeg('a.jpg'); 

     // Gets the width and height of the source image  
     $size_src=getimagesize('a.jpg'); 
     $w=$size_src['0']; 
     $h=$size_src['1']; 

     // Specifies the maximum width (possibly height) that will be zoomed out  
     $max=300; 

     // According to the maximum value 300 And work out the other 1 The length of the edges, the width and height of the scaled image  
     if($w > $h){ 
         $w=$max; 
         $h=$h*($max/$size_src['0']); 
     }else{ 
         $h=$max; 
         $w=$w*($max/$size_src['1']); 
     } 

       
     // The statement 1 a $w Wide, $h High true-color picture resources  
     $image=imagecreatetruecolor($w, $h); 

       
     // Key functions, parameters (target resource, source, start coordinates of target resource x,y,  The start coordinates of the source resource x,y, The width and height of the target resource w,h, The width and height of the source resource w,h )  
     imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']); 

     // Tells the browser to parse it as a picture  
     header('content-type:image/png'); 
     imagepng($image); 

     // Destruction of resources  
     imagedestroy($image); 

 ?> 


Related articles: