php 获取图片高宽与宽度代码

 更新时间:2016年11月25日 16:58  点击:2074
在php中要获取图片的高与宽度,php自身带有一个函数getimagesize,他会返回一个数组,数组下标1为图片高度,数组下标0一宽度哦。

//you do not need to alter these functions

 代码如下 复制代码
function getheight($image) {
 $size = getimagesize($image);
 $height = $size[1];
 return $height;
}


//you do not need to alter these functions

 代码如下 复制代码
function getwidth($image) {
 $size = getimagesize($image);
 $width = $size[0];
 return $width;
}
本教程是一款php图片上传然后,把上传的图片生成小图片哦,是一款非常好的文件上传类,如果你正在找类程序可以进来看看
 代码如下 复制代码

function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {
    global $webaddr,$_files,$my;
    $phpv=str_replace('.', '', php_version);
    $filename=$upname;
    $max_file_size = 2147483648;        //上传文件大小限制, 单位byte 2m
    $path_im = $path_dim;               //生成大图保存文件夹路径
    $path_sim = $path_xim;              //缩略图保存文件夹路径
    $simclearly=75;
    $simclearlypng =$phpv>=512?7:75;        //缩略图清晰度0-100,数字越大越清晰,文件尺寸越大
    $smallmark = $smallmark;            //是否生成缩略图(1为加生成,其他为不);
    $dst_sw =$dstsw;                   //定义缩略图宽度,高度我采用等比例缩放,所以只要比较宽度就可以了
    $uptypes=array(
        'image/jpg',
        'image/jpeg',
        'image/png',
        'image/pjpeg',
        'image/gif',
        'image/bmp',
        'image/x-png'
    );

    if (!is_uploaded_file($_files[$filename][tmp_name])) {
        dsetcookie('setok','upload1');
        header("location:111cn.net/profile");
        exit;
    }
    $file = $_files[$filename];
    $pinfo = pathinfo($file["name"]);
    if ($filetype=="null") {
        $filetype = $pinfo['extension'];
    }
    if (!in_array(strtolower($pinfo['extension']),array("jpg","jpeg","png","gif"))) {
        dsetcookie('setok','upload3');
        header("location:111cn.net/profile");
        exit;
    }

    if($max_file_size < $file["size"]) {//检查文件大小
        dsetcookie('setok','upload2');
        header("location:111cn.net/profile");
        exit;
    }
    if(!in_array($file["type"],$uptypes)) { //检查文件类型
        dsetcookie('setok','upload3');
        header("location:111cn.net/profile");
        exit;
    }
    if(!file_exists($path_im)) {
        mkdir($path_im);
    }

    $filename = $file["tmp_name"];
    $im_size = getimagesize($filename);

    $src_w = $im_size[0];
    $src_h = $im_size[1];
    $src_type = $im_size[2];

    $all_path = $path_im.$newname.".".$filetype;//路径+文件名,目前以上传时间命名
    if (file_exists($all_path)) {
        @unlink($all_path);
    }
    if(!move_uploaded_file ($filename,$all_path)) {
        dsetcookie('setok','upload4');
        header("location:111cn.net/profile");
        exit;
    }
    $pinfo = pathinfo($all_path);
    $fname = $pinfo[basename];

    switch($src_type) {//判断源图片文件类型
         case 1://gif
         $src_im = @imagecreatefromgif($all_path);//从源图片文件取得图像
         break;
         case 2://jpg
         $src_im = @imagecreatefromjpeg($all_path);
         break;
         case 3://png
         $src_im = @imagecreatefrompng($all_path);
         break;
         //case 6:
         //$src_im=imagecreatefromwbmp($all_path);
         //break;
         default:
         dsetcookie('setok','upload3');
         header("location:111cn.net/profile");
         exit;
    }

   if($smallmark == 1) {
       if(!file_exists($path_sim)) {//检查缩略图目录是否存在,不存在创建
           mkdir($path_sim);
       }
       if ($smallname) $newname=$smallname;
       $sall_path = $path_sim.$newname.".".$filetype;
       if (file_exists($sall_path)) {
           @unlink($sall_path);
       }
       if($src_w <= $dst_sw) { // 原图尺寸 <= 缩略图尺寸
           if ($dstsh==0)  {
                $dst_sim = @imagecreatetruecolor($src_w,$src_h); //新建缩略图真彩位图
                $sx=$sy=0;
           } else {
                $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图
                $sx=($dstsw-$src_w)/2;
                $sy=($dstsh-$src_h)/2;
           }
           $img = @imagecreatefrompng("images/phbg.png");
           @imagecopymerge($dst_sim,$img,0,0,0,0,$dstsw,$dstsh,100); //原图图像写入新建真彩位图中
           @imagecopymerge($dst_sim,$src_im,$sx,$sy,0,0,$src_w,$src_h,100); //原图图像写入新建真彩位图中
       }

       if($src_w > $dst_sw) { // 原图尺寸 > 缩略图尺寸
           $dst_sh = $dst_sw/$src_w*$src_h;
           if ($dst_sh<$dstsh) {
               $dst_sh=$dstsh;
               $dst_sw=$dst_sh/$src_h*$src_w;
           }
           if ($dstsh==0) {
                $dst_sim = @imagecreatetruecolor($dst_sw,$dst_sh); //新建缩略图真彩位图(等比例缩小原图尺寸)
           } else {
                $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图(等比例缩小原图尺寸)
           }
           @imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //原图图像写入新建真彩位图中
       }

       switch($src_type) {
            case 1:@imagegif($dst_sim,$sall_path,$simclearly);//生成gif文件,图片清晰度0-100
            break;
            case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);//生成jpg文件,图片清晰度0-100
            break;
            case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);//生成png文件,图片清晰度0-100
            break;
            //case 6:
            //imagewbmp($dst_sim,$sall_path);
            break;
       }
       //释放缓存
       @imagedestroy($dst_sim);
    }
    @imagedestroy($src_im);
    return $newname.".".$filetype;
}
?>

本文章里面image就是你要生成的图片地址哦,这是一php按比例生成缩略图代码,只要给你图片他就能生成指定大小的图片哦,并且不变形ekt
 代码如下 复制代码

function resizeimage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imagetype) = getimagesize($image);
 $imagetype = image_type_to_mime_type($imagetype);
 $newimagewidth = ceil($width * $scale);
 $newimageheight = ceil($height * $scale);
 $newimage = imagecreatetruecolor($newimagewidth,$newimageheight);
 switch($imagetype) {
  case "image/gif":
   $source=imagecreatefromgif($image);
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image);
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image);
   break;
   }
 imagecopyresampled($newimage,$source,0,0,0,0,$newimagewidth,$newimageheight,$width,$height);
 
 switch($imagetype) {
  case "image/gif":
     imagegif($newimage,$image);
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newimage,$image,90);
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newimage,$image); 
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
?>

php 图片等比例缩放代码是一款根据用户上传的图片来指定比例大小的图片,原理很简单就是算出图片大小进等比例就行了,第二款生成小图是固定图片大小,但是如何图片小于设定的图片就填白,这是一个好方法哦。
 代码如下 复制代码

<?php
header("content-type:image/jpeg");
$filename = hsdir.'/mljntc2p.jpg';
$im = imagecreatefromjpeg($filename);
$h=imagesy($im);//获得目标图片高度
$new_img_width  = 257;
$new_img_height = 522;

$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagefilledrectangle($newim,0,$h,$new_img_width,$new_img_height,$white);
//填充  目标图片高度作为起驶y坐标 以指定截取宽高为结束坐标
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

代码二

 代码如下 复制代码

<?php
header("content-type:image/jpeg");
$filename = 'myface.jpg';
$im = imagecreatefromjpeg($filename);
$new_img_width  = 80;
$new_img_height = 150;
$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

这款关于php 中文汉字验证码生成程序其实很简单的,生成中文汉字验证码需要字体来支持了,下面我们用的arial.ttf来支持生成,否则生成中文图片验证码会出乱码的。
 代码如下 复制代码

class securecode
{
    private static $instance=null;
    private $code = '';
    private $fontfile;
    private $validate;
    private $image;
    private $specialadd = 'special string for securecode';
    private $codeexpire=86400;
    private $codecookiename='secure_code';

    /**
     * 构造方法
     */
    private function securecode()
    {
        $this->fontfile = dirname( __file__ ) . '/arial.ttf';
    }

    private function __construct()
    {
        $this->securecode();
    }
   
    public static function getinstance()
    {
        if (self::$instance==null)
            self::$instance=new self();
       
        return self::$instance;
    }

    /**
     * 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
     * @param $fontfile 文件路径
     * @return void
     */
    function loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

    /**
     * 图片输出方法,在执行本方法前程序不应该有任何形式的输出
     * @return void;
     */
    function stroke()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

    /**
     * 图片保存方法
     * @param $filename 保存路径
     * @return void
     */
    function save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
     * 验证码验证方法
     * @param $input 要验证的字符串,即用户的输入内容
     * @return boolean 验证结果
     */
    function verify($input)
    {
        $input=strtolower($input);
        $targetcode=$this->authcode($input);
        $code=$this->getcookie();
        if (empty($code)||$code!=$targetcode)
            $result= false;
        else
            $result=true;
        $_cookie[$this->codecookiename]='';
        setcookie ( $this->codecookiename, '', - 1 );
        return $result;
    }

    /**
     * 图片创建方法
     * @return void;
     */
    function createimage()
    {
        $this->randcode();
       
        $size = 30;
        $width = 90;
        $height = 35;
        $degrees = array (
            rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
        );
       

        for ($i = 0; $i < 4; ++$i)
        {
            if (rand() % 2);
            else $degrees[$i] = -$degrees[$i];
        }
       
        $this->image = imagecreatetruecolor( $size , $size );
        $this->validate = imagecreatetruecolor( $width , $height );
        $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
        $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
        imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
       
        for ($i = 0; $i < 4; ++$i)
        {
            $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
           
            if ($temp[2] > 60) $temp[2] = 60;
           
            $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
            $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
        }
       
        for ($i = 0; $i < 200; ++$i)
        {
            $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
            imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
        }
       
        $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
       
        if ($temp[2] < 200) $temp[2] = 255;
       
        $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
        $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
       
        self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
       
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        putenv( 'gdfontpath=' . realpath( '.' ) );
       
        // name the font to be used (note the lack of the .ttf extension
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
       
        $this->image = imagerotate( $this->image , $degrees[0] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
        $this->image = imagerotate( $this->image , $degrees[1] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
        $this->image = imagerotate( $this->image , $degrees[2] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
        $this->image = imagerotate( $this->image , $degrees[3] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
        imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
    }

 

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • PHP成员变量获取对比(类成员变量)

    下面本文章来给大家介绍在php中成员变量的一些对比了,文章举了四个例子在这例子中分别对不同成员变量进行测试与获取操作,下面一起来看看。 有如下4个代码示例,你认...2016-11-25
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php 获取用户IP与IE信息程序

    php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • php获取一个文件夹的mtime的程序

    php获取一个文件夹的mtime的程序了,这个就是时间问题了,对于这个问题我们来看小编整理的几个例子,具体的操作例子如下所示。 php很容易获取到一个文件夹的mtime,可以...2016-11-25
  • 如何获取网站icon有哪些可行的方法

    获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon,所以此方法很多情况都不可用。 更好的办法是通过google提供的服务来实现:http://www.google.com/s2/favi...2014-06-07
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • jquery如何获取元素的滚动条高度等实现代码

    主要功能:获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 :$(window).width(); 获取页面的文档高度 $(document).height(); 获取页面的文档宽度 :$(document).width();...2015-10-21
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • jquery获取div距离窗口和父级dv的距离示例

    jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
  • Jquery 获取指定标签的对象及属性的设置与移除

    1、先讲讲JQuery的概念,JQuery首先是由一个 America 的叫什么 John Resig的人创建的,后来又很多的JS高手也加入了这个团队。其实 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用简单的一些代...2014-05-31
  • java 画pdf用itext调整表格宽度、自定义各个列宽的方法

    这篇文章主要介绍了java 画pdf用itext调整表格宽度、自定义各个列宽的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • C#获取字符串后几位数的方法

    这篇文章主要介绍了C#获取字符串后几位数的方法,实例分析了C#操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25