Home  >  Article  >  Backend Development  >  An example of PHP image reduction function

An example of PHP image reduction function

WBOY
WBOYOriginal
2016-07-25 08:55:401187browse
  1. /**

  2. * Reduce image function
  3. * Delete the original image and keep the image after operation
  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. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn