php基礎五之影像處理

不言
發布: 2023-03-24 16:28:01
原創
1724 人瀏覽過

這篇文章介紹的內容是關於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, &#39;haha.jpg&#39;);
//     echo "<img src=&#39;haha.jpg&#39;>";
//     imagedestroy($img);
?>

<!-- 开发验证码(生成验证码) -->
<?php
    check_code();
    function check_code($width = 100, $height = 50,
        $num = 4, $type = &#39;jpeg&#39;){
        $img = imagecreate($width, $height);
        $string = &#39;&#39;;
        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(&#39;%c&#39;, $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 = &#39;image&#39; . $type;
        $header = &#39;Content-type:image/&#39;.$type;
        if (function_exists($func)) {
            header($header);
            $func($img);
        }else {
           echo &#39;图片类型不支持&#39;; 
        }
        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(&#39;fbb.png&#39;);
    
    $percent = 0.1;
    list($width, $height) = getimagesize(&#39;fbb.png&#39;);
    
    $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(&#39;content-type:image/jpeg&#39;);
    imagejpeg($new_image);
?>

<!-- 图片水印处理 -->
<?php
    $dst = imagecreatefrompng(&#39;https://img.php.cn/upload/course/000/
        000/002/5833ebba648cf229.png&#39;);
    $src = imagecreatefrompng(&#39;https://img.php.cn/
        upload/course/000/000/002/5833ebe90cc11285.png&#39;);
    $dst_info = getimagesize(&#39;5833ebba648cf229.png&#39;);
    $src_info = getimagesize(&#39;5833ebe90cc11285.png&#39;);
    
    $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(&#39;Content-type:image/png&#39;);
    imagepng($dst);
    imagedestroy($dst);
    imagedestroy($src);
?>

<!-- 做一个智能的图片水印函数 -->
<?php
    
?>
登入後複製

相關推薦:

php基礎四

php基礎三


#

以上是php基礎五之影像處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!