Resize image in PHP

前端 未结 13 1636
长情又很酷
长情又很酷 2020-11-22 04:18

I\'m wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I\'m a relative PHP novice

13条回答
  •  一向
    一向 (楼主)
    2020-11-22 04:45

    private function getTempImage($url, $tempName){
      $tempPath = 'tempFilePath' . $tempName . '.png';
      $source_image = imagecreatefrompng($url); // check type depending on your necessities.
      $source_imagex = imagesx($source_image);
      $source_imagey = imagesy($source_image);
      $dest_imagex = 861; // My default value
      $dest_imagey = 96;  // My default value
    
      $dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
    
      imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
    
      imagejpeg($dest_image, $tempPath, 100);
    
      return $tempPath;
    

    }

    This is an adapted solution based on this great explanation. This guy made a step by step explanation. Hope all enjoy it.

提交回复
热议问题