首页 >后端开发 >php教程 > 正文

php基础学习:图像处理

原创2018-05-22 13:49:250683
在学习php过程中会遇到图像处理的情况,本篇将会介绍图像处理的方法。

<!-- 图像处理 -->

<?php 
//     图片处理gd2配置文件修改 
?> 
   
<!-- 用图片处理函数画一张图 --> 
<?php 
//     $img = imagecreatetruecolor(500, 500); 
//     $red = imagecolorallocate($img, 255, 0, 0); 
//     $green = imagecolorallocate($img, 0, 255, 0); 
//     $blue = imagecolorallocate($img, 0, 0, 255); 
//     $pur = imagecolorallocate($img, 255, 0, 255); 
//     $yellow = imagecolorallocate($img, 121, 72, 0); 
       
//     imagefilledrectangle($img, 0, 0, 500, 500, $green); 
//     imageline($img, 0, 0, 500, 500, $red); 
//     imageline($img, 500, 0, 0, 500, $blue); 
       
//     imagefilledellipse($img, 250, 250, 200, 200, $yellow); 
       
//     imagefilledellipse($img, 200, 200, 300, 300, $blue); 
       
//     imagejpeg($img, 'haha.jpg'); 
//     echo "<img src='haha.jpg'>"; 
//     imagedestroy($img); 
?> 
   
<!-- 开发验证码(生成验证码) --> 
<?php 
    check_code(); 
    function check_code($width = 100, $height = 50, 
        $num = 4, $type = 'jpeg'){ 
        $img = imagecreate($width, $height); 
        $string = ''; 
        for ($i = 0;$i < $num; $i++){ 
            $rand = mt_rand(0, 2); 
            switch ($rand) { 
                case 0: 
                    $ascii = mt_rand(48, 57); 
                    break; 
                case 1: 
                    $ascii = mt_rand(65, 90); 
                    break; 
                case 2: 
                    $ascii = mt_rand(97, 122); 
                    break; 
            } 
            $string .= sprintf('%c', $ascii); 
        } 
        imagefilledrectangle($img, 0, 0, $width, $height, randBg($img)); 
        for ($i = 0;$i < 50; $i++){ 
            imagesetpixel($img, mt_rand(0, $width),  
                mt_rand(0, $height), randPix($img)); 
        } 
        for ($i = 0;$i < $num;$i++){ 
            $x = floor($width/$num) * $i + 2; 
            $y = mt_rand(0, $height - 15); 
               
            imagechar($img, 5, $x, $y, $string[$i], randPix($img)); 
        } 
           
        $func = 'image' . $type; 
        $header = 'Content-type:image/'.$type; 
        if (function_exists($func)) { 
            header($header); 
            $func($img); 
        }else { 
           echo '图片类型不支持';  
        } 
        imagedestroy($img); 
        return $string; 
    } 
    function randBg($img){ 
        return imagecolorallocate($img, mt_rand(130, 255), 
            mt_rand(130, 255), mt_rand(130, 255)); 
    } 
    function randPix($img){ 
        return imagecolorallocate($img, mt_rand(0, 120),  
            mt_rand(0, 120), mt_rand(0, 120)); 
    } 
?>  
<!-- 图像缩放和剪裁技术 --> 
<?php 
    $image = imagecreatefrompng('fbb.png'); 
       
    $percent = 0.1; 
    list($width, $height) = getimagesize('fbb.png'); 
       
    $new_width = $width * $percent; 
    $new_height = $height * $percent; 
       
    $new_image = imagecreatetruecolor($new_width, $new_height); 
    imagecopyresampled($new_image, $image, 0, 0, 
        0, 0, $new_width, $new_height, $width, $height); 
    header('content-type:image/jpeg'); 
    imagejpeg($new_image); 
?> 
<!-- 图片水印处理 --> 
<?php 
    $dst = imagecreatefrompng('https://img.php.cn/upload/course/000/ 
        000/002/5833ebba648cf229.png'); 
    $src = imagecreatefrompng('https://img.php.cn/ 
        upload/course/000/000/002/5833ebe90cc11285.png'); 
    $dst_info = getimagesize('5833ebba648cf229.png'); 
    $src_info = getimagesize('5833ebe90cc11285.png'); 
       
    $dst_x = $dst_info[0] - $src_info[0]; 
    $dst_y = $dst_info[1] - $src_info[1]; 
    imagecopymerge($dst, $src, $dst_x, $dst_y, 0, 0,  
        $src_info[0], $src_info[1], 100); 
    header('Content-type:image/png'); 
    imagepng($dst); 
    imagedestroy($dst); 
    imagedestroy($src); 
?>   
<!-- 做一个智能的图片水印函数 --> 
<?php        
?>

本文讲解了图像处理的相关方法,更多相关内容请关注php中文网。

相关推荐:

通过cURL来做小偷程序

php会话管理和控制

php基础学习:错误处理

以上就是php基础学习:图像处理的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:图像处理 php 基础
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    推荐视频教程
  • javascript初级视频教程javascript初级视频教程
  • jquery 基础视频教程jquery 基础视频教程
  • 视频教程分类