php检查测验上传图片的长度和幅度,PHP完结图片裁剪与缩放的两种方法

 

上传头像,分界面无跳转的措施比相当多,笔者用的是加个iframe这种。下边直接上代码。

方法一

/*
array getimagesize ( string $filename [, array &$imageinfo ] )

 代码如下

html:

 代码如下

getimagesize()函数将显著别的给定的图像大小的文书,并重返随着文件类型和可观/宽度的文本字符串是在叁个常规的HTML
IMG标签和呼应的HTTP内容类型所利用的尺码。
The getimagesize() function will determine the size of any given image
file and return the dimensions along with the file type and a
height/width text string to be used inside a normal HTML I www.111cn.net
MG tag and the correspondant HTTP content type

<?php

复制代码 代码如下:

图表裁剪缩放函数,协理方面裁剪和自定义坐标裁剪
    /**
     * 图片裁剪函数,接济钦定定点裁剪和方向裁剪三种裁剪形式
     * @param <string>  $src_file       原图片路线
     * @param <int>     $new_width     
裁剪后图片宽度(当宽度超过原图片宽度时,去原图片宽度)
     * @param <int>     $new_height    
裁剪后图片中度(当宽度超越原图片宽度时,去原图片中度)
     * @param <int>     $type          
裁剪方式,1-方位方式裁剪;0-定位情势裁剪。
     * @param <int>     $pos           
方位格局裁剪时的发端方位(当选定点形式裁剪时,此参数不起功能)
     *                                     
1为最上部居左,2为顶上部分居中,3为顶部居右;
     *                                     
4为当道居左,5为当道居中,6为当道居右;
     *                                     
7为底端居左,8为底端居中,9为底端居右;
     * @param <int>     $start_x        伊始地方X
(当选定方位情势裁剪时,此参数不起功用)
     * @param <int>     $start_y       
起头地点Y(当选定方位情势裁剪时,此参数不起功效)
     * @return <string>                 裁剪图片存款和储蓄路线
     */
    function thumb($src_file, $new_width, $new_height, $type = 1,
$pos = 5, $start_x = 0, $start_y = 0) {
        $pathinfo = pathinfo($src_file);
        $dst_file = $pathinfo[‘dirname’] . ‘/’ .
$pathinfo[‘filename’] .’_’. $new_width . ‘x’ . $new_height . ‘.’ .
$pathinfo[‘extension’];
        if (!file_exists($dst_file)) {
            if ($new_width < 1 || $new_height < 1) {
                echo “params width or height error !”;
                exit();
            }
            if (!file_exists($src_file)) {
                echo $src_file . ” is not exists !”;
                exit();
            }
            // 图像类型
            $img_type = exif_imagetype($src_file);
            $support_type = array(IMAGETYPE_JPEG, IMAGETYPE_PNG,
IMAGETYPE_GIF);
            if (!in_array($img_type, $support_type, true)) {
                echo “只援助jpg、png、gif格式图片裁剪”;
                exit();
            }
            /* 载入图像 */
            switch ($img_type) {
                case IMAGETYPE_JPEG :
                    $src_img = imagecreatefromjpeg($src_file);
                    break;
                case IMAGETYPE_PNG :
                    $src_img = imagecreatefrompng($src_file);
                    break;
                case IMAGETYPE_GIF :
                    $src_img = imagecreatefromgif($src_file);
                    break;
                default:
                echo “载入图像错误!”;
                exit();
            }
            /* 获取源图片的小幅度和中度 */
            $src_width = imagesx($src_img);
            $src_height = imagesy($src_img);
            /* 总结剪切图片的幅度和中度 */
            $mid_width = ($src_width < $new_width) ? $src_width :
$new_width;
            $mid_height = ($src_height < $new_height) ?
$src_height : $new_height;
            /* 开首化源图片剪切裁剪的发端地方坐标 */
            switch ($pos * $type) {
                case 1://1为顶部居左
                    $start_x = 0;
                    $start_y = 0;
                    break;
                case 2://2为顶上部分居中
                    $start_x = ($src_width – $mid_width) / 2;
                    $start_y = 0;
                    break;
                case 3://3为最上部居右
                    $start_x = $src_width – $mid_width;
                    $start_y = 0;
                    break;
                case 4://4为主旨居左
                    $start_x = 0;
                    $start_y = ($src_height – $mid_height) / 2;
                    break;
                case 5://5为中心居中
                    $start_x = ($src_width – $mid_width) / 2;
                    $start_y = ($src_height – $mid_height) / 2;
                    break;
                case 6://6为当道居右
                    $start_x = $src_width – $mid_width;
                    $start_y = ($src_height – $mid_height) / 2;
                    break;
                case 7://7为底端居左
                    $start_x = 0;
                    $start_y = $src_height – $mid_height;
                    break;
                case 8://8为底端居中
                    $start_x = ($src_width – $mid_width) / 2;
                    $start_y = $src_height – $mid_height;
                    break;
                case 9://9为底端居右
                    $start_x = $src_width – $mid_width;
                    $start_y = $src_height – $mid_height;
                    break;
                default://随机
                    break;
            }
            // 为划分图像创造背景画板
            $mid_img = imagecreatetruecolor($mid_width,
$mid_height);
            //拷贝剪切的图像数据到画板,生成剪切图像
            imagecopy($mid_img, $src_img, 0, 0, $start_x, $start_y,
$mid_width, $mid_height);
            // 为裁剪图像创造背景画板
            $new_img = imagecreatetruecolor($new_width,
$new_height);
            //拷贝剪切图像到背景画板,并按百分比裁剪
            imagecopyresampled($new_img, $mid_img, 0, 0, 0, 0,
$new_width, $new_height, $mid_width, $mid_height);
            /* 按格式保存为图片 */
            switch ($img_type) {
                case IMAGETYPE_JPEG :
                    imagejpeg($new_img, $dst_file, 100);
                    break;
                case IMAGETYPE_PNG :
                    imagepng($new_img, $dst_file, 9);
                    break;
                case IMAGETYPE_GIF :
                    imagegif($new_img, $dst_file, 100);
                    break;
                default:
                    break;
            }
        }
        return ltrim($dst_file, ‘.’);
    }

和getimagesize()也得以再次回到一些imageinfo参数的更加多消息。

/**

//route 为后端接口
//upload/avatar 为上传的头像的保留地址
//imgurl=/upload/avatar/<?=$uid?>
这里最终的<?=$uid?>是为了前边用js完毕即时显得最新的改变后的头像用的,参照上边的js部分的代码
//头像保存名叫uid.type,如1.jpg,2.png等
//$user[‘avatar’]
顾客只要上传过头像,该客商数据库中的avatar字段将给予时间戳,不然为空
<form target=”iframe” enctype=”multipart/form-data”
action=”route?imgurl=/upload/avatar/<?=$uid?>” method=”post”
id=”upload_form”>
<img class=”thumb” src=”<?php if($user[‘avatar’]) echo $my_img;
else echo ‘/view/img/100.png’; ?>” style=”width:100px; height:100px;”
/>
<input type=”file” name=”file” size=”28″ />
<input type=”submit” name=”submit_file” value=”确定” style=”display:
none;”/>
</form>
<iframe id=”iframe” name=”iframe” style=”display:
none;”></iframe>

附件是二个有样例图片的测验demo。
而是,那个函数不帮忙动态gif图片的剪裁缩放。

在乎:请稳重,少年警察讯问和JP2是有两样位深度组件的力量。在这种景况下,为“比特”的市场总值是参天的位深度的好多不便。别的,JP2上的JPEG文件大概带有四个3000codestreams。在这种状态下,和getimagesize()重临第叁个码流的股票总值是在文书的根接触。

* @name thumb  缩略图函数

php:

方法二,

注:有关材质寻找Logo从最高比特率Logo。
*/

* @param  sting  $img_name  图片路径

php检查测验上传图片的长度和幅度,PHP完结图片裁剪与缩放的两种方法。复制代码 代码如下:

 代码如下

list($width, $height) = getimagesize($image);
$new_dimensions = resize_dimensions(300,400,$width,$height);

* @param  int   $max_width 略图最大幅度面

$token = param(‘token’);
$user = user_from_token($token);
!$user AND exit(“<p class=’iframe_message’
status=’0′>$lang[user_not_exists]美高梅开户网址,</p>”);
//文件存款和储蓄路线
$file_path=”./upload/avatar/”;
//664权力为文件属主和属组客商可读和写,其余顾客只读。
if(is_dir($file_path) != TRUE) mkdir($file_path, 0664) ;
//定义允许上传的文本扩展名
$ext_arr = array(“gif”, “jpg”, “jpeg”, “png”, “bmp”);

<?php
list($src_w,$src_h)=getimagesize($src_img);  // 获取原图尺寸
$dst_scale = $dst_h/$dst_w; //指标图像长度宽度比
$src_scale = $src_h/$src_w; // 原图长度宽度比

// Calculates restricted dimensions with a maximum of $goal_width by
$goal_height
function resize_dimensions($goal_width,$goal_height,$width,$height)
{
    $return = array(‘width’ => $width, ‘height’ => $height);
   
    // If the ratio > goal ratio and the width > goal width resize
down to goal width
    if ($width/$height > $goal_width/$goal_height && $width >
$goal_width) {
        $return[‘width’] = $goal_width;
        $return[‘height’] = $goal_width/$width * $height;
    }
    // Otherwise, if the height > goal, resize down to goal height
    else if ($height > $goal_height) {
        $return[‘width’] = $goal_height/$height * $width;
        $return[‘height’] = $goal_height;
    }
   
    return $return;
}

* @param  int   $max_height 略图最大惊人

if (empty($_FILES) === false) {
  //推断检查
  $photo_up_size > 2097152 AND exit(“<p
class=’iframe_message’
status=’0′>对不起,您上传的肖像抢先了2M</p>”);
  $_FILES[“file”][“error”] > 0 AND exit(“<p
class=’iframe_message’
status=’0′>文件上传产生错误:”.$_FILES[“file”][“error”].”</p>”);
  //获得文件扩充名
  $temp_arr = explode(“.”, $_FILES[“file”][“name”]);
  $file_ext = array_pop($temp_arr);
  $file_ext = trim($file_ext);
  $file_ext = strtolower($file_ext);
  //检查扩充名
  if (in_array($file_ext, $ext_arr) === false) {
    exit(“<p class=’iframe_message’
status=’0′>上传文件扩张名是不容许的扩大名</p>”);
  }
  //删除目录下一样前缀的文件
  if($dh = opendir($file_path)) {
    while(($file = readdir($dh)) !== false) {
      $file_arr = $file.split(‘.’);
      if($file_arr[0] == $user[‘uid’])
unlink($file_path.$file);
    }
  }
  //以uid重命名文件
  $new_name = $user[‘uid’].”.”.$file_ext;
  //将文件移动到存款和储蓄目录下
  move_uploaded_file($_FILES[“file”][“tmp_name”],
$file_path.$new_name);
  //裁剪压缩图片
  clip($file_path.$new_name, $file_path.$new_name, 0, 0, 100,
100);
  clip_thumb($file_path.$new_name, $file_path.$new_name, 100,
100);
  //向数据表写入文件存款和储蓄音信以便管理
  user_update($user[‘uid’], array(‘avatar’=>time()));
  exit(“<p class=’iframe_message’
status=’1′>文件上传成功</p>”);
} else {
  exit(“<p class=’iframe_message’
status=’0′>无科学的文本上传</p>”);
}

if($src_scale>=$dst_scale)

 // 过高
 $w = intval($src_w);
 $h = intval($dst_scale*$w);
 $x = 0;
 $y = ($src_h – $h)/3;
}
else
{
// 过宽
 $h = intval($src_h);
 $w = intval($h/$dst_scale);
 $x = ($src_w – $w)/2;
 $y = 0;
}
// 剪裁
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);
// 缩放
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w*$scale);
$final_h = intval($h*$scale);
imagecopysampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);
// 保存
$timestamp = time();
imagejpeg($target, “$timestamp.jpg”);
imagedestroy($target);
?>

/*
地方的函数我们固然利用

* @param  sting  $suffix
略图后缀(如”img_x.jpg”代表小图,”img_m.jpg”代表中图,”img_l.jpg”代表大图)

<?php

方法三

php 有个图片GD库getimagesize()函数。
有个函数是赢得图片的大旨消息。
getimagesize()
$img=getimagesize(‘图片源’);
宽度为=$img[0];
高度为=$img[1];
格式为=$img[2];

* @return  void

function ext($filename) {
return strtolower(substr(strrchr($filename, ‘.’), 1));
}

据书上说自定义的指标图, 按目的图高宽比裁剪、缩放源图.

一旦你要轻便的话能够更简便易行如

*/

/*
实例:
thumb(APP_PATH.’xxx.jpg’, APP_PATH.’xxx_thumb.jpg’, 200, 200);

盛传参数:

*/
$picpath = ‘ww.111cn.net.gif’;
$array = getimagesize($picpath);
print_r( $array );

functionthumb($img_name,$max_width,$max_height,$path,$new_name)

返回:
array(‘filesize’=>0, ‘width’=>0, ‘height’=>0)
*/
function thumb($sourcefile, $destfile, $forcedwidth = 80, $forcedheight
= 80) {
$return = array(‘filesize’=>0, ‘width’=>0, ‘height’=>0);
$imgcomp = 10;
$destext = ext($destfile);
if(!in_array($destext, array(‘gif’, ‘jpg’, ‘bmp’, ‘png’))) {
return $return;
}

 代码如下

echo ‘图片宽度为’.$array[0];
echo ‘图片高度为’.$array[1];
echo ‘图片格式为’.$array[2];

{

$imgcomp = 100 – $imgcomp;
$imginfo = getimagesize($sourcefile);
$src_width = $imginfo[0];
$src_height = $imginfo[1];
if($src_width == 0 || $src_height == 0) {
return $return;
}
$src_scale = $src_width / $src_height;
$des_scale = $forcedwidth / $forcedheight;

$source_path string 源图路线

方法四
  //renumber
  $my_image = array_values(getimagesize(‘test.jpg’));
  //use list on new array
  list($width, $height, $type, $attr) = $my_image;

  $img_infos=getimagesize($img_name);

if(!function_exists(‘imagecreatefromjpeg’)) {
copy($sourcefile, $destfile);
$return = array(‘filesize’=>filesize($destfile),
‘width’=>$src_width, ‘height’=>$src_height);
return $return;
}

$target_width integer 指标图宽度

  //view new array
  print_r($my_image);

  $img_height=$img_infos[0];//图片高

// 按规定比例缩略
if($src_width <= $forcedwidth && $src_height <= $forcedheight)
{
$des_width = $src_width;
$des_height = $src_height;
} elseif($src_scale >= $des_scale) {
$des_width = ($src_width >= $forcedwidth) ? $forcedwidth :
$src_width;
$des_height = $des_width / $src_scale;
$des_height = ($des_height >= $forcedheight) ? $forcedheight :
$des_height;
} else {
$des_height = ($src_height >= $forcedheight) ? $forcedheight :
$src_height;
$des_width = $des_height * $src_scale;
$des_width = ($des_width >= $forcedwidth) ? $forcedwidth :
$des_width;
}

$target_height integer 指标图中度

  //spit out content
  echo ‘Attribute: ‘.$attr.'<br />’;
  echo ‘Width: ‘.$width.'<br />’;

  $img_width=$img_infos[1];//图片宽

switch ($imginfo[‘mime’]) {
case ‘image/jpeg’:
$img_src = imagecreatefromjpeg($sourcefile);
!$img_src && $img_src = imagecreatefromgif($sourcefile);
break;
case ‘image/gif’:
$img_src = imagecreatefromgif($sourcefile);
!$img_src && $img_src = imagecreatefromjpeg($sourcefile);
break;
case ‘image/png’:
$img_src = imagecreatefrompng($sourcefile);
break;
case ‘image/wbmp’:
$img_src = imagecreatefromwbmp($sourcefile);
break;
default :
return $return;
}

支撑图片类型: image/gif, image/jpeg, image/png.

//这里面就能有图表的幅度与高度了

  $img_extension=”;   //图片后缀名

$img_dst = imagecreatetruecolor($des_width, $des_height);
$img_color = imagecolorallocate($img_dst, 255, 255, 255);
imagefill($img_dst, 0, 0 ,$img_color);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $des_width,
$des_height, $src_width, $src_height);
//$tmpfile = $temp_path.md5($destfile);
$tmpfile = $destfile;
switch($destext) {
case ‘jpg’: imagejpeg($img_dst, $tmpfile, $imgcomp); break;
case ‘gif’: imagegif($img_dst, $tmpfile, $imgcomp); break;
case ‘png’: imagepng($img_dst, $tmpfile, version_compare(PHP_VERSION,
‘5.1.2’) == 1 ? 7 : 70); break;
}
$r = array(‘filesize’=>filesize($tmpfile), ‘width’=>$des_width,
‘height’=>$des_height);;
copy($tmpfile, $destfile);
//unlink($tmpfile);
imagedestroy($img_dst);
return $r;
}
/*
* 图片裁切
*
* @param string $srcname 原图片路径(绝对路线/*.jpg)
* @param string $forcedheight 裁切后生成新名称(相对路线/rename.jpg)
* @param int $sourcefile 被裁切图片的X坐标
* @param int $destfile 被裁切图片的Y坐标
* @param int $destext 被裁区域的肥瘦
* @param int $imgcomp 被裁区域的莫斯中国科学技术大学学
clip(‘xxx/x.jpg’, ‘xxx/newx.jpg’, 10, 40, 150, 150)
*/
function clip($sourcefile, $destfile, $clipx, $clipy, $clipwidth,
$clipheight) {
$getimgsize = getimagesize($sourcefile);
if(empty($getimgsize)) {
return 0;
} else {
$imgwidth = $getimgsize[0];
$imgheight = $getimgsize[1];
if($imgwidth == 0 || $imgheight == 0) {
return 0;
}
}

function imagecropper($source_path, $target_width, $target_height)
{
 $source_info   = getimagesize($source_path);
 $source_width  = $source_info[0];
 $source_height = $source_info[1];
 $source_mime   = $source_info[‘mime’];
 $source_ratio  = $source_height / $source_width;
 $target_ratio  = $target_height / $target_width;

//再一个用到getimagesize显示缩略图的代码
function show_thumbnail($file)
{
    $max = 200 // Max. thumbnail width and height

  

if(!function_exists(‘imagecreatefromjpeg’)) {
copy($sourcefile, $destfile);
return filesize($destfile);
}
switch($getimgsize[2]) {
case 1 :
$imgcolor = imagecreatefromgif($sourcefile);
break;
case 2 :
$imgcolor = imagecreatefromjpeg($sourcefile);
break;
case 3 :
$imgcolor = imagecreatefrompng($sourcefile);
break;
}
//$imgcolor = imagecreatefromjpeg($sourcefile);
$img_dst = imagecreatetruecolor($clipwidth, $clipheight);
$img_color = imagecolorallocate($img_dst, 255, 255, 255);
imagefill($img_dst, 0, 0, $img_color);
imagecopyresampled($img_dst, $imgcolor, 0, 0, $clipx, $clipy,
$imgwidth, $imgheight, $imgwidth, $imgheight);
$tmpfile = $destfile;
imagejpeg($img_dst, $tmpfile, 100);
$n = filesize($tmpfile);
copy($tmpfile, $destfile);
return $n;
}

 // 源图过高
 if ($source_ratio > $target_ratio)
 {
  $cropped_width  = $source_width;
  $cropped_height = $source_width * $target_ratio;
  $source_x = 0;
  $source_y = ($source_height – $cropped_height) / 2;
 }
 // 源图过宽
 elseif ($source_ratio < $target_ratio)
 {
  $cropped_width  = $source_height / $target_ratio;
  $cropped_height = $source_height;
  $source_x = ($source_width – $cropped_width) / 2;
  $source_y = 0;
 }
 // 源图适中
 else
 {
  $cropped_width  = $source_width;
  $cropped_height = $source_height;
  $source_x = 0;
  $source_y = 0;
 }

    $size = getimagesize($file);

  switch($img_infos[2])

// 先裁切后缩略,因为明确了,width, height, 没有须要再次来到宽高。
function clip_thumb($sourcefile, $destfile, $forcedwidth = 80,
$forcedheight = 80) {
// 获取原图片宽高
$getimgsize = getimagesize($sourcefile);
if(empty($getimgsize)) {
return 0;
} else {
$src_width = $getimgsize[0];
$src_height = $getimgsize[1];
if($src_width == 0 || $src_height == 0) {
return 0;
}
}

 switch ($source_mime)
 {
  case ‘image/gif’:
   $source_image = imagecreatefromgif($source_path);
   break;

    if ( $size[0] <= $max && $size[1] <= $max )
    {
        $ret = ‘<img src=”‘.$file.'” ‘.$size[3].’
border=”0″>’;
    }
    else
    {
        $k = ( $size[0] >= $size[1] ) ? $size[0] / $max :
$size[1] / $max;
        $ret = ‘<a
href=”javascript教程:;”
onClick=”window.open(‘image.php?img=’;
        $ret .= $file.”,”,’width=’.$size[0];
        $ret .= ‘,height=’.$size[1].”)”>’;
        $ret .= ‘<img src=”‘.$file.'”
width=”‘.floor($size[0]/$k).'” height=”‘.floor($size[1]/$k).'”
border=”0″ alt=”View full-size image”></a>’;
    }

  {

$src_scale = $src_width / $src_height;
$des_scale = $forcedwidth / $forcedheight;

  case ‘image/jpeg’:
   $source_image = imagecreatefromjpeg($source_path);
   break;

    return $ret;
}

    case1:

if($src_width <= $forcedwidth && $src_height <= $forcedheight)
{
$des_width = $src_width;
$des_height = $src_height;
$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height);
return filesize($destfile);
// 原图为横着的矩形
} elseif($src_scale >= $des_scale) {
// 以原图的冲天作为标准,实行缩略
$des_height = $src_height;
$des_width = $src_height / $des_scale;
$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height);
if($n <= 0) return 0;
$r = thumb($destfile, $destfile, $forcedwidth, $forcedheight);
return $r[‘filesize’];
// 原图为竖着的矩形
} else {
// 以原图的小幅度作为正式,实行缩略
$des_width = $src_width;
$des_height = $src_width / $des_scale;
$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height);
if($n <= 0) return 0;
$r = thumb($destfile, $destfile, $forcedwidth, $forcedheight);
return $r[‘filesize’];
}
}

  case ‘image/png’:
   $source_image = imagecreatefrompng($source_path);
   break;

      $img_extension=’gif’;

?>

  default:
   return false;
   break;
 }

      break;

笔者们php中安装重临内容,会发现,在产出相应处境后,重临内容出现在页面包车型大巴iframe中,所以大家设定了相应的class,以便前端获得再次来到内容,做出相应管理。clip(),clip_thumb()为裁剪图片函数,可收缩图片大小,裁取图片以左上角为起源,长度宽度为100的圆柱形。

 $target_image  = imagecreatetruecolor($target_width,
$target_height);
 $cropped_image = imagecreatetruecolor($cropped_width,
$cropped_height);

    case2:

js:

 // 裁剪
 imagecopy($cropped_image, $source_image, 0, 0, $source_x,
$source_y, $cropped_width, $cropped_height);
 // 缩放
 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0,
$target_width, $target_height, $cropped_width, $cropped_height);

      $img_extension=’jpeg’;

复制代码 代码如下:

 header(‘Content-Type: image/jpeg’);
 imagejpeg($target_image);
 imagedestroy($source_image);
 imagedestroy($target_image);
 imagedestroy($cropped_image);
}

      break;

var jsubmit_file = jinput.filter(‘[name=”submit_file”]’);
var jfile = jinput.filter(‘[name=”file”]’);
var jiframe = $(‘#iframe’);
var jthumb = $(‘.thumb’);
var type = ”;
jfile.on(‘change’, function() {
var path = jfile.val();
var file_arr = path.split(‘.’);
type = file_arr[file_arr.length-1];
jsubmit_file.trigger(‘click’);
});
jiframe.on(‘load’, function() {
var jiframe_message =
$(window.frames[‘iframe’].document).find(‘.iframe_message’);
if(jiframe_message.attr(‘status’) != 0) {
var url = this.contentWindow.location.href;
var url_arr = url.split(‘=’);
jthumb.attr(‘src’, url_arr[1] + ‘.’ + type);
}
alert(jiframe_message.text());
});

    case3:

如此基本就兑现了图片上传、上传结果提醒、即时展现上传的新式头像那多少个功效,网络有各类插件,即便成效丰硕,正是体量太大,这个看大家选拔了。

      $img_extension=’png’;

您也许感兴趣的篇章:

  • php达成头像上传预览功用
  • php达成文件上传及头像预览功效
  • 针对多客户完成头像上传作用PHP代码
    适用于登录页面制作
  • php录制拍照上传头像效率达成代码分享
  • PHP+ajaxfileupload+jcrop插件完美兑现头像上传剪裁
  • php头像上传预览实例代码

      break;

    default:

      $img_extension=’jpeg’;

      break;

  }

  

  $new_img_size=array();

  $new_img_size[‘width’] =$max_width;

  $new_img_size[‘height’] =$max_height;

    

  $img_func=”;  //函数名称

  $img_handle=”; //图片句柄

  $thum_handle=”;//略图图片句柄

  

  switch($img_extension)

  {

    case’jpg’:

      $img_handle= imagecreatefromjpeg($img_name);

      $img_func=’imagejpeg’;

      break;

    case’jpeg’:

      $img_handle= imagecreatefromjpeg($img_name);

      $img_func=’imagejpeg’;

      break;

    case’png’:

      $img_handle= imagecreatefrompng($img_name);

      imagesavealpha($img_handle, true);

      $img_func=’imagepng’;

      break;

    case’gif’:

      $img_handle= imagecreatefromgif($img_name);

      $img_func=’imagegif’;

      break;

    default:

      $img_handle= imagecreatefromjpeg($img_name);

      $img_func=’imagejpeg’;

      break;

  }

  

  $quality= 100;//图片品质

  if($img_func==’imagepng’)

  {

    $quality= 9;

  } 

  

  $thum_handle=
imagecreatetruecolor($new_img_size[‘height’],$new_img_size[‘width’]);

  imagealphablending($thum_handle,false);//这里很关键,意思是不合併颜色,直接用$img图像颜色替换,包涵透明色;

  imagesavealpha($thum_handle,true);//这里相当的重大,意思是决不丢了$thumb图像的透明色;

  

  if(function_exists(‘imagecopyresampled’))

  {

    imagecopyresampled($thum_handle,$img_handle,  0, 0, 0,
 0,$new_img_size[‘height’],$new_img_size[‘width’],$img_height,$img_width);

  } 

  else 

  {

    imagecopyresized($thum_handle,$img_handle,  0, 0, 0,
 0,$new_img_size[‘height’],$new_img_size[‘width’],$img_height,$img_width);

  }

  

  call_user_func_array($img_func,array($thum_handle,$path.’/’.$new_name,$quality));

  imagedestroy($thum_handle);//清除句柄

  imagedestroy($img_handle);//清除句柄

}

  

  

$IconArray=array 

(

  “IOS8″=>array 

  (

    array(“width”=> 180, “height”=>
180,”path”=>”ios8”,  “name”=>”Icon-180.png”),

    array(“width”=> 120, “height”=>
120,”path”=>”ios8”,  “name”=>”Icon-120.png”),

    array(“width”=> 152, “height”=>
152,”path”=>”ios8”,  “name”=>”Icon-152.png”),

    array(“width”=> 76,  “height”=>
76,   “path”=>”ios8”,  “name”=>”Icon-76.png”), 

    array(“width”=> 144, “height”=>
144,”path”=>”ios8”,  “name”=>”Icon-144.png”),

    array(“width”=> 72,  “height”=>
72,   “path”=>”ios8”,  “name”=>”Icon-72.png”),

    array(“width”=> 114, “height”=>
114,”path”=>”ios8”,  “name”=>”Icon-114.png”),

    array(“width”=> 57,  “height”=>
57,   “path”=>”ios8”,  “name”=>”Icon-57.png”), 

    array(“width”=> 100, “height”=>
100,”path”=>”ios8”,  “name”=>”Icon-100.png”),

    array(“width”=> 50,  “height”=>
50,   “path”=>”ios8”,  “name”=>”Icon-50.png”), 

    array(“width”=> 80,  “height”=>
80,   “path”=>”ios8”,  “name”=>”Icon-80.png”),

    array(“width”=> 40,  “height”=>
40,   “path”=>”ios8”,  “name”=>”Icon-40.png”),

    array(“width”=> 58,  “height”=>
58,   “path”=>”ios8”,  “name”=>”Icon-58.png”),

    array(“width”=> 29,  “height”=>
29,   “path”=>”ios8”,  “name”=>”Icon-29.png”),

  ),

  “IOS”=>array 

  (

    array(“width”=> 180, “height”=>
180,”path”=>”ios”,   “name”=>”Icon-60@3x.png”),

    array(“width”=> 120, “height”=>
120,”path”=>”ios”,   “name”=>”Icon-60@2x.png”),

    array(“width”=> 152, “height”=>
152,”path”=>”ios”,   “name”=>”Icon-76@2x.png”),

    array(“width”=> 76,  “height”=>
76,   “path”=>”ios”,   “name”=>”Icon-76.png”), 

    array(“width”=> 144, “height”=>
144,”path”=>”ios”,   “name”=>”Icon-72@2x.png”),

    array(“width”=> 72,  “height”=>
72,   “path”=>”ios”,   “name”=>”Icon-72.png”),

    array(“width”=> 114, “height”=>
114,”path”=>”ios”,   “name”=>”Icon-57@2x.png”),

    array(“width”=> 57,  “height”=>
57,   “path”=>”ios”,   “name”=>”Icon-57.png”), 

    array(“width”=> 100, “height”=>
100,”path”=>”ios”,   “name”=>”Icon-50@2x.png”),

    array(“width”=> 50,  “height”=>
50,   “path”=>”ios”,   “name”=>”Icon-50.png”), 

    array(“width”=> 80,  “height”=>
80,   “path”=>”ios”,   “name”=>”Icon-40@2x.png”),

    array(“width”=> 40,  “height”=>
40,   “path”=>”ios”,   “name”=>”Icon-40.png”),

    array(“width”=> 58,  “height”=>
58,   “path”=>”ios”,   “name”=>”Icon-29@2x.png”),

    array(“width”=> 29,  “height”=>
29,   “path”=>”ios”,   “name”=>”Icon-29.png”),

  )

  ,

  “ANDROID”=>array

  (

    array(“width”=> 144, “height”=>
144,  “path”=>”android/drawable”,     “name”=>”icon.png”),

    array(“width”=> 144, “height”=>
144,  “path”=>”android/drawable-xhdpi”,”name”=>”icon.png”),

    array(“width”=> 72,  “height”=>
72,”path”=>”android/drawable-hdpi”,   “name”=>”icon.png”),

    array(“width”=> 48,  “height”=>
48,”path”=>”android/drawable-mdpi”,   “name”=>”icon.png”),

    array(“width”=> 32,  “height”=>
32,”path”=>”android/drawable-ldpi”,   “name”=>”icon.png”)

  )

);

  

  

foreach($IconArray[“IOS8”]as$key=>$IconType) 

{

  thumb(“icon.png”,$IconType[‘width’],$IconType[‘height’],$IconType[‘path’],$IconType[‘name’]);

}

  

foreach($IconArray[“IOS”]as$key=>$IconType) 

{

  thumb(“icon.png”,$IconType[‘width’],$IconType[‘height’],$IconType[‘path’],$IconType[‘name’]);

}

  

foreach($IconArray[“ANDROID”]as$key=>$IconType) 

{

  thumb(“icon.png”,$IconType[‘width’],$IconType[‘height’],$IconType[‘path’],$IconType[‘name’]);

}

  

?>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图