php 图片等比例缩放和截图

字体大小: 中小 标准 ->行高大小: 标准

为什么要缩放和截图呢?最重要一点,就是加载的速度,如果我想要显示一个30x30的图片,但是你上传的图片是300x300,页面加载图片的时候,肯定30x30的要快。所以我们就要截取或者缩放,现在sns的网站都提供,头像的截取。

一,准备test.html页面

  1. <html>
  2. <body>
  3. <form action='test.php' method='post' name='form' enctype="multipart/form-data">
  4. <input type='file' name='photo'>
  5. <input type=submit value='submit'>
  6. </form>
  7. </body>
  8. </html>

二,准备Image.class.php文件

  1. <?php
  2. class Image {
  3. protected $nameinfo;
  4. protected $InputImageFileExtension;
  5. public static function getInstance() {
  6. static $instance;
  7. if (!isset ($instance)) {
  8. $class = __CLASS__;
  9. $instance = new $class ();
  10. }
  11. return $instance;
  12. }
  13. function uploadresize($fileparam, $imageparam) {
  14. $this->nameinfo = explode('.',$fileparam['name']);
  15. //取得图片格式
  16. $this->InputImageFileExtension = $this->nameinfo[1];
  17. //判断是不是给了新的文件名
  18. if(emptyempty($imageparam['imagename'])){
  19. $outputFileName = $this->nameinfo[0];
  20. }else{
  21. $outputFileName = $imageparam['imagename'];
  22. }
  23. //判断路径是否正确
  24. if (!file_exists($imageparam['imagepath'])) {
  25. if(!mkdir($imageparam['imagepath'])){
  26. throw new Exception('image path is wrong');
  27. exit;
  28. }
  29. }
  30. $file_src = $imageparam['imagepath']."/". $outputFileName . "." . $this->InputImageFileExtension;
  31. // temporary safe image storage
  32. if(file_exists($file_src)){
  33. unlink($file_src);
  34. }
  35. move_uploaded_file($fileparam['tmp_name'], $file_src);
  36. switch ($this->InputImageFileExtension) {
  37. case "jpg" :
  38. $SRC_IMAGE = ImageCreateFromJPEG($file_src);
  39. break;
  40. case "gif" :
  41. $SRC_IMAGE = ImageCreateFromgif($file_src);
  42. break;
  43. case "wbmp" :
  44. $SRC_IMAGE = ImageCreateFromwbmp($file_src);
  45. break;
  46. case "png" :
  47. $SRC_IMAGE = ImageCreateFrompng($file_src);
  48. break;
  49. }
  50. if(emptyempty($imageparam['endy']) || !emptyempty($imageparam['radio'])){
  51. $imageparam['endy'] = ImageSY($SRC_IMAGE);
  52. }
  53. if(emptyempty($imageparam['endx']) || !emptyempty($imageparam['radio'])){
  54. $imageparam['endx'] = ImageSX($SRC_IMAGE);
  55. }
  56. if(!emptyempty($imageparam['radio'])){
  57. $imageparam['startx'] = 0;
  58. $imageparam['starty'] = 0;
  59. $imageparam['outx'] = $imageparam['endx']*$imageparam['radio'];
  60. $imageparam['outy'] = $imageparam['endy']*$imageparam['radio'];
  61. }
  62. $DEST_IMAGE = imagecreatetruecolor($imageparam['outx'], $imageparam['outy']);
  63. unlink($file_src);
  64. $createimage=imagecopyresampled($DEST_IMAGE, $SRC_IMAGE, 0, 0, $imageparam['startx'], $imageparam['starty'], $imageparam['outx'], $imageparam['outy'],$imageparam['endx'], $imageparam['endy']);
  65. if (!$createimage) {
  66. imagedestroy($SRC_IMAGE);
  67. imagedestroy($DEST_IMAGE);
  68. return (0);
  69. } else {
  70. imagedestroy($SRC_IMAGE);
  71. switch (strtolower($this->InputImageFileExtension)) {
  72. case "jpg" :
  73. $I = ImageJPEG($DEST_IMAGE, $file_src);
  74. break;
  75. case "gif" :
  76. $I = Imagegif($DEST_IMAGE, $file_src);
  77. break;
  78. case "wbmp" :
  79. $I = Imagewbmp($DEST_IMAGE, $file_src);
  80. break;
  81. case "png" :
  82. $I = Imagepng($DEST_IMAGE, $file_src);
  83. break;
  84. }
  85. if ($I) {
  86. imagedestroy($DEST_IMAGE);
  87. }
  88. }
  89. $URL = $file_src;
  90. return $URL;
  91. }
  92. }
  93. ?>

三,准备调用文件test.php

  1. <?php
  2. include "Image.class.php";
  3. $array = $_FILES['photo'];
  4. //截取参数
  5. $resizeParam = array (
  6. 'imagename' => "", //默认情况下,来中原文件名
  7. 'outx' => 300, //输入时的横坐标
  8. 'outy' => 300, //输入时的纵坐标
  9. 'startx' => 100, //原图中的起点横坐标
  10. 'starty' => 100, //原图中的起点纵坐标
  11. 'endx' => 300, //原图中的终点横坐标
  12. 'endy' => 300, //原图中的终点纵坐标
  13. 'imagepath' => 'd:/xampp/www' //图片存储路径
  14. );
  15. //等比例缩放参数
  16. $resizeParam = array (
  17. 'imagename' => "aaaa", //默认情况下,还是原文件名,在这里缩放后文件名为aaaa
  18. 'radio' => 0.5, //缩放比例
  19. 'imagepath' => 'd:/xampp/www' //图片存储路径
  20. );
  21. if (!emptyempty ($array)) {
  22. $crop = Image :: getInstance()->uploadresize($array, $resizeParam);
  23. }
  24. ?>

说明,在上面的操作中,我没有做,是否是图片的check,图片大小的check,截取位置是否超出图片范围的check。改一点点就ok了。

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/67520.html