PHP图像处理之在原图像处理

摘要:
˃II获取图像属性imagesx;获取图像的宽度;获取图片的高度getimagesize;返回数组,0==宽度,1==高度,2==类型IV图像剪辑imagecopyresized();复制一些图像并调整imagecopyrasampled()的大小;重新采样和复制一些图像并调整大小˂?

处理原有的图像
        图片处理,缩放,裁剪,翻转,旋转,透明,锐化等图片操作
        一.创建图片资源
            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");
?>

免责声明:文章转载自《PHP图像处理之在原图像处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇禁用SQL Server的SSRS日志追踪-防止Windows 磁盘被占满socket 客户端和服务端思考下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

css实现五颜六色的按钮导航

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>css实现五颜六色的按钮导航_网页代码站(www.webdm.cn)</title&...

python3下pygame显示中文的设置

1.先看代码: import pygame from pygame.locals import * def main(): pygame.init() screen = pygame.display.set_mode((1000, 450)) #窗口的大小 pygame.display.set_caption('pygame程序的...

VSCode——自定义VSCode背景图片

本文转载自https://blog.csdn.net/yukinoai/article/details/845649491.以管理员身份运行VS Code,安装background插件2.打开setting,在搜索框中输入background,选择扩展中的plugin background,选择在setting.json中编辑3.在用户设置中输入以下代码,...

css3中动画animation的应用

<!DOCTYPE html> <html> <head> <style> /*@-webkit-keyframes anim1 { // 规定动画。 0% { Opacity: 0; font-size: 12px; } 100% { Opacity: 1; font-size: 34px;...

CSS3实现3D地球自转行星公转

截图效果:实际效果是动态的:地球自西向东自转,行星绕着地球公转,轨道也会转动 HTML页面代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CSS3星系轨道3D</title> &l...

Android开发之玩转FlexboxLayout布局

在这之前,我曾认真的研究过鸿洋大神的Android 自定义ViewGroup 实战篇 -> 实现FlowLayout,按照大神的思路写出了一个流式布局,所有的东西都是难者不会会者不难,当自己能自定义流式布局的时候就会觉得这东西原来很简单了。如果各位小伙伴也看过那篇文章的话,应该知道自定义流式布局还是非常麻烦的,不过Google今年开源了新的容器,就是...