为什么要缩放和截图呢?最重要一点,就是加载的速度,如果我想要显示一个30x30的图片,但是你上传的图片是300x300,页面加载图片的时候,肯定30x30的要快。所以我们就要截取或者缩放,现在sns的网站都提供,头像的截取。
一,准备test.html页面
- <html>
- <body>
- <form action='test.php' method='post' name='form' enctype="multipart/form-data">
- <input type='file' name='photo'>
- <input type=submit value='submit'>
- </form>
- </body>
- </html>
二,准备Image.class.php文件
- <?php
- class Image {
- protected $nameinfo;
- protected $InputImageFileExtension;
- public static function getInstance() {
- static $instance;
- if (!isset ($instance)) {
- $class = __CLASS__;
- $instance = new $class ();
- }
- return $instance;
- }
- function uploadresize($fileparam, $imageparam) {
- $this->nameinfo = explode('.',$fileparam['name']);
- //取得图片格式
- $this->InputImageFileExtension = $this->nameinfo[1];
- //判断是不是给了新的文件名
- if(emptyempty($imageparam['imagename'])){
- $outputFileName = $this->nameinfo[0];
- }else{
- $outputFileName = $imageparam['imagename'];
- }
- //判断路径是否正确
- if (!file_exists($imageparam['imagepath'])) {
- if(!mkdir($imageparam['imagepath'])){
- throw new Exception('image path is wrong');
- exit;
- }
- }
- $file_src = $imageparam['imagepath']."/". $outputFileName . "." . $this->InputImageFileExtension;
- // temporary safe image storage
- if(file_exists($file_src)){
- unlink($file_src);
- }
- move_uploaded_file($fileparam['tmp_name'], $file_src);
- switch ($this->InputImageFileExtension) {
- case "jpg" :
- $SRC_IMAGE = ImageCreateFromJPEG($file_src);
- break;
- case "gif" :
- $SRC_IMAGE = ImageCreateFromgif($file_src);
- break;
- case "wbmp" :
- $SRC_IMAGE = ImageCreateFromwbmp($file_src);
- break;
- case "png" :
- $SRC_IMAGE = ImageCreateFrompng($file_src);
- break;
- }
- if(emptyempty($imageparam['endy']) || !emptyempty($imageparam['radio'])){
- $imageparam['endy'] = ImageSY($SRC_IMAGE);
- }
- if(emptyempty($imageparam['endx']) || !emptyempty($imageparam['radio'])){
- $imageparam['endx'] = ImageSX($SRC_IMAGE);
- }
- if(!emptyempty($imageparam['radio'])){
- $imageparam['startx'] = 0;
- $imageparam['starty'] = 0;
- $imageparam['outx'] = $imageparam['endx']*$imageparam['radio'];
- $imageparam['outy'] = $imageparam['endy']*$imageparam['radio'];
- }
- $DEST_IMAGE = imagecreatetruecolor($imageparam['outx'], $imageparam['outy']);
- unlink($file_src);
- $createimage=imagecopyresampled($DEST_IMAGE, $SRC_IMAGE, 0, 0, $imageparam['startx'], $imageparam['starty'], $imageparam['outx'], $imageparam['outy'],$imageparam['endx'], $imageparam['endy']);
- if (!$createimage) {
- imagedestroy($SRC_IMAGE);
- imagedestroy($DEST_IMAGE);
- return (0);
- } else {
- imagedestroy($SRC_IMAGE);
- switch (strtolower($this->InputImageFileExtension)) {
- case "jpg" :
- $I = ImageJPEG($DEST_IMAGE, $file_src);
- break;
- case "gif" :
- $I = Imagegif($DEST_IMAGE, $file_src);
- break;
- case "wbmp" :
- $I = Imagewbmp($DEST_IMAGE, $file_src);
- break;
- case "png" :
- $I = Imagepng($DEST_IMAGE, $file_src);
- break;
- }
- if ($I) {
- imagedestroy($DEST_IMAGE);
- }
- }
- $URL = $file_src;
- return $URL;
- }
- }
- ?>
三,准备调用文件test.php
- <?php
- include "Image.class.php";
- $array = $_FILES['photo'];
- //截取参数
- $resizeParam = array (
- 'imagename' => "", //默认情况下,来中原文件名
- 'outx' => 300, //输入时的横坐标
- 'outy' => 300, //输入时的纵坐标
- 'startx' => 100, //原图中的起点横坐标
- 'starty' => 100, //原图中的起点纵坐标
- 'endx' => 300, //原图中的终点横坐标
- 'endy' => 300, //原图中的终点纵坐标
- 'imagepath' => 'd:/xampp/www' //图片存储路径
- );
- //等比例缩放参数
- $resizeParam = array (
- 'imagename' => "aaaa", //默认情况下,还是原文件名,在这里缩放后文件名为aaaa
- 'radio' => 0.5, //缩放比例
- 'imagepath' => 'd:/xampp/www' //图片存储路径
- );
- if (!emptyempty ($array)) {
- $crop = Image :: getInstance()->uploadresize($array, $resizeParam);
- }
- ?>
说明,在上面的操作中,我没有做,是否是图片的check,图片大小的check,截取位置是否超出图片范围的check。改一点点就ok了。
转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/php/645.html