当前位置:Gxlcms > PHP教程 > PHP图片缩小函数一例

PHP图片缩小函数一例

时间:2021-07-01 10:21:17 帮助过:3人阅读

  1. /**

  2. * 缩小图片函数
  3. * 删除了原始图片保留了操作后的图片
  4. * @param string $fileName
  5. * @return void
  6. */
  7. private function createSmallImg($fileName)
  8. {
  9. list($width,$height,$type,$attr) = getimagesize($fileName);
  10. $imgOld=imagecreatefromjpeg($fileName);
  11. $imgObj=imagecreatetruecolor($width-100,$height-100);
  12. if(function_exists('imagecopyresampled'))
  13. {
  14. imagecopyresampled($imgObj,$imgOld,0,0,0,0,$width-100,$height-100,imagesx($imgOld),imagesy($imgOld));
  15. }
  16. else
  17. {
  18. imagecopyresized($imgObj,$imgOld,0,0,0,0,$width-100,$height-100,imagesx($imgOld),imagesy($imgOld));
  19. }
  20. imagedestroy($imgOld);
  21. unlink($fileName);
  22. imagejpeg($imgObj,$fileName,100);
  23. chmod($fileName,0777);
  24. imagedestroy($imgObj);
  25. }

  26. //调用示例

  27. $filename = "/images/test.jpg";
  28. createSmallImg($filename);
  29. ?>

人气教程排行