搜尋
首頁php教程php手册php随笔1-php图片处理,
php随笔1-php图片处理,Jun 13, 2016 am 08:53 AM
php內容圖片處理知識

php随笔1-php图片处理,

php图片处理的知识内容

upload_image.php:

<span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span>
<span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span>></span>
<span><</span><span>head</span><span>></span>
<span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html; charset=gb2312"</span> <span>/></span>
 <span><</span><span>title</span><span>></span>图片上传页<span></</span><span>title</span><span>></span>
 <span><</span><span>style </span><span>type</span><span>="text/css"</span><span>></span><span>
.file-box</span><span>{</span><span> position</span><span>:</span><span>relative</span><span>;</span><span>width</span><span>:</span><span>340px</span><span>}</span><span> 
.txt</span><span>{</span><span> height</span><span>:</span><span>22px</span><span>;</span><span> border</span><span>:</span><span>1px solid #cdcdcd</span><span>;</span><span> width</span><span>:</span><span>200px</span><span>;</span><span>border-right</span><span>:</span><span>none</span><span>;</span><span>}</span><span> 
.btn</span><span>{</span><span> background-color</span><span>:</span><span>#FFF</span><span>;</span><span> border</span><span>:</span><span>1px solid #CDCDCD</span><span>;</span><span>height</span><span>:</span><span>26px</span><span>;</span><span> width</span><span>:</span><span>70px</span><span>;</span><span>}</span><span> 
.file</span><span>{</span><span> position</span><span>:</span><span>absolute</span><span>;</span><span> top</span><span>:</span><span>0</span><span>;</span><span> right</span><span>:</span><span>80px</span><span>;</span><span> height</span><span>:</span><span>24px</span><span>;</span><span> filter</span><span>:</span><span>alpha(opacity:0)</span><span>;</span><span>opacity</span><span>:</span><span> 0</span><span>;</span><span>width</span><span>:</span><span>260px </span><span>}</span>
 <span></</span><span>style</span><span>></span>
 <span><</span><span>script  </span><span>type</span><span>="text/javascript"</span> <span>></span>
 <span>//</span><span>JS获取路径 ---成功</span>
    <span>function</span><span> change()<br />  {
          </span><span>var</span><span> pic</span><span>=</span><span>document.getElementById(</span><span>'</span><span>pic</span><span>'</span><span>);
          </span><span>var</span><span> file</span><span>=</span><span>document.getElementById(</span><span>'</span><span>fileField</span><span>'</span><span>);
          pic.src</span><span>=</span><span>file.value;
          </span><span>if</span><span>(file.files){
            </span><span>if</span><span>(file.files.item(</span><span>0</span><span>)){
            </span><span>//</span><span>src = window.URL.createObjectURL(fileObj.files[0]);</span>
<span>            url</span><span>=</span><span>window.URL.createObjectURL(file.files.item(</span><span>0</span><span>));  
             document.getElementById(</span><span>'</span><span>textfield</span><span>'</span><span>).value </span><span>=</span><span> pic.src ;
             pic.src</span><span>=</span><span>url;
            }
          }
   } 
 </span><span></</span><span>script</span><span>></span> 
<span></</span><span>head</span><span>></span>

<span><</span><span>body</span><span>></span>
<span><</span><span>form </span><span>action</span><span>="check_image.php"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span>
            <span><</span><span>table </span><span>border</span><span>="1"</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>上传人名称:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>='txt' </span><span>name</span><span>="username"</span> <span>/></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>*上传图片:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>div </span><span>class</span><span>="file-box"</span><span>><</span><span>input </span><span>type</span><span>='text' </span><span>name</span><span>='textfield' </span><span>id</span><span>='textfield' </span><span>class</span><span>='txt' </span><span>/><</span><span>input </span><span>type</span><span>='button' </span><span>class</span><span>='btn' </span><span>value</span><span>='浏览...' </span><span>/><</span><span>br </span><span>/></span>
                        <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="uploadfile"</span><span> class</span><span>="file"</span><span> id</span><span>="fileField"</span><span> size</span><span>="28"</span><span> onchange</span><span>="change();"</span> <span>/></span> 
                        <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> id</span><span>="theFilePath"</span><span> name</span><span>="theFilePath"</span><span> value</span><span>=""</span><span>></span>
                    <span></</span><span>div</span><span>></span>
                    <span></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td </span><span>colspan</span><span>="2"</span><span>></span>
                        <span><</span><span>small</span><span>><</span><span>em</span><span>></span> * 可接受的图像格式:GIF,JPG、JPEG和PNG。上传后图片会变为1280X960大小。<span></</span><span>em</span><span>></</span><span>small</span><span>></span>
                    <span></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>图片标题:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>='txt' </span><span>name</span><span>="caption"</span><span>/></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td </span><span>colspan</span><span>="2"</span><span> style</span><span>="text-align:center;"</span><span>></span>
                        <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="Upload"</span><span>  class</span><span>='btn' </span><span>/></span>
                    <span></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
            <span></</span><span>table</span><span>></span>
            <span><</span><span>img </span><span>src</span><span>=""</span><span>  name</span><span>="pic"</span><span> id</span><span>="pic"</span> <span>/></span> <span><</span><span>br </span><span>/></span>
        <span></</span><span>form</span><span>></span>
<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

 

check_image.php   集合图片上传,MySQL数据库保存,图片放大至1280X960,图片加文字,图片PS处理,图片缩略图,图片打LOGO水印。功能集合强大。

<?<span>php     
    </span><span>//</span><span>session</span>
    <span>session_start</span><span>();
    </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
    {
            </span><span>if</span>(<span>$_FILES</span>['uploadfile']['error'] !=<span> UPLOAD_ERR_OK)
            {
                </span><span>switch</span>(<span>$_FILES</span>['uploadfile']['error'<span>])
                {
                    </span><span>case</span> UPLOAD_ERR_INI_SIZE: <span>//</span><span>其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值</span>
                        <span>die</span>('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_FORM_SIZE: <span>//</span><span>其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值</span>
                        <span>die</span>('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_PARTIAL: <span>//</span><span>其值为 3,文件只有部分被上传</span>
                        <span>die</span>('文件只有部分被上传'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_NO_FILE: <span>//</span><span>其值为 4,没有文件被上传</span>
                        <span>die</span>('没有文件被上传'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_NO_TMP_DIR: <span>//</span><span>其值为 6,找不到临时文件夹</span>
                        <span>die</span>('找不到临时文件夹'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_CANT_WRITE: <span>//</span><span>其值为 7,文件写入失败</span>
                        <span>die</span>('文件写入失败'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_EXTENSION: <span>//</span><span>其他异常</span>
                        <span>die</span>('其他异常'<span>);
                    </span><span>break</span><span>;
                }
            }
        </span><span>//</span><span>判断图片的后缀</span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMAGETYPE_GIF:
                <span>$ext</span> = '.gif'<span>;
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_JPEG:
                <span>$ext</span> = '.jpg'<span>;
            </span><span>break</span><span>;    
            </span><span>case</span> IMAGETYPE_PNG:
                <span>$ext</span> = '.png'<span>;
            </span><span>break</span><span>;    
            </span><span>default</span>    :
                <span>die</span>('您上传的文件不支持的文件类型。'<span>);
        }
        </span><span>//</span><span>关键位置-取出文件后缀</span>
        <span>$_SESSION</span>['ext'] = <span>$ext</span><span>;
    }
    
    </span><span>/*</span><span>
    //改变图像的大小为1280x960的方法
    function resizepic($imgsrc,$imagename,$toimgwidth,$toimgheigth)
    {
        //$imgsrc:图片路径 $imgname: 图片保存的名称 $toimgwidth: 图片最终的宽度 $toimgheigth: 图片最终的高度
        $arr = getimagesize($imgsrc);
        $imgWidth =  $toimgwidth;
        $imgHeigth =  $toimgheigth;
        if($_SESSION['ext'] == '.jpg')
        {
             header('Content-Type:image/jpeg');
             $imgSrc = imagecreatefromjpeg($imgsrc);
        }
        if($_SESSION['ext'] == '.png')
        {
            header('Content-Type:image/png');
            $imgSrc = imagecreatefrompng($imgsrc);
        }
        if($_SESSION['ext'] == '.gif')
        {
            header('Content-Type:image/gif');
            $imgSrc = imagecreatefromgif($imgsrc);
        }
        
        $image = imagecreatetruecolor($imgWidth, $imgHeigth);
        imagecopyresampled($image,$imgSrc,0,0,0,0,$imgWidth,$imgHeigth,$arr[0],$arr[1]);
        if($_SESSION['ext'] == '.jpg')
        {
            imagejpeg($image,$dir.'/'.$imagename);
        }
        if($_SESSION['ext'] == '.png')
        {
            imagepng($image,$dir.'/'.$imagename);
        }
        if($_SESSION['ext'] == '.gif')
        {
            imagegif($image,$dir.'/'.$imagename);
        }
        //销毁由url生成的图片
        imagedestroy($image);
    }
    </span><span>*/</span>
    

    <span>//</span><span>创建moviesite数据库--MySQL</span>
    <span>$coon</span> = <span>mysql_connect</span>("localhost","root","lifu") or <span>die</span>('不能连接到数据库'<span>);
    </span><span>if</span> (!<span>$coon</span><span>)
      {
          </span><span>die</span>('无法连接: ' . <span>mysql_error</span><span>());
      }
    
    </span><span>if</span> (<span>mysql_query</span>("CREATE DATABASE if not exists moviesite",<span>$coon</span>))    <span>//</span><span>如果没有就创建数据库</span>
<span>      {
          </span><span>echo</span> "数据库创建成功<br>"<span>;
      }
    </span><span>else</span><span>
      {
          </span><span>echo</span> "数据库创建错误: " . <span>mysql_error</span><span>();
      }
    </span><span>//</span><span> 创建images表</span>
    <span>mysql_select_db</span>("moviesite", <span>$coon</span><span>);
    </span><span>//</span><span>如果没有就创建表</span>
    <span>$sql</span> = "<span>CREATE TABLE if not exists images                       
    (
        image_id int NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(image_id),
        image_caption varchar(15), 
        image_username varchar(15),
        image_filename varchar(15),
        image_date  datetime
    )</span>"<span>;
    
    </span><span>mysql_query</span>(<span>$sql</span>,<span>$coon</span><span>);
    </span><span>mysql_close</span>(<span>$coon</span><span>);
    
    </span><span>//</span><span>修改图片效果</span>
    <span>$db</span> = <span>mysql_connect</span>('localhost','root','lifu') or <span>die</span>('不能连接到数据库'<span>);
    </span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));

    </span><span>//</span><span>上传文件的路径</span>
    <span>$dir</span> = 'D:\phpStudy\WWW\loadimages\images'<span>;
    
    </span><span>//</span><span>缩略图的路径</span>
    <span>$thumbdir</span> = 'D:\phpStudy\WWW\loadimages\images\thumbs'<span>;
    
    </span><span>//</span><span>设置环境变量</span>
    <span>putenv</span>('GDFONTPATH='.'C:\Windows\Fonts'<span>);
    </span><span>$font</span> = "C:\Windows\Fonts\arial.ttf"<span>;
    
    </span><span>//</span><span>upload_image.php页面传递过来的参数,如果是上传图片</span>
    <span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
    {        </span><span>/*</span><span>代码重复
            if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
            {
                switch($_FILES['uploadfile']['error'])
                {
                    case UPLOAD_ERR_INI_SIZE: //其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
                        die('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
                    break;
                    case UPLOAD_ERR_FORM_SIZE: //其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
                        die('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');
                    break;
                    case UPLOAD_ERR_PARTIAL: //其值为 3,文件只有部分被上传
                        die('文件只有部分被上传');
                    break;
                    case UPLOAD_ERR_NO_FILE: //其值为 4,没有文件被上传
                        die('没有文件被上传');
                    break;
                    case UPLOAD_ERR_NO_TMP_DIR: //其值为 6,找不到临时文件夹
                        die('找不到临时文件夹');
                    break;
                    case UPLOAD_ERR_CANT_WRITE: //其值为 7,文件写入失败
                        die('文件写入失败');
                    break;
                    case UPLOAD_ERR_EXTENSION: //其他异常
                        die('其他异常');
                    break;
                }
            }
            </span><span>*/</span>
        
        <span>$image_caption</span> = <span>$_POST</span>['caption'<span>];
        </span><span>$image_username</span> = <span>$_POST</span>['username'<span>];
        </span><span>$image_date</span> = <span>date</span>('Y-m-D'<span>);
        </span><span>/*</span><span>getimagesize方法返回一个数组,
        $width : 索引 0 包含图像宽度的像素值,
        $height : 索引 1 包含图像高度的像素值,
        $type : 索引 2 是图像类型的标记:
        = GIF,2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 
        = TIFF(intel byte order),8 = TIFF(motorola byte order),
        = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM,
        $attr : 索引 3 是文本字符串,内容为&ldquo;height="yyy" width="xxx"&rdquo;,可直接用于 IMG 标记
        </span><span>*/</span>
        
        <span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
        
        </span><span>//</span><span>imagecreatefromgXXX方法从一个url路径中创建一个新的图片</span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMAGETYPE_GIF:
                <span>$image</span> = imagecreatefromgif(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>('你上传的文件是不支持的文件类型'<span>);
                </span><span>$ext</span> = '.gif'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_JPEG:
                <span>$image</span> = imagecreatefromjpeg(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>('你上传的文件是不支持的文件类型'<span>);
                </span><span>$ext</span> = '.jpg'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
            </span><span>break</span><span>;    
            </span><span>case</span> IMAGETYPE_PNG:
                <span>$image</span> = imagecreatefrompng(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>('你上传的文件是不支持的文件类型'<span>);
                </span><span>$ext</span> = '.png'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
            </span><span>break</span><span>;    
            </span><span>default</span>    :
                <span>die</span>('您上传的文件不支持的文件类型。'<span>);
        }
        
        
        </span><span>//</span><span>将图片改为1280X960
        //新建一个指定大小的真彩色图像big</span>
        <span>$big</span> = imagecreatetruecolor(1280, 960<span>);
        </span><span>//</span><span>重采样拷贝部分图像并调整大小为1280X960</span>
        imagecopyresampled(<span>$big</span>, <span>$image</span>, 0, 0, 0, 0, 1280, 960, <span>$array</span>[0], <span>$array</span>[1<span>]);
        
        
        </span><span>$query</span> = 'insert into images(image_caption,image_username,image_date) values ("'.<span>$image_caption</span>.'","'.<span>$image_username</span>.'",now())'<span>;
        </span><span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
        </span><span>$last_id</span> = <span>mysql_insert_id</span><span>();
        </span><span>//</span><span>用写入的id作为图片的名字,避免同名的文件存放在同一目录中</span>
        <span>$imagename</span> = <span>$last_id</span>.<span>$ext</span><span>;
        </span><span>$image_id</span> = <span>$last_id</span><span>;
        
        </span><span>$query</span> = 'update images set image_filename="'.<span>$imagename</span>.'" where image_id='.<span>$last_id</span><span>;
        </span><span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));

        </span><span>//</span><span>有url指定的图片创建图片并保存到指定目录</span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMAGETYPE_GIF:<span>
                imagegif(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_JPEG:<span>
                imagejpeg(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_PNG:<span>
                imagepng(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
        }
        </span><span>//</span><span>销毁由url生成的图片</span>
        imagedestroy(<span>$big</span><span>);
        imagedestroy(</span><span>$image</span><span>);
        
    }
    </span><span>else</span>  <span>//</span><span>如果图片已经上传,则从数据库中取图片名字</span>
<span>    {    
        </span><span>$query</span> = 'select image_id,image_caption,image_username,image_date from images where image_id='.<span>$_POST</span>['id'<span>];
        </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
        </span><span>//</span><span>将结果集转换成关联数据,再将关联数组的键名当作变量名,值作为变量的值。</span>
        <span>extract</span>(<span>mysql_fetch_assoc</span>(<span>$result</span><span>));
        </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$image_id</span>.<span>$_SESSION</span>['ext'<span>]);
    }

    </span><span>//</span><span>如果是保存图片</span>
    <span>if</span>(<span>$_POST</span>['submit'] == 'Save'<span>)
    {    
        </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['id']) && <span>ctype_digit</span>(<span>$_POST</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]))
        {
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
            {
                </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
            {
                </span><span>$image</span> = imagecreatefrompng(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
            {
                </span><span>$image</span> = imagecreatefromgif(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
        }
        </span><span>else</span><span>
        {
            </span><span>die</span>('指定的无效图像'<span>);
        }
        
        
        </span><span>$effect</span> = (<span>isset</span>(<span>$_POST</span>['effect'])) ? <span>$_POST</span>['effect'] : -1<span>;
        
        </span><span>switch</span>(<span>$effect</span><span>)
        {
            </span><span>case</span> IMG_FILTER_NEGATE:<span>
                imagefilter(</span><span>$image</span> , IMG_FILTER_NEGATE);     <span>//</span><span>将图像中所有颜色反转</span>
            <span>break</span><span>;
            </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
                imagefilter(</span><span>$image</span> , IMG_FILTER_GRAYSCALE);  <span>//</span><span>将图像转换为灰度的</span>
            <span>break</span><span>;
            </span><span>case</span> IMG_FILTER_EMBOSS:<span>
                imagefilter(</span><span>$image</span> , IMG_FILTER_EMBOSS);     <span>//</span><span>使图像浮雕化</span>
            <span>break</span><span>;
            </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
                imagefilter(</span><span>$image</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>//</span><span>用高斯算法模糊图像</span>
            <span>break</span><span>;    
        }
        
        </span><span>//</span><span>图片指定位置打印文字</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
        {    </span><span>/*</span><span>
            array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
            $image  图像源   $size    字体大小   $angle   逆时针旋转角度   $fontfile  字体类型   $text  文本内容
            </span><span>*/</span><span>
            imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
        }
        
        </span><span>//</span><span>图片指定位置打LOGO</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
        {
            </span><span>//</span><span>获取水印图片的尺寸并创建水印</span>
            <span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
            </span><span>//</span><span>$x = ($width-$wmk_width) / 2;
            //$y = ($height-$wmk_height)/2;</span>
            <span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
            </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
            </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
            </span><span>//</span><span>把水印图片和原图片合并在一起</span>
            <span>/*</span><span>
            将 wmk 图像中坐标从 0,0 开始,宽度为 $wmk_width,高度为 $wmk_height 的一部分拷贝到 image 图像中坐标为 $x 和 $y 的位置上。
            两图像将根据 pct=20 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为                     
            100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
            </span><span>*/</span><span>
            imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
            </span><span>//</span><span>清除水印图片</span>
            imagedestroy(<span>$wmk</span><span>);
        }
        
        </span><span>//</span><span>缩略图大小</span>
        <span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
        </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                       
        </span><span>//</span><span>创建一个缩略图</span>
        <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
        imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>, <span>$width</span>, <span>$height</span><span>);
        
        </span><span>//</span><span>保存缩略图</span>
        <span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
        {
            imagejpeg(</span><span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'], 100<span>);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
        {</span>
            imagepng(<span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
        {
            imagegif(</span><span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>echo</span> "修改好的缩略图:<img  src='"."images/thumbs/" . <span alt="php随笔1-php图片处理," >$_POST</span>['id'] . <span>$_SESSION</span>['ext']."' alt='' /> <br />"<span>;
        </span><span>//</span><span>清除缩略图</span>
        imagedestroy(<span>$thumb</span><span>);
        
        </span><span>//</span><span>保存原图</span>
        <span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
        {
            imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'] , 100<span>);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
        {
            </span><span>//</span><span>压缩等级必须是0-9的png文件</span>
            imagepng(<span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
        {
            imagegif(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
        }    
        </span><span>echo</span> "修改好的大图:<img  src='"."images/" . <span alt="php随笔1-php图片处理," >$_POST</span>['id'] . <span>$_SESSION</span>['ext']."' alt='' /> <br />"<span>;
    </span>?>
        <html>
            <head>
                <title>你的图片在这!</title>
            </head>
            <body>
                <h1 id="你的图片被保存">你的图片被保存!</h1>
                <img  src="images/<?php echo <span alt="php随笔1-php图片处理," >$_POST</span>['id'];?>.jpg" alt="" />
            </body>
        </html>
    <?<span>php 
    }
    </span><span>else</span><span>
    {   
    </span>?>
    <html>
        <head>
            <title>你的图片在这!</title>
        </head>
        <body>
            <p>这里是你上传到服务器上的图片:</p>
        </body>
    </html>
    <?<span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.<span>$_SESSION</span>['ext'<span>];
        }
        </span><span>else</span><span>
        {
            </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>];
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
            {
                </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>);
            }
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
            {
                </span><span>$imagename</span> .= '&logo=1'<span>;
            }
        }
    </span>?>
    <div>
    <?php  <span>echo</span>  "src:".<span>$imagename</span> ;  ?>
    <img  src="<?php echo <span alt="php随笔1-php图片处理," >$imagename</span>;?>">as</span>:</td>
            <td><?php <span>echo</span>  "moviesite数据库images表第".<span>$image_id</span> ."行!" ;?></td>
        </tr>
        <tr>
            <td>Height:</td>
            <td><?php <span>echo</span> <span>$height</span>;?></td>
        </tr>
        <tr>
            <td>Widht:</td>
            <td><?php <span>echo</span> <span>$width</span>;?></td>
        </tr>
        <tr>
            <td>Upload <span>date</span>:</td>
            <td><?php <span>echo</span> <span>$image_date</span>;?></td>
        </tr>
    </table>
    <p>你可以从下面的选项列表中对你的图像应用一个特殊的效果。注:使用任何一个过滤器,保存一个图像 <em>可以撤消</em></p>
    <form action="<?php echo <span>$_SERVER</span>['PHP_SELF'];?>" method="post">
        
            <input type="hidden" name="id" value="<?php echo <span>$image_id</span>;?>"/>
            <select name="effect" id="">
                <option value="-1">保持原状</option>
                <?<span>php 
                    </span><span>echo</span> '<option value="'.IMG_FILTER_GRAYSCALE.'" '<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_GRAYSCALE)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> ' >灰度图像</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_GAUSSIAN_BLUR)
                    {
                        </span><span>echo</span> ' selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>高斯算法模糊图像</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_EMBOSS.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_EMBOSS)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>图像浮雕化</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_NEGATE.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_NEGATE)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>图像中所有颜色反转</option>'<span>;
                </span>?>
            </select><br />   
            <?<span>php 
                </span><span>echo</span> '<input type="checkbox" name="emb_caption"'<span>;
                </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
                {
                    </span><span>echo</span> ' checked="checked"'<span>;
                }
                </span><span>echo</span> ' />是否嵌入在图像的标题?'<span>;
                </span><span>echo</span> '<br />'<span>;
                
                </span><span>//</span><span>添加水印选项</span>
                <span>echo</span> '<input type="checkbox" name="emb_logo" '<span>;
                </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
                {
                    </span><span>echo</span> 'checked="checked"'<span>;
                }
                </span><span>echo</span> ' />是否嵌入668LOGO?'. '<br />'<span>;
            </span>?>         
            <input type="submit" value="Save" name="submit" />
            <!--<input type="submit" value="Preview" name="submit" />-->
         </form>
        </div>

    <?<span>php 
    }
    </span><span>mysql_close</span>(<span>$db</span><span>);
    </span>?>

 

本来有一个Preview的,后来没弄了,但是代码还是给上  image_effect.php ,已经把Preview注释掉了。

<?<span>php 
</span><span>//</span><span>上传文件的路径</span>
<span>$dir</span> = 'D:\phpStudy\WWW\loadimages\images'<span>;
</span><span>//</span><span>设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');</span>
<span>$font</span> = "C:\Windows\Fonts\arial.ttf"<span>;

</span><span>if</span>(<span>isset</span>(<span>$_GET</span>['id']) && <span>ctype_digit</span>(<span>$_GET</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_GET</span>['id'].<span>$_SESSION</span>['ext'<span>]))
{
    </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_GET</span>['id'].<span>$_SESSION</span>['ext'<span>]);
}
</span><span>else</span><span>
{
    </span><span>die</span>('invalid image specified'<span>);
}

</span><span>$effect</span> = (<span>isset</span>(<span>$_GET</span>['e'])) ? <span>$_GET</span>['e'] : -1<span>;

</span><span>switch</span>(<span>$effect</span><span>)
{
    </span><span>case</span> IMG_FILTER_NEGATE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_NEGATE);
    </span><span>break</span><span>;
    </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GRAYSCALE);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_EMBOSS:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_EMBOSS);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GAUSSIAN_BLUR);
    </span><span>break</span><span>;    
}

</span><span>if</span>(<span>isset</span>(<span>$_GET</span>['capt'<span>]))
{
    </span><span>//</span><span>echo $_GET['capt'];</span>
    imagettftext(<span>$image</span>, 12, 0, 20, 20, 0, <span>$font</span>, <span>$_GET</span>['capt'<span>]);
}

</span><span>if</span>(<span>isset</span>(<span>$_GET</span>['logo'<span>]))
{
    </span><span>list</span>(<span>$widht</span> , <span>$height</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$_GET</span>['id'].<span>$_SESSION</span>['ext'<span>]);
    </span><span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
    </span><span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
    </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
    </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
    imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
    imagedestroy(</span><span>$wmk</span><span>);
}


</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
{
     </span><span>header</span>('Content-Type:image/jpeg'<span>);
     imagejpeg(</span><span>$image</span> , '' , 100<span>);
}
</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
{
    </span><span>header</span>('Content-Type:image/png'<span>);
    imagepng(</span><span>$image</span> , ''<span> );
}
</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
{
    </span><span>header</span>('Content-Type:image/gif'<span>);
    imagegif(</span><span>$image</span> , ''<span> );
}    

</span>?>

内容总结:

本人是自学的php,代码书写多有不规范之处,并且有些位置的代码有重复累赘。以后需要加强。

upload_image.php: 主要是 HTML+Javascritp+Css 没什么可说的,主要就 javascript 调用 window.URL.createObjectURL(fileObj) 方法。获取图片的相对路径,并做图片的展示。

check_image.php: 1、session 用于保存从 upload_image.php 传递过来的图片的后缀名。

          2、数据库moviesite创建,images表的创建  

    <span>$sql</span> = "<span>CREATE TABLE if not exists images                       
    (
        image_id int NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(image_id),
        image_caption varchar(15), 
        image_username varchar(15),
        image_filename varchar(15),
        image_date  datetime
    )</span>";

         3、使用 imagecreatetruecolor 和 imagecopyresampled 将图片大小调整为1280X960.然后将图片上传保存到指定位置。

<span>     //</span><span>将图片改为1280X960
        //新建一个指定大小的真彩色图像big</span>
        <span>$big</span> = imagecreatetruecolor(1280, 960<span>);
        </span><span>//</span><span>重采样拷贝部分图像并调整大小为1280X960</span>
        imagecopyresampled(<span>$big</span>, <span>$image</span>, 0, 0, 0, 0, 1280, 960, <span>$array</span>[0], <span>$array</span>[1]);

         4、如果图片已经上传,则从数据库中取图片名字

        <span>$query</span> = 'select image_id,image_caption,image_username,image_date from images where image_id='.<span>$_POST</span>['id'<span>];
        </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
        </span><span>//</span><span>将结果集转换成关联数据,再将关联数组的键名当作变量名,值作为变量的值。</span>
        <span>extract</span>(<span>mysql_fetch_assoc</span>(<span>$result</span><span>));
        </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$image_id</span>.<span>$_SESSION</span>['ext']);

         5、bool imagefilter ( resource $src_im , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 ]]] )  对图像使用过滤器   PS图片

<span> imagefilter() 把过滤器 filtertype 应用到图像上,在需要时使用 arg1,arg2 和 arg3。

filtertype 可以是下列中的一个:

    IMG_FILTER_NEGATE:将图像中所有颜色反转。
    IMG_FILTER_GRAYSCALE:将图像转换为灰度的。
    IMG_FILTER_BRIGHTNESS:改变图像的亮度。用 arg1 设定亮度级别。
    IMG_FILTER_CONTRAST:改变图像的对比度。用 arg1 设定对比度级别。
    IMG_FILTER_COLORIZE:与 IMG_FILTER_GRAYSCALE 类似,不过可以指定颜色。用 arg1,arg2 和 arg3 分别指定 red,blue 和 green。每种颜色范围是 0 到 255。
    IMG_FILTER_EDGEDETECT:用边缘检测来突出图像的边缘。
    IMG_FILTER_EMBOSS:使图像浮雕化。
    IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊图像。
    IMG_FILTER_SELECTIVE_BLUR:模糊图像。
    IMG_FILTER_MEAN_REMOVAL:用平均移除法来达到轮廓效果。
    IMG_FILTER_SMOOTH:使图像更柔滑。用 arg1 设定柔滑级别。</span>

          6、图片加文字

        <span>//</span><span>图片指定位置打印文字</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
        {    </span><span>/*</span><span>
            array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
            $image  图像源   $size    字体大小   $angle   逆时针旋转角度   $fontfile  字体类型   $text  文本内容
            </span><span>*/</span><span>
            imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
        }</span>

         7、图片打LOGO

        <span>//</span><span>图片指定位置打LOGO</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
        {
            </span><span>//</span><span>获取水印图片的尺寸并创建水印</span>
            <span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
            </span><span>//</span><span>$x = ($width-$wmk_width) / 2;
            //$y = ($height-$wmk_height)/2;</span>
            <span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
            </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
            </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
            </span><span>//</span><span>把水印图片和原图片合并在一起</span>
            <span>/*</span><span>
            将 wmk 图像中坐标从 0,0 开始,宽度为 $wmk_width,高度为 $wmk_height 的一部分拷贝到 image 图像中坐标为 $x 和 $y 的位置上。
            两图像将根据 pct=20 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为                     
            100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
            </span><span>*/</span><span>
            imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
            </span><span>//</span><span>清除水印图片</span>
            imagedestroy(<span>$wmk</span><span>);
        }</span>

          8、缩略图 128X96 大小

        <span>//</span><span>缩略图大小</span>
        <span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
        </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                       
        </span><span>//</span><span>创建一个缩略图</span>
        <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
        imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>, <span>$width</span>, <span>$height</span>);
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器