PHP实现图片不变型裁剪及图片按比例裁剪的方法

这篇文章主要介绍了PHP实现图片不变型裁剪及图片按比例裁剪的方法,涉及PHP裁剪缩略图的常用技巧,需要的朋友可以参考下。

本文实例讲述了PHP实现图片不变型裁剪及图片按比例裁剪的方法,分享给大家供大家参考,具体如下:

图片不变型裁剪

  1. <?php
  2. /**
  3. * imageCropper
  4. * @param string $source_path
  5. * @param string $target_width
  6. * @param string $target_height
  7. */
  8. function imageCropper($source_path, $target_width, $target_height){
  9. $source_info = getimagesize($source_path);
  10. $source_width = $source_info[0];
  11. $source_height = $source_info[1];
  12. $source_mime = $source_info['mime'];
  13. $source_ratio = $source_height / $source_width;
  14. $target_ratio = $target_height / $target_width;
  15. if ($source_ratio > $target_ratio){
  16. // image-to-height
  17. $cropped_width = $source_width;
  18. $cropped_height = $source_width * $target_ratio;
  19. $source_x = 0;
  20. $source_y = ($source_height - $cropped_height) / 2;
  21. }elseif ($source_ratio < $target_ratio){
  22. //image-to-widht
  23. $cropped_width = $source_height / $target_ratio;
  24. $cropped_height = $source_height;
  25. $source_x = ($source_width - $cropped_width) / 2;
  26. $source_y = 0;
  27. }else{
  28. //image-size-ok
  29. $cropped_width = $source_width;
  30. $cropped_height = $source_height;
  31. $source_x = 0;
  32. $source_y = 0;
  33. }
  34. switch ($source_mime){
  35. case 'image/gif':
  36. $source_image = imagecreatefromgif($source_path);
  37. break;
  38. case 'image/jpeg':
  39. $source_image = imagecreatefromjpeg($source_path);
  40. break;
  41. case 'image/png':
  42. $source_image = imagecreatefrompng($source_path);
  43. break;
  44. default:
  45. return ;
  46. break;
  47. }
  48. $target_image = imagecreatetruecolor($target_width, $target_height);
  49. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  50. // copy
  51. imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  52. // zoom
  53. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  54. header('Content-Type: image/jpeg');
  55. imagejpeg($target_image);
  56. imagedestroy($source_image);
  57. imagedestroy($target_image);
  58. imagedestroy($cropped_image);
  59. }
  60. $filename = "8fcb7a0831b79c61.jpg";
  61. imageCropper($filename,200,200);
  62. ?>

图片按比例裁剪

  1. <?php
  2. /**
  3. * imageZoom
  4. * @param string $file
  5. * @param double $zoom
  6. */
  7. function imageZoom($filename,$zoom=0.6){
  8. //baseinfo
  9. $sourceImageInfo = getimagesize($filename);
  10. $sourceWidth = $sourceImageInfo[0];
  11. $sourceHeight = $sourceImageInfo[1];
  12. $sourceMine = $sourceImageInfo['mime'];
  13. $sourceRatio = $sourceWidth/$sourceHeight;
  14. $sourceX = 0;
  15. $sourceY = 0;
  16. //zoom
  17. $targetRatio = $zoom;
  18. //target-widht-height
  19. $targetWidth = $sourceWidth*$targetRatio;
  20. $targetHeight = $sourceHeight*$targetRatio;
  21. //init-params
  22. $sourceImage = null;
  23. switch($sourceMine){
  24. case 'image/gif':
  25. $sourceImage = imagecreatefromgif($filename);
  26. break;
  27. case 'image/jpeg':
  28. $sourceImage = imagecreatefromjpeg($filename);
  29. break;
  30. case 'image/png':
  31. $sourceImage = imagecreatefrompng($filename);
  32. break;
  33. default:
  34. return ;
  35. break;
  36. }
  37. //temp-target-image
  38. $tempSourceImage = imagecreatetruecolor($sourceWidth, $sourceHeight);
  39. $targetImage = imagecreatetruecolor($targetWidth,$targetHeight);
  40. //copy
  41. imagecopy($tempSourceImage, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
  42. //zoom
  43. imagecopyresampled($targetImage, $tempSourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
  44. //header
  45. header('Content-Type: image/jpeg');
  46. //image-loading
  47. imagejpeg($targetImage);
  48. //destroy
  49. imagedestroy($tempSourceImage);
  50. imagedestroy($sourceImage);
  51. imagedestroy($targetImage);
  52. }
  53. $filename = "8fcb7a0831b79c61.jpg";
  54. imageZoom($filename);
  55. ?>

希望本文所述对大家PHP程序设计有所帮助。