php 图片等比例缩放和截图

张映 发表于 2010-04-19

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

三,准备调用文件test.php

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

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



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/php/645.html