zoukankan      html  css  js  c++  java
  • PHP图像处理之在原图像处理

    处理原有的图像
            图片处理,缩放,裁剪,翻转,旋转,透明,锐化等图片操作
            一.创建图片资源
                imagecreatetruecolor(width,height);
                gif   jpg    png

                imagecreatefromgif(图片名称);
                imagecreatefrompng(图片名称);
                imagecreatefromjpeg(图片名称);

                画出各种图形(圆形、矩形、线段、文字)

                imagegif(图片位置);
                imagepng(图片位置);
                imagejpeg(图片位置);

                imagedestroy(图片资源);

    <?php
    //利用原有的图片创建图片资源
    $img=imagecreatefromgif("./images/map.gif");
    //设置颜色
    $red=imagecolorallocate($img, 255, 0, 0);
    
    //画线
    imageline($img,0, 0, 100, 100, $red);
    
    //画圆
    imageellipse($img, 200, 100, 100, 100, $red);
    
    //保存图片
    imagegif($img,"./images/map2.gif");
    
    //释放资源
    imagedestroy($img);
    ?>


            二.获取图片的属性
                imagesx(res);  取得图片的宽度
                imagesy(res);  取得图片的高度

                getimagesize(图片名称);   返回数组,0==width,1==height,2==type

    <?php
    $img=imagecreatefromgif("./images/map.gif");
    $red=imagecolorallocate($img, 255, 0, 0);
    
    //输出图片的高度和宽度
    echo ''.imagesx($img)."<br>";
    echo 'height:'.imagesy($img)."<br>";
    
    echo '<pre>';
    //取得图像大小
    print_r(getimagesize("./images/map.gif"));
    echo '</pre>';
    
    imagedestroy($img);
    ?>



            三.透明处理
                png jpeg 透明都正常,只有gif不正常

                imagecolortransparent();    将某个颜色定义为透明色
                imagecolorstotal();   取得一幅图像的调色板中颜色的数目
                imagecolorsforindex();   取得某索引的颜色

    <?php
    function thumn($background,$newWidth,$newHeight,$newFile){
        //取出图片的宽高
        list($width,$height)=getimagesize($background);
        //求的而缩放的宽高
        if ($newWidth && ($width < $height)){
            $newWidth=($newHeight/$height)*$width;
        } else {
               $newHeight=($newWidth/$width)*$height;
        }
        //创建缩放后图片的画布
        $new=imagecreatetruecolor($newWidth,$newHeight);
        $img=imagecreatefromgif($background);
    
        //imagecolortransparent();    将某个颜色定义为透明色
        $otsc=imagecolortransparent($img);
        //imagecolorstotal();   取得一幅图像的调色板中颜色的数目
        if($otsc>=0 && $otsc<imagecolorstotal($img)){
            //imagecolorsforindex();   取得某索引的颜色
            $tran=imagecolorsforindex($img, $otsc);
            //新的颜色
            $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
            //新的颜色设为缩放图片的颜色
            imagefill($new, 0, 0, $newt);
            //将缩放图片的颜色设为背景透明
            imagecolortransparent($new,newt);
        }
        //缩放原图片
        imagecopyresampled($new, $img,0,0,0,0, $newWidth, $newHeight, $width, $height);
        //保存缩放后的图片
        imagegif($new,$newFile);
        //释放资源
        imagedestroy($new);
        imagedestroy($img);
    }
    
    
    thumn("images/map.gif",200,200,"./images/map3.gif");
    ?>


            四.图片的裁剪
                imagecopyresized();    拷贝部分图像并调整大小
                imagecopyresampled();    重采样拷贝部分图像并调整大小

    <?php
    function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
    
        $back=imagecreatefromjpeg($background);
    
        $new=imagecreatetruecolor($cut_width, $cut_height);
    
        imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
    
        imagejpeg($new, $location);
    
        imagedestroy($new);
        imagedestroy($back);
    }
    
    cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");
    ?>


                五、加水印(文字, 图片)
         
                    imagettftext();
                     imagecopy();

    <?php
        function mark_text($background, $text, $x, $y){
            $back=imagecreatefromjpeg($background);
    
            $color=imagecolorallocate($back, 0, 255, 0);
        
            imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);
    
            imagejpeg($back, "./images/hee7.jpg");
    
            imagedestroy($back);
        }
    
        mark_text("./images/hee.jpg", "细说PHP", 150, 250);
    
        function mark_pic($background, $waterpic, $x, $y){
            $back=imagecreatefromjpeg($background);
            $water=imagecreatefromgif($waterpic);
            
    
            $w_w=imagesx($water);
            $w_h=imagesy($water);
    
            imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
    
            imagejpeg($back,"./images/hee8.jpg");
    
            imagedestroy($back);
            imagedestroy($water);
        }
    
        mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200)
    ?>


     
                  六、图片旋转
     
                    imagerotate -- 用给定角度旋转图像

    <?php
        $back=imagecreatefromjpeg("./images/hee.jpg");
    
        $new=imagerotate($back, 45, 0);
    
        imagejpeg($new, "./images/hee9.jpg");
    ?>



                七、图片翻转
            
                    沿Y轴

                    沿X轴

    <?php
        function turn_y($background, $newfile){
            $back=imagecreatefromjpeg($background);
    
            $width=imagesx($back);
            $height=imagesy($back);
    
            $new=imagecreatetruecolor($width, $height);
    
            for($x=0; $x < $width; $x++){
                imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
            }
    
            imagejpeg($new, $newfile);
    
            imagedestroy($back);
            imagedestroy($new);
        }
    
        function turn_x($background, $newfile){
            $back=imagecreatefromjpeg($background);
    
            $width=imagesx($back);
            $height=imagesy($back);
    
            $new=imagecreatetruecolor($width, $height);
    
            for($y=0; $y < $height; $y++){
                imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
            }
    
            imagejpeg($new, $newfile);
    
            imagedestroy($back);
            imagedestroy($new);
        }
    
        turn_y("./images/hee.jpg", "./images/hee11.jpg");
        turn_x("./images/hee.jpg", "./images/hee12.jpg");
    ?>


                八、锐化
                    imagecolorsforindex()
                    imagecolorat()

    <?php
    function sharp($background, $degree, $save){
        $back=imagecreatefromjpeg($background);
    
        $b_x=imagesx($back);
        $b_y=imagesy($back);
    
        $dst=imagecreatefromjpeg($background);
        for($i=0; $i<$b_x; $i++){
            for($j=0; $j<$b_y; $j++){
                $b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));    
                $b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));
    
                $r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));
                $g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"]));
                $b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"]));
    
                $r=min(255, max($r, 0));
                $g=min(255, max($g, 0));
                $b=min(255, max($b, 0));
    
                if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){
                    $d_clr=Imagecolorallocate($dst, $r, $g, $b);
                }
    
                imagesetpixel($dst, $i, $j, $d_clr);
            }
        
        }
        imagejpeg($dst, $save);
        imagedestroy($back);
        imagedestroy($dst);
    }
    
    sharp("./images/hee.jpg", 20, "./images/hee13.jpg");
    ?>
  • 相关阅读:
    jeecg 笔记之 自定义显示按钮 (exp 属性)
    jeecg 笔记之 自定义word 模板导出(一)
    jeecg 默认为空的字段值是如何被填充的?
    算法题——立方体的体对角线穿过多少个正方体?
    借用Snippet插件美化博客中的代码
    用PS设计等高线效果的背景图片
    算法题——投篮比赛获胜概率问题
    计算机中的颜色XIV——快速变换颜色的V分量
    算法实践——Twitter算法面试题(积水问题)的线性时间解法
    UI设计实战篇——利用Bootstrap框架制作查询页面的界面
  • 原文地址:https://www.cnblogs.com/Y-HKL/p/5468869.html
Copyright © 2011-2022 走看看