首页 > 编程技术 > php

php图像处理常用函数与实例

发布时间:2016-11-25 16:58

 代码如下 复制代码

<?php教程
header("Content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");

// 复制图像www.111cn.net
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);

// 输出jpeg图像
imagejpeg($imagen1);

//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);

?>

获取图片属性代码

 代码如下 复制代码

<?php
$info = getimagesize("imagen2.jpg");
print_r($info);

?>

生成png图片的php代码

 代码如下 复制代码

<?php
//PNG格式图像处理函数
function Loadpng ($imgname) {
    $im = @ImageCreateFromPNG ($imgname);
    if (!$im) {    //载入图像失败                     
        $im = ImageCreate (400, 30);     
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
       ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
       ImageString($im, 4, 5, 5, "Error loading: $imgname", $tc);
    }
    return $im;
 }
 $imgPng=Loadpng("./karte.png");
   /* 输出图像到浏览器 */
 header("Content-type: image/png");
 imagePng($imgPng);
 ?>

这款php生成图片与验证码图片生成原理代码,是由php gd库来支持,如果你的系统不能创建图片就把gd.dll前面的;去再,重起apache,如果是iis重起iis就OK了。

<?

 代码如下 复制代码
$w?$RESIZEWIDTH=$w:$RESIZEWIDTH=400;// 生成图片的宽度
$h?$RESIZEHEIGHT=$h:$RESIZEHEIGHT=400;// 生成图片的高度
function ResizeImage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
        if($maxwidth && $width > $maxwidth){
            $widthratio = $maxwidth/$width;
            $RESIZEWIDTH=true;//www.111cn.net
        }
        if($maxheight && $height > $maxheight){
            $heightratio = $maxheight/$height;
            $RESIZEHEIGHT=true;
        }
        if($RESIZEWIDTH && $RESIZEHEIGHT){
            if($widthratio < $heightratio){
                $ratio = $widthratio;
            }else{
                $ratio = $heightratio;
            }
        }elseif($RESIZEWIDTH){
            $ratio = $widthratio;
        }elseif($RESIZEHEIGHT){
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if(function_exists("imagecopyresampled")){
              $newim = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        ImageJpeg ($newim,$name);
        ImageDestroy ($newim);
    }else{
        ImageJpeg ($im,$name);
    }
}
 代码如下 复制代码
if($_FILES['uploadfile']['size']){
    if($_FILES['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_FILES['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('www.111cn.net.jpg');
        }
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,'bbs.jpg');
        ImageDestroy ($im);
  
    }
}
//$uploadfile="bbs.jpg";

?>

创建图片后记得用ImageDestroy 清空内存哦。

关于php生成图形验证码我们讲过很多,这是一个非常简单的适合于php入门者的教程,一个本的用php生成验证码的实例代码。

<?php
$height = 300;
$width = 300;
//创建背景图
$im = ImageCreateTrueColor($width, $height);
//分配颜色
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
//绘制颜色至图像中
ImageFill($im, 0, 0, $blue);
//绘制字符串:Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
//输出图像,定义头
Header ('Content-type: image/png');
//将图像发送至浏览器
ImagePng($im);
//清除资源
ImageDestroy($im);
?>

function image3($length=4,$type='png',$width=180,$height=60,$fontface='fpnf.ttf',$verifyName='verify') {
        $code = $this->rand_string($length,4);
        $width = ($length*25)>$width?$length*25:$width;
        $authCode = new Zend_Session_Namespace('Auth_Code');
        $authCode->imagecode = $randval;
       
        $im=imagecreatetruecolor($width,$height);
        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //边框色
        $bkcolor=imagecolorallocate($im,250,250,250);
        imagefill($im,0,0,$bkcolor);
        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
        // 干扰
        for($i=0;$i<15;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
        }
        for($i=0;$i<255;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
        }
        if(!is_file($fontface)) {
            $fontface = dirname(__FILE__)."/".$fontface;
        }
        for($i=0;$i<$length;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
            $codex= substr($code,$i,1);
            imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
        }
        $this->output($im,$type);
    }
   
    function output($im,$type='png')
    {
        header("Content-type: image/".$type);
        $ImageFun='Image'.$type;
        $ImageFun($im);
        imagedestroy($im);
    }

<?php教程
 
class My_Lib_simpleimage {
 var $image;
 var $image_type;
 function load($filename) { 
    //$filename 文件的临时文件名 $_FILES['name']['tmp_name']
  $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {
  $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {
  $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {
  $this->image = imagecreatefrompng($filename);
  }
 }
 
 function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  if( $image_type == IMAGETYPE_JPEG ) {
  imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {
  imagegif($this->image,$filename);
  } elseif( $image_type == IMAGETYPE_PNG ) {
  imagepng($this->image,$filename);
  }
  if( $permissions != null) {
  chmod($filename,$permissions);
  }
 }
 function output($image_type=IMAGETYPE_JPEG) {
  if( $image_type == IMAGETYPE_JPEG ) {
  imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {
  imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {
  imagepng($this->image);
  }
 }
 function getWidth() {
  return imagesx($this->image);
 }
 function getHeight() {
  return imagesy($this->image);
 }
 
 #设置缩略图片高度
 function resizeToHeight($height) {
  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
 }
 #设置缩略图片宽度
 function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
 }
 function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
 }
 function resize($width,$height) {
  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
 }
}
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: